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.
Related
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.
This question already has answers here:
Clarification needed regarding getchar() and newline
(4 answers)
Closed 4 years ago.
I have this following code.
#include <stdio.h>
int main () {
char letter1;
char letter2;
printf("Enter Letter 1: ");
letter1 = getchar();
printf("\n");
printf("Enter Letter 2: ");
letter2 = getchar();
printf("\n");
printf("%c - %c\n", letter1, letter2);
return(0);
}
I am trying to use getchar() to define letter1 and letter2, however it will only work for letter1, then set the value of letter2 to '\n'. Does anyone know a work around for this, so I can type values for both variables?
This is because when you press the first letter and type the Enter key, you send the codes for the letter and the '\n' one after the other to the input buffer from where it will be consumed.
So the first getchar() get the code for the letter you typed and thus the letter is removed from the input buffer but that of \n remains which is consumed by the next getchar().
Inorder to avoid this, you need to keep consuming from the input buffer till the '\n' is consumed.
One way of doing this is
int c;
while( (c=getchar())=='\n' && c!=EOF);
This will keep on consuming from the input buffer as long as the read character is a \n and EOF is not reached. getchar() would return EOF on error.
int letter1, letter2;
int c;
printf("Enter Letter 1: ");
letter1 = getchar();
printf("\n");
printf("Enter Letter 2: ");
while( (c=getchar())=='\n' && c!=EOF);
letter2=c;
printf("\n");
printf("%c - %c\n", letter1, letter2);
Edit: As pointed out in the comments, note that getchar() returns an int and not a char. See this post.
So here is my code.
int main(int argc, char* argv[])
{
char c;
size_t place;
while (1) {
scanf("%c %u", &c, &place);
printf("%c\n", c);
}
return 0;
}
When I compiled and ran program I expected to see terminal like this:
a 1
a
b 2
b
c 3
c
But I saw this:(some extra '\n')
a 1
a
b 2
b
c 3
c
Please help me find what I did wrong.
You may try like this:
scanf(" %c %u", &c, &place);
^^--Add space here
instead of
scanf("%c %u", &c, &place);
printf("char is %c\n", c); Adding some string before printing the received character will helps you to understand more clearly why its behaving like this. After your input some blank spaces or new line is entered, that is used in the next scanf. You can even print the ascii value of the charater like printf("char is %d, %c", c, c); and serach the ASCII value in the ASCII table, to understand exactly which value (blanks or new line or tab) scanf has received mistakenly.
Keeping blankspace as first character in format string of scanf will helps to skip all leading blanks(including tabs and newline). Like scanf(" %c %u", &c, &place);
I am trying to make the program print out "OK" for as long as i enter 'y' as a choice at the end, but it is not looping, it just prints out "OK" and ends the program even if i enter a 'y' at the end. Please help.
#include <stdio.h>
int main()
{
char c = 'y';
while (c == 'y')
{
printf_s("OK\n");
scanf_s("%c", &c);
if (c != 'y')
{
break;
}
}
return 0;
}
On first iteration when you press Enter key then a newline character \n is passed to the input along with y. On second iteration scanf_s reads \n.
Change
scanf_s("%c", &c);
to
scanf_s(" %c", &c);
^Notice the space before %c
A space before %c specifier can consume any number of white-space characters.
Change the scanf_s line as follows
scanf_s("%c", &c, 1);
This extra parameter is specifying the size of the c argument. Plain old scanf doesn't require this argument but the versions ending with _s do
Also the if block with the break statement is unnecessary because the conditional on the while loop effectively does the same thing. It could be written as follows
while (c == 'y')
{
printf_s("OK\n");
scanf_s("%c", &c, 1);
}
OK I used scanf instead of scanf_s, that solved the problem, thanks everyone.
You are wrong with scanf
if you use like this u will see it is working ..
#include <stdio.h>
int main()
{
int c = 1;
while (c == 1)
{
printf_s("OK\n");
scanf_s("%d", &c);
if (c != 1)
{
printf_s("hello\n");
continue;
}
}
return 0;
}
Put a space before %c to skip whitespace.
I am trying to make a very basic program to compare 2 numbers. after entering the 2 numbers the user is asked if they want to compare the 2 numbers. if y/n for yes or no. the problem I run into is that the program does not seem to ask for my input and immediately goes to the next print statement. Here is the code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main(void){
int n1;
int n2;
char ch;
printf("compare 2 numbers, input a number\n");
scanf("%d", &n1);
printf("your first number is %d\n", n1);
printf("enter your second number to compare \n");
scanf("%d", &n2);
printf("your second number is %d\n", n2);
printf("do you want to compare these numbers? y/n \n");
//here is the problem. after entering y, the program closes.
//at this point I just want it to print the letter it was given by the user.
scanf("%c", &ch);
//the print statement that is supposed to print the letter the user inputs
printf("%c is a vowel.\n", ch);
getch();
return 0;
}
//I was using this code as a reference which runs correctly
#include <stdio.h>
int main()
{
char ch;
printf("Enter a character\n");
scanf("%c", &ch);
if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I'
|| ch =='o' || ch=='O' || ch == 'u' || ch == 'U')
printf("%c is a vowel.\n", ch);
else
printf("%c is not a vowel.\n", ch);
return 0;
}
When you do
printf("enter your second number to compare \n");
scanf("%d", &n2);
you will enter the second number and press ENTER. This ENTER (\n) is still there in the buffer.
The scanf function removes whitespace automatically. scanf() leaves the new line char in buffer when you are using %c (%c are the exception they don't remove whitespace).
scanf("%c", &ch);
instead of this use
scanf("\n%c", &ch);
When you use %c,spaces and "escape character" as a valid character.So it will be stored in the ch.In the first two scanf,you use the %d can do no wrong.
In the second program,you call scanf("%c", &ch); at the first.This problem does not arise.If you call again, also can appear the same problem.
The reason for this problem is newline character \n leftover by the previous scanf after pressing Enter. This \n is left for the next call of scanf.
To avoid this problem you need to place a space before %c specifier in your scanf.
scanf(" %c", &C);
...
scanf(" %c", &B);
...
scanf(" %c", &X);
A space before %c is able to eat up any number of newline characters.
OR
You can use scanf to eat the single character without assigning it to anything like this::
scanf( "%[^\n]%*c", &C ) ;
%[^\n] tells the scanf to read every character that is not '\n'. That leaves the '\n' character in the input buffer, then the * (assignment suppression) will consume the a single character ('\n') but would not assign it to anything.