This question already has an answer here:
How to read / parse input in C? The FAQ
(1 answer)
Closed 6 years ago.
Hi consider the simple program below:
int main(void)
{
//exercise 1
float num2;
printf("please enter a number \n");
scanf_s("%f", &num2);
printf("the number multiple by 3 is %3.3f\n", num2 * 3);
//exercise 2
char ch1, ch2, ch3, ch4;
printf("enter a word with four char\n");
ch1 = getchar();
ch2 = getchar();
ch3 = getchar();
ch4 = getchar();
printf("the chars in reverse order are\n");
putchar(ch4);
putchar(ch3);
putchar(ch2);
putchar(ch1);
putchar('\n');
}
the output is:
please enter a number
2
the number multiple by 3 is 6.000
enter a word with four char
ffff
the chars in reverse order are
fff
3 chars printed to the console, if i move the code block of exercise 2 above 1:
int main(void)
{
//exercise 2
char ch1, ch2, ch3, ch4;
printf("enter a word with four char\n");
ch1 = getchar();
ch2 = getchar();
ch3 = getchar();
ch4 = getchar();
printf("the chars in reverse order are\n");
putchar(ch4);
putchar(ch3);
putchar(ch2);
putchar(ch1);
putchar('\n');
//exercise 1
float num2;
printf("please enter a number \n");
scanf_s("%f", &num2);
printf("the number multiple by 3 is %3.3f\n", num2 * 3);
}
the result as expected:
enter a word with four char
ffff
the chars in reverse order are
ffff
please enter a number
2
the number multiple by 3 is 6.000
i want to know why it's working when i change the order the code block and how can i solve it, thank you.
want to know why it's working when i change the order the code block and how can i solve it,
That's because scanf_s("%f", &num2); leaves newline character in the input buffer. So your first getchar(); will interpret that newline as ch1.
For this case, a silent preceding getchar would do:
getchar(); // will consume the remaining newline from stdin
ch1 = getchar();
ch2 = getchar();
ch3 = getchar();
ch4 = getchar();
There is a newline character when you input the first floating number, and it is input as a char by the call to getchar. Another way to fix this is to get entire line as a string using fgets, then parse it with whatever format you want:
char line[512];
printf("please enter a number \n");
fgets(line, sizeof line, stdin); // the newline is consumed here
sscanf(line, "%f", &num2);
ch1 = getchar(); // working as expected
Related
I am trying to write a program that takes in 5 characters then takes in a number and a letter and switches the character at the index/number to the new character. I think I have it but it is not working and defaulting the number to 0.
Also, is there a way to get both inputs at the same time?
char str[5];
int index;
char temp;
printf("Enter five characters\n");
scanf("%s", str);
printf("Please enter a number.\n");
scanf("%d", &index);
printf("Please enter a letter.\n");
scanf("%s", &temp);
str[index - 1] = temp;
printf("The five characters are now %s\n", str);
accessing the char array with the index variable is giving me the first element always.
FYI: I haven't code in C for a long, long time; however, I did run this code below successfully. You might have to review it and fix other things I might have missed.
First, we will change the first scanf by indicating we want only 5 characters scanf("%5c", str); and I'm indicating we want the last position in the array to have.
Next, we will do a for loop because we want to only accept an index between 1 to 5.
Finally, and this is important, we change the scanf to get one character; however, this isn't intuitive as before, because you need a space before the character indicator: scanf(" %c", &temp);
Code:
#include <stdio.h>
int main()
{
char str[5];
printf("Enter five characters\n");
scanf("%5c", str);
int index = -1;
while (index < 1 || index > sizeof(str)){
printf("Please enter a number 1 to %d.\n", (int) sizeof(str));
scanf("%d", &index);
}
char temp;
printf("Please enter a letter.\n");
scanf(" %c", &temp);
str[index - 1] = temp;
printf("The five characters are now %s\n", str);
return 0;
}
Result:
Enter five characters
12345
Please enter a number 1 to 5.
1
Please enter a letter.
x
The five characters are now x2345
...Program finished with exit code 0
Press ENTER to exit console.
#include <stdio.h>
int main()
{
char str[5];
int index;
char temp;
printf("Enter five characters\n");
scanf("%s", str);
printf("Please enter a number.\n");
scanf("%d", &index);
printf("Please enter a letter.\n");
scanf(" %c", &temp);
str[index - 1] = temp;
printf("The five characters are now %s\n", str);
return 0;
}
Output:
Enter five characters
12345
Please enter a number.
2
Please enter a letter.
x
The five characters are now 1x345
Press any key to continue . . .
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 1 year ago.
I'm learning C and I created a small program which outputs the ASCII code of the entered character or outputs the character from ASCII Code entered. It first asks the user whether they want to convert Character to ASCII Code or ASCII Code to Character.
I don't know what the problem is in my code. When I run it as a separate file, it works fine.
# include <stdio.h>
int main(void) {
char character;
printf("Enter a character: \n");
scanf("%c", &character);
printf("The character you entered: %c\n", character);
printf("ASCII code of %c is %d\n", character, character);
}
The above code works fine. This is the code which converts character to ASCII Code.
Here's the code which has the option to choose which one to run :
#include <stdio.h>
int main(void) {
char option;
printf("-------- MENU --------\n");
printf("1. Get ASCII code of a character.\n");
printf("2. Get character from ASCII code.\n");
printf(": ");
scanf("%s", &option);
if (option == '1') {
char character;
printf("Enter a character: \n");
scanf("%c", &character);
printf("The character you entered: %c\n", character);
printf("ASCII code of %c is %d\n", character, character);
}
else {
if (option == '2') {
int number;
printf("Enter a Number: \n");
scanf("%d", &number);
printf("The number you entered: %d\n\n", number);
printf("The ASCII character with code %d is %c\n", number, number);
}
else {
printf("Invalid Input !\n");
}
}
}
The above code asks the user to type in 1 or 2 to decide to run Convert Character to ASCII Code or Convert ASCII Code to Character. When I run this code, If I choose option 2, it works just fine and gives me a proper result and if I give 3 or any other invalid option, it gives me "Invalid Input !" printed on the screen. But, when I choose option 1, It just prints out this
-------- MENU --------
1. Get ASCII code of a character.
2. Get character from ASCII code.
: 1
Enter a character:
The character you entered:
ASCII code of
is 10
I don't know what the problem is. Please help me figure it out.
the problem is the second scanf and the %c. You have to add a white space like this scanf(" %c").
Full code:
#include <stdio.h>
int main(void) {
char option;
printf("-------- MENU --------\n");
printf("1. Get ASCII code of a character.\n");
printf("2. Get character from ASCII code.\n");
printf(": ");
scanf("%s", &option);
if (option == '1') {
char character;
printf("Enter a character: \n");
scanf(" %c", &character);
printf("The character you entered: %c\n", character);
printf("ASCII code of %c is %d\n", character, character);
}
else {
if (option == '2') {
int number;
printf("Enter a Number: \n");
scanf("%d", &number);
printf("The number you entered: %d\n\n", number);
printf("The ASCII character with code %d is %c\n", number, number);
}
else {
printf("Invalid Input !\n");
}
}
}
Output:
-------- MENU --------
1. Get ASCII code of a character.
2. Get character from ASCII code.
: 1
Enter a character:
a
The character you entered: a
ASCII code of a is 97
Please look at the following question were the reason is explained.
Hi I am new to programming and I got a trouble when I try to make a little change to the example in the book.
/* Chapter 3 Example, C Prime Plus */
#include <stdio.h>
int main(void)
{
char Letter, ch;
int intValue;
printf("Please enter a letter: \n");
scanf("%c", &Letter); /* user inputs character */
printf("The code for %c is %d.\n", Letter, Letter);
printf("Now is another we to implement the process: \n");
printf("RN, the value of ch is %c, and the value of intValue is %d\n", ch, intValue);
printf("Please enter a letter: \n");
scanf("%c", &ch);
intValue = ch;
printf("The code for %c is %d.\n", ch, intValue);
return 0;
}
When I run it, the outcome would be
Please enter a letter:
M
The code for M is 77.
Now is another we to implement the process:
RN, the value of ch is , and the value of intValue is 0
Please enter a letter:
The code for
is 10.
and the part
"
Now is another we to implement the process:
RN, the value of ch is , and the value of intValue is 0
Please enter a letter:
The code for
is 10. " will all come out without asking me to enter a value.
I want to know why and are there any other way to implement it that is different from examples in the book?
Thank you for your time!
Hi Matt_C and welcome to SO.
First, you don't need the second bloc of printfs and the scanf, it just trying to do the same thing and there is an order error.
Second, it is tricky when you try consecutive scanf, it holds the last key pressed (enter is the last key pressed = \n). This is why it skips the second scanf.
There a little solution for that, add a space at the beginning of the scanfs. Try this:
int main() {
char exit, letter;
while (1) {
printf("Please enter a letter: ");
scanf(" %c", &letter);
printf("\nThe code for '%c' is %d. \n\n", letter, letter);
printf("Exit ? (y/n): ");
scanf(" %c", &exit);
if(exit == 'y')
{
break;
}
system("clear"); // UNIX
//system("cls"); // DOS
}
}
Don't forget to choose one answer that you believe is the best solution to your problem.
#include <stdio.h>
// source tutorial points
int main(){
int c;
char d;
printf("Enter First value \n");
c = getchar();
printf("Enter Second value \n");
d = getchar();
printf("You have entered first \n");
putchar(c);
printf("You have entered second \n");
putchar(d);
return(0);
}
when I am entering first value it is not asking for other value please help I do not expect a character not possible in char.
Because each time you input a character and hit ENTER. So, second getchar in your code reads the enter character.
Your code should change to:
c = getchar();
getchar(); // for consuming the enter character
printf("Enter Second value \n");
d = getchar();
getchar(); // for consuming the enter character
The output:
Enter First value
a
Enter Second value
b
You have entered first
a
You have entered second
b
In the following code, why are the values of ch2 and ch3 equal to -1?
char ch1;
char ch2;
char ch3;
printf("put Type: ");
ch1 = getchar();
_flushall();
printf("put Type: ");
ch2 = getchar();
_flushall();
printf("put Type: ");
ch3 = getchar();
printf("\n");
printf("the ascii value for this three types is: %d, %d, %d", ch1, ch2, ch3);
If getchar encounters an error, it returns EOF (which is a macro that expands into an int with a negative value, typically "-1"). This explains why you see the -1.
As to why getchar fails, this is a separate question. Usually it means that it has reached the end of the input stream. I assume this has something to do with the fact that you're flushing it with _flushall?