I want to keep entering a character till a '*' is entered and change the case of the character. The code I have written is
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
int main()
{
char character;
char con_char;
while(1)
{
printf("Enter a character:");
scanf("%c",&character);
printf("ASCII value of %c is %d\n", character, character);
if (character=='*')
{
printf("The program terminates\n");
exit(0);
}
else if (character>='a' && character<='z')
{
con_char = toupper(character);
printf("%c\t\t%c\n", character, con_char);
}
else if (character>='A' && character<='Z')
{
con_char = tolower(character);
printf("%c\t\t%c\n", character, con_char);
}
else
{
con_char=character;
printf("%c\t\t%c\n", character, con_char);
}
}
return 0;
}
The program works well but only for the format of the output. The line "Enter a character" is displayed in the output two times for a single input. I am not able to understand the reason. Please help.
Output:
Enter a character:f
ASCII value of f is 102
f F
Enter a character:ASCII value of
is 10
Enter a character:
Related
I am creating a very basic calculator in C but the output is not coming as desired.
#include<stdio.h>
int main(int argc, char const *argv[])
{
/* code */
char ch;
int a,b,p=0;
scanf("%d",&a);
while(1)
{
ch=getchar();
if(ch==';')
{
p=2;
break;
}
scanf("%d",&b);
if(ch=='+')
{
a+=b;
}
if(ch=='-')
{
a-=b;
}
if(ch=='*')
{
a*=b;
}
if(ch=='/' && b!=0)
{
a/=b;
}
if(ch=='/' && b==0)
{
printf("INVALID INPUT\n");
p=2;
break;
}
}
if(p!=0)
printf("%d",a);
return 0;
}
The Output is always coming as the initial value which has been assigned to "a".
Output which is coming-
4
+
5
;
4
Expected output -
4
+
5
;
9
Can you please help me with this issue of why the expression is not getting evaluated correctly?
The line
scanf("%d",&a);
will consume the first number from the input stream, but it will leave the newline character on the input stream.
Therefore, when you later call
ch=getchar();
it will read that newline character. It will not read the + character.
If you want to read the + character, then you can change that line to the following:
scanf( " %c", &ch );
This line will first discard all whitespace characters and will match the first non-whitespace character on the input stream.
Afterwards, your program will have the desired output:
4
+
5
;
9
An alternative solution would be to discard the rest of the line after every call to scanf that uses the %d format specifier. That way, calling getchar immediately afterwards should read the + character as intended.
You can discard the remainder of an input line using the following code:
//discard remainder of input line
{
int c;
do
{
c = getchar();
} while ( c != EOF && c != '\n' );
}
Here is a more compact way of writing the same thing:
//discard remainder of input line
for ( int c; (c=getchar()) != EOF && c != '\n'; )
;
The only problem is in scanning the inputs.
As a tab or a new line must seperate the value supplied to scanf.
In short, just add \n at the end of scanf.
scanf("%d\n", &a);
scanf("%d\n", &b);
this should do it.
I was going to make a loop that if I type the alphabet then the ascii value comes out. Unless I type '0' in.
but result is as below. There is the code that I made below the result. Where the value 10 is coming from?
Press any Alphabet
A
65
Press any Alphabet
10
Press any Alphabet
char aski;
while(1)
{
printf("Press any Alphabet\n");
scanf("%c", &aski);
if (aski == '0')
break;
else
printf("%d\n", aski);
}
scanf reads an extra \n. ASCII of \n is 10. That's why you get 10. I suggest you to use getchar() to read extra \n.
#include <stdio.h>
int main()
{
char aski;
while (1)
{
printf("Press any Alphabet\n");
scanf("%c", &aski);
getchar();
if (aski == '0')
break;
else
printf("%d\n", aski);
}
return 0;
}
The output is:
Press any Alphabet
a
97
Press any Alphabet
b
98
PS: I stopped excution after, entering b.
When you are pressing enter you are in fact creating a \n (new line) This char has the value of 10, which is what is being printed.
char c = '\n';
printf("%d",c);
Would give you 10 as result.
Try this
char aski;
scanf("%c ", &aski);
Notice the space after the %c, this makes sure to read all whitespace inputted.
I'm new to C and I'm currently making a tutorial.
I have decided to combine two exercices and I want to take inputs
as long as I don't just do < return >. This is my code until now:
#include<stdio.h>
char letter;
int i=0;
int main() {
while (i==0) {
printf(">> ");
letter = getchar();
printf("%d\n", letter);
if (letter==10) { //10 is the ASCII id for <return>
printf("End\n");
i = 1;
}
printf("You've entered %c\n", letter);
}
}
The problems is that when I enter a letter for the first time, it does what I want it to do: take a number, and return it on the screen. Directly after that it sets 10 on it's on own!
This is the output:
>> r //I've entered 'r'
114
Das Zeichen ist r
>> 10 //I've entered nothing. It entered that on it's own
End
The letter is
What is going on here? Why does it do that? I didn't enter anything or pressed anything.
The reason that when you enter "r" you also press Enter ('\n' which equals to ASCII 10). The shortest way is to add another getchar() inserted to consume the '\n':
int main() {
while (i==0) {
printf(">> ");
letter = getchar();
getchar();
printf("%d\n", letter);
if (zeichen==10) { //10 is the ASCII id for <return>
printf("End\n");
i = 1;
}
printf("You've entered %c\n", letter);
}
}
There is also another short way which is comparing letter value early after it's assignment like following:
int main() {
while (i==0) {
printf(">> ");
letter = getchar();
if(letter == '\n')
continue;
printf("%d\n", letter);
if (zeichen==10) { //10 is the ASCII id for <return>
printf("End\n");
i = 1;
}
printf("You've entered %c\n", letter);
}
}
however you still need to assign zeichen to something otherwise you'll get undefined behavior
Standard portable C API are not good to handle keystrokes. To "enter r" you need to press "r" "return" so your program using getchar() will receive them both immediately. Your need to use ncurses or cornio.
while (i==0) {
printf(">> ");
letter = getchar();
printf("%d\n", letter);
if (zeichen==10) { //10 is the ASCII id for <return>
printf("End\n");
break;
}
Use break statement instead of i=1;
If using Windows, use _getch() from "conio.h"
Also, instead of checking for 10 (which is the linefeed character not Return), it is better to check for \r, which is what the ENTER key returns. Never use literal values when more descriptive escapes can be used:
line feed = \n
Return = \r
etc.
I'm trying to make a simple program for which the user is supposed to enter character 'a'. It is supposed to loop until 'a' is input. I have one statement printed if there is no input which works correctly. There is another statement if an incorrect letter or number is input, but the problem is that this causes the program to loop more than once and it prints the statements multiple times. Any help in fixing this is appreciated.
#include <stdio.h>
int main()
{
char input;
int i, len,num;
len = 1;
do
{
puts("Please enter alphabet 'a': ");
scanf("%c", &input);
for(i=0; i<len; i++)
{
if(isalpha(input)==0)
{
printf("Please input something.\n");
continue;
}
if(input == 'A' || input == 'a')
{
printf("Congratulations! You successfully input letter 'a'.");
return(0);
}
else
{
printf("That's not letter 'a'.");
}
}
}
while(1);
}
The problem is that after entering the character, you press newline and this is send to the input buffer. Now the next time scanf() is called, it reads the value from the buffer which is '\n' and scanf() thus stores this to input. Now this can be easily solved by the method pointed by #Gopi, but there is a better way. This is the code.
#include <stdio.h>
#include<ctype.h>
int main()
{
char input,ch;
do
{
puts("Please enter alphabet 'a': ");
scanf("%c", &input);
while( input!='\n' && (ch=getchar())!='\n' && ch!= EOF); // look here
if(isalpha(input)==0)
{
printf("Please input something.\n");
continue;
}
if(input == 'A' || input == 'a')
{
printf("Congratulations! You successfully input letter 'a'.");
return(0);
}
else
{
printf("That's not letter 'a'.");
}
}
while(1);
}
Now with the statement while((ch=getchar())!='\n' && ch!= EOF);, all the characters like '\n' are just flushed and not stored to input and thus solves the problem.
Also note that you don't need the for loop here, its useless for this code ( unless this is not your original code and there are other parts in it ).
There is a newline character in the buffer after the first input which is not flushed and that is being picked up by the %c in the second iteration.
Change your scanf() to
scanf(" %c", &input);
Note the space before %c which gobbles the newline character
Can some one explain me why I see a double input of the printf() function the while loop:
#include <ctype.h>
#include <stdio.h>
int main(){
int x = 0;
while ( x != 'q'){
printf("\nEnter a letter:");
x=getchar();
printf("%c\n", x);
if( isalpha(x) )
printf( "You entered a letter of the alphabet\n" );
if( isdigit(x) )
printf( "You entered the digit %c\n", x);
}
return 0;
}
The output of the code in Debian Squeeze (gcc version 4.4.5 (Debian 4.4.5-8)) is:
Enter a letter:1
1
You entered the digit 1
Enter a letter: // why is the first one appearing ???
Enter a letter:2
2
You entered the digit 2
The first one reads the line terminator character you entered when hitting Enter after 1 (the line terminator will remain in the input buffer).
You can verify this by adding an else branch:
#include <ctype.h>
#include <stdio.h>
int main()
{
int x = 0;
while ( x != 'q')
{
printf("\nEnter a letter:");
x = getchar();
printf("%c\n", x);
if( isalpha(x) )
printf( "You entered a letter of the alphabet\n" );
else if( isdigit(x) )
printf( "You entered the digit %c\n", x);
else
printf("Neither letter, nor digit: %02X\n", x);
}
return 0;
}
The output:
Enter a letter:1
1
You entered the digit 1
Enter a letter:
Neither letter, nor digit: 0A
Enter a letter:2
The byte 0A is the line-feed character.
The second time through the loop, getchar() is getting the Enter after the first char you entered.
You could do something like
while ((c = getchar()) != EOF && c != '\n') {} /* eat the rest of the line */
to get rid of everything up to and including the next Enter after getting a char and before asking for another.
If you want to check characters as you enter a slightly advanced technique would be to change stdin's behavior to raw mode.Then as soon as the user hits a char you get that in your variable.
Check this for some start
Use CTRL + D to get the functionality of the newline character without its side effect.