#include <stdio.h>
int main()
{
float num1, num2,output;
char operator;
printf("Enter a number");
scanf("%f", &num1);
printf("Enter another number");
scanf("%f", &num2);
printf("Press + to add, - to subtract, * to multiply, / to divide.");
scanf("c", &operator);
if(operator == '+')
{
output = num1 + num2;
}
if(operator == '-')
{
output = num1 - num2;
}
if(operator == '*')
{
output = num1 * num2;
}
if(operator == '/')
{
output = num1 / num2;
}
printf("a");
scanf("%f", &output);
}
23 Replies - 5279 Views - Last Post: 25 September 2010 - 01:26 PM
#1 Guest_Guest*
How to prompt user to enter a value for char variable?
Posted 22 September 2010 - 05:11 PM
I'm working on a program that will ask a user to enter two numbers and a operator of their choice(+, -, *, /) Based on what they choose, the program will do that operation with both numbers. My problem right now is that I can't get the statement that prompts the user to enter a operator to show. Prompting the user for two numbers works fine but prompting for an operator does even show up. Here's my code:
Replies To: How to prompt user to enter a value for char variable?
#2
Re: How to prompt user to enter a value for char variable?
Posted 22 September 2010 - 05:13 PM
The format specifier for char is %c:
scanf("%c", &operator);
#3 Guest_Guest*
Re: How to prompt user to enter a value for char variable?
Posted 22 September 2010 - 05:17 PM
I just tried that and got the same results
#4
Re: How to prompt user to enter a value for char variable?
Posted 22 September 2010 - 05:39 PM
I have no idea why, but it really didn't work on MinGW...
I suggest you to use the C++ libraries:
I suggest you to use the C++ libraries:
#include <iostream> // c++ input/output
using namespace std; // standard namespace
int main() {
float num1, num2,output;
char op;
cout << "Enter a number: "; // equivalent to printf
cin >> num1; // equivalent to scanf
cout << "Enter another number: ";
cin >> num2;
cout << "Press + to add, - to subtract, * to multiply, / to divide: ";
cin >> op;
if(op == '+')
{
output = num1 + num2;
}
else if(op == '-')
{
output = num1 - num2;
}
else if(op == '*')
{
output = num1 * num2;
}
else if(op == '/')
{
output = num1 / num2;
}
cout << "Result = " << output;
return 0;
}
#5
Re: How to prompt user to enter a value for char variable?
Posted 22 September 2010 - 05:41 PM
Typical problem scanf + EOL.
Use this:
Don't know if it's the right way to do it, but it should do the trick.
And the last one:
Maybe you wanted to print the result right?
Use this:
scanf(" %f", &num1); //Whitespace at the beginning of the string
Don't know if it's the right way to do it, but it should do the trick.
And the last one:
scanf("%f", &output);
Maybe you wanted to print the result right?
printf("%f", output);
#6 Guest_Guest*
Re: How to prompt user to enter a value for char variable?
Posted 22 September 2010 - 05:49 PM
I tried leaving a space in front of the string but it still didnt work.
#include <stdio.h>
#include <math.h>
int main()
{
float num1, num2,output;
char operator;
printf("Enter a number");
scanf(" %f", &num1);
printf("Enter another number");
scanf(" %f", &num2);
printf("Press + to add, - to subtract, * to multiply, / to divide.");
scanf(" %c", &operator);
if(operator == '+')
{
output = num1 + num2;
}
if(operator == '-')
{
output = num1 - num2;
}
if(operator == '*')
{
output = num1 * num2;
}
if(operator == '/')
{
output = num1 / num2;
}
printf(" %f", &output);
}
#7
Re: How to prompt user to enter a value for char variable?
Posted 22 September 2010 - 05:55 PM
That's because you're printing output's address, not its value.
should be
printf(" %f", &output);
should be
printf("%f", output);
#8 Guest_Guest*
Re: How to prompt user to enter a value for char variable?
Posted 22 September 2010 - 06:02 PM
Tried it and still no luck
#9
Re: How to prompt user to enter a value for char variable?
Posted 22 September 2010 - 06:09 PM
Then you're doing it very wrong.
Works perfectly for me, with only one modification: the removal of the & operator on the last printf call.
Works perfectly for me, with only one modification: the removal of the & operator on the last printf call.
#10
Re: How to prompt user to enter a value for char variable?
Posted 22 September 2010 - 06:10 PM
Instead of the whitespace trick you can use:
But I'm pretty sure that way is a bad habit.
Anyway, I tried your code and worked fine here (mingw).
fflush(stdin);
But I'm pretty sure that way is a bad habit.
Anyway, I tried your code and worked fine here (mingw).
#11 Guest_c.user*
Re: How to prompt user to enter a value for char variable?
Posted 22 September 2010 - 06:27 PM
fflush() can be used only for output streams, stdin is an input stream
This post has been edited by c.user: 22 September 2010 - 06:27 PM
#12
Re: How to prompt user to enter a value for char variable?
Posted 22 September 2010 - 06:33 PM
#13
Re: How to prompt user to enter a value for char variable?
Posted 22 September 2010 - 07:02 PM
Quote
This is not true. The only thing about using it on input streams is the functionality isn't guaranteed on all platforms.
It's very few lines to portably and cleanly extract from the stream. Wrap that functionality into your own function and call that instead.
#14 Guest_c.user*
Re: How to prompt user to enter a value for char variable?
Posted 22 September 2010 - 07:51 PM
Seta00 said:
This is not true. The only thing about using it on input streams is the functionality isn't guaranteed on all platforms.
On all major implementations, it means clear the buffer.
On all major implementations, it means clear the buffer.
no, it gives undefined behavior (look in C99 and C89)
where did you find that it clears the buffer ?
Attached image(s)
#15
Re: How to prompt user to enter a value for char variable?
Posted 22 September 2010 - 08:48 PM
I did know that fflush(stdin) was a bad habit, didn't remember why.
Apparently, this EOL problem with scanf only appears when reading a char.
If the whitespace trick didn't work, you can try something like this:
or
To use the malloc version remember to include stdlib.h or malloc.h.
Apparently, this EOL problem with scanf only appears when reading a char.
If the whitespace trick didn't work, you can try something like this:
char c;
scanf("%c%c", (char*)malloc(sizeof(char)), &c);
or
char c, aux; //Won't use aux value
scanf("%c%c", &aux, &c);
To use the malloc version remember to include stdlib.h or malloc.h.
This post has been edited by anonymouscodder: 22 September 2010 - 08:49 PM
|
|

New Topic/Question
Reply
MultiQuote









|