My code is behaving weirdly. In loop 1 it is working perfectly but in loop 2 it is automatically printing ASCII value 10. please help!.
#include<stdio.h>
int main(){
char c;
int loop =1;
do{
printf("\nLoop = %d\nWrite character of which you want to find acii values: ", loop);
scanf("%c", &c);
printf("\nASCII value of %c is %d.", c, c);
loop++;
}while(c != 'Z');
printf("\n******END*****");
return 0;
}
10 is ASCII for newline. You are reading whitespace, which you can avoid by using
scanf(" %c", &c);
Note the additional blank before the character specifier. It instructs scanf to ignore white space.
Is there a simple way to make sure you're reading a character through scanf. If it were an integer I'd use a do while loop
do{
printf("enter a number");
fehler = scanf(" %d", &x);
getchar();
} while(fehler!=1);
But I'm not fully sure what to do if the input is meant to be a string. I know the alphabets are stored as ASCII values but the if constraints in the while statement don't seem to be working(unless I'm doing it wrong)
char * temp2;
temp2 = malloc(sizeof(string));
do{
printf("PLease enter a string: ");
scanf(" %s", temp2);
getchar();
} while(temp2 <= 'A' && temp2 <= 'z')
You can't compare a string to a single character. You have to loop through the entire string, checking every character.
#include <ctype.h>
int is_alphabetic(char *str) {
for (int i = 0; str[i]; i++) {
if (!isalpha(str[i])) {
return 0;
}
}
return 1;
}
...
do{
printf("Please enter an alphabetic string: ");
scanf(" %s", temp2);
getchar();
} while(!is_alphabetic(temp2));
You see printf and scanf work independently. Whatever you store be it a character or number is stored in form of a number. Now it depends on the printf function what it demands.
Eg.: If you store 'a' at a location, the number 97 is stored. Now if you print a number it prints 97 and if you demand a character it gives a.
#include <stdio.h>
int main()
{
int i = 97;
printf("%d \n", i);
printf("%c", i);
return 0;
}
See the results. Further char, int , long int are just data types which specify the number of bits that would be resrved for the inputs for the variable.
Execute this program and you'll understand:
#include <stdio.h>
int main()
{
int i;
for (i=97; i <=200 ; i++)
{
printf("%d %c,\t",i,i);
};
return 0;}
This will show you a nmber when printed as a number and then the SAME number read as character.
Note there are no markers in memory to store which type of data it is. It is straightforward stored as number.
scanf is absolutely the wrong tool for this. But if you want to read only alphabetic characters, you can do it easily enough with something like:
char s[32];
if( 1 == scanf(" %31[a-zA-Z]", s) ){ ... }
The %31[a-zA-Z] conversion specifier will match only the literal characters a thru z and A thru Z, and will only consume up to 31 characters of input. You must always use a field width modifier with %s or %[] conversion specifiers to avoid an overflow.
I read chars until '\n', convert them to int and sum the numbers until the result is only one digit.
I can't use mod or .
The first run went well, but the second one keep running and not waiting to \n.
any reason for keeping the '\n'?
#include<stdio.h>
int main(){
char str[8], conv_str[8],c;
int i,val,ans = 0;
while(1){
printf("Enter 8 values(0-9) :\n");
scanf("%[^\n]", str); // Scan values to str untill \n
for(i = 0;i < 8;i++){
val = str[i]-48; //convert from asci to int
ans += val;
}
while(ans > 9){
// itoa convert int to string, str(the input) is the buffer and 10 is the base
itoa(ans,conv_str,10);
ans = (conv_str[0]-48) + (conv_str[1]-48) ;
}
printf("the digit is: %d", ans);
printf("\ncontinue? (y/n)\n");
scanf("%s", &c);
if (c == 'n')
break;
memset(str, 0, sizeof(str));
}
return 0;
}
TIA
You have multiple problems in the code. Some of them are
scanf("%s", &c); is wrong. c is a char, you must use %c conversion specifier for that.
You never checked for the return value of scanf() calls to ensure success.
While scanning for character input, you did not clear the buffer of any existing inputs. Any existing character, including a newline ('\n') already present in the buffer will be considered as a valid input for %c. You need to clear the buffer before you read a character input.
In my code below in C, I wonder why it does't work when I prompt (ask) the user to enter firstly an int then followed by char, as I think it should?
The following is my code:
void square(int side, char fillCharacter);
int main() {
int inputVal;
char inputChar;
printf("Enter side value: ");
scanf("%d", &inputVal);
printf("Enter any character: ");
scanf("%c", &inputChar);
square(inputVal, inputChar);
return 0;
}
void square(int side, char fillCharacter) {
int i, j;
for ( i = 1; i <= side; i++ ) {
for ( j = 1; j <= side; j++) {
printf("%c", fillCharacter);
}
printf("\n");
}
}
The "%c" format specifier matches white space characters and the "%d" specifier leaves the trailing '\n' inserted when you pressed Enter/Return, so you need to instruct scanf() to ignore white space characters with the "%c" specifier by explicitly adding a white space in front of the specifier like this
scanf(" %c", &inputChar);
/* ^_ explicit white space here */
read scanf()'s, linux manual page and search for the "%c" specifier to see what I mean.
Here is my sample code :
char a;
char str [20];
unsigned char b[8] ;
unsigned char c[8];
int argsread;
int i;
init_8051();
while(1)
{
printf("\n enter a 64 bit operation \n");
argsread = scanf("%s", str);
sscanf(str, "0x%x%c0x%x", b, &a, c);
printf("\n %d arguments read \n",argsread);
for(i=0;i<8;i++)
{
printf("\n %#x %c %#x\n",b[i],a,c[i]);
}
}
}
The problem here is that when i enter for example in the terminal the following input :
0x1234567890abcdef+0x1234567890abcdef
this leads to an error where output for char array is totally wrong and most of the numbers are traced into a , which should have been the plus sign , am ai doing something fundamentally wrong ?
Update:
I changed my code to the following :
while(1)
{
printf("\n enter a 64 bit operation \n");
argsread = scanf("%s", str);
sscanf(str, "0x%s%c0x%s", b, &a, c);
printf("\n %d arguments read \n",argsread);
printf("\n %s \n",b);
}
and i increased size of str to 30 the problem is the output of the program is :
1 arguments read
abcdef+0xabcdef
so the value of b instead of being just abcdef it the the whole string!!
Update2:
Found this code which works perfect but i wanted to use scanf instead of cin here is the code
:`#include <iostream>
using namespace std;
int main()
{
float a, b, result;
char oper, clear;
cout << "Please enter an equation: i.e 5+5 " << endl;
for (;;) {
cin >> a;
cin >> oper;
cin >> b;
if (oper == '+')
result = a + b;
else if (oper == '-')
result = a - b;
else if (oper == '*')
result = a * b;
else if (oper == '/')
result = a / b;
cout << "= " << result << endl << endl;
cin.clear();
cin.ignore();
}
} `
User input and error handling much easier if code starts with fgets().
Then use sscanf(), strtol(), etc. to parse.
printf("\n enter a 64 bit operation \n");
char buf[100];
if (fgets(buf, sizeof buf, stdin) == NULL) Handle_IOErrororEOF();
char a;
char b[17]; // use right size arrays
char c[17];
// use width of 16 and %[]
if (sscanf(buf, " 0x%16[0-9abcdef] %c 0x%16[0-9abcdef]", b, &a, c) != 3) {
Handle_Bad_Input();
}
OTOH, just use an integer format specifier that allows hex input "%x" or "%i"
unsigned long long b,c;
if (sscanf(buf, "%lli %c%lli", &b, &a, &c) != 3) {
Handle_Bad_Input();
}
Why char str [20]; scanf("%s", str); has trouble:
"%s" does 3 things:
1) scans, but does not save, all preceding (0 or more) white-space (' ', '\t', '\n ', etc.).
2) scans and saves all non-white-space.
3) Finally reaching a white-space. it stops scanning and puts that white-space back into stdin.
The "%s" specifier lacks a width, like "%19s", so it can easily overfill str
sscanf(str, "0x%s%c0x%s", b, &a, c); has trouble too.
Input has no white-space between the end of the first number and the '+', so "%s" continues scanning. Code does not check the return value from sscanf() and then uses a, b, c. So a, b, c may not be properly scanned nor initialized - leading to potential undefined behavior .
You are reading a string using scanf and storing it in an integer. I think that is what is causing you the problem.
Also, from the MAN Page
AME
scanf, fscanf, sscanf, vscanf, vsscanf, vfscanf
...
RETURN VALUE
These functions return the number of input items successfully matched
and assigned, which can be fewer than provided for, or even zero in the
event of an early matching failure.
The value EOF is returned if the end of input is reached before either
the first successful conversion or a matching failure occurs. EOF is
also returned if a read error occurs, in which case the error indicator
for the stream (see ferror(3)) is set, and errno is set indicate the
error.
I think you missplaced the assignment, if you want to know how many arguments matched, then change this
argsread = scanf("%s", str);
sscanf(str, "0x%x%c0x%x", b, &a, c);
to this
scanf("%s", str);
argsread = sscanf(str, "%x%c%x", b, &a, c);
According to your comments, you should change some other things, may be this code will work
char a;
char str [64];
unsigned int b;
unsigned int c;
char B[11];
char C[11];
int argsread;
while(1)
{
printf("\n enter a 64 bit operation \n");
scanf("%s", str);
argsread = sscanf(str, "0x%x%c0x%x", &b, &a, &c);
if (argsread == 3)
{
printf("\n %d arguments read\n", argsread);
snprintf(B, sizeof(B), "0x%08X", b);
snprintf(C, sizeof(B), "0x%08X", c);
printf("\n %s%c%s\n", B, a, C);
}
}
return 0;