How to prompt user to enter a value for char variable?

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 7435 Views - Last Post: 25 September 2010 - 01:26 PM Rate Topic: -----

#1 Guest_Guest*


Reputation:

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:

   #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);

 }



Is This A Good Question/Topic? 0

Replies To: How to prompt user to enter a value for char variable?

#2 Seta00  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 84
  • Joined: 22-September 10

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);

Was This Post Helpful? 0
  • +
  • -

#3 Guest_Guest*


Reputation:

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
Was This Post Helpful? 0

#4 Seta00  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 84
  • Joined: 22-September 10

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:
#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;
}


Was This Post Helpful? 0
  • +
  • -

#5 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

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:
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);

Was This Post Helpful? 0
  • +
  • -

#6 Guest_Guest*


Reputation:

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);

 }


Was This Post Helpful? 0

#7 Seta00  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 84
  • Joined: 22-September 10

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.
printf(" %f", &output);


should be
printf("%f", output);


Was This Post Helpful? 0
  • +
  • -

#8 Guest_Guest*


Reputation:

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 :(
Was This Post Helpful? 0

#9 Seta00  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 84
  • Joined: 22-September 10

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.
Was This Post Helpful? 0
  • +
  • -

#10 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 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:
fflush(stdin);


But I'm pretty sure that way is a bad habit.

Anyway, I tried your code and worked fine here (mingw).
Was This Post Helpful? -1
  • +
  • -

#11 Guest_c.user*


Reputation:

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

Was This Post Helpful? 0

#12 Seta00  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 84
  • Joined: 22-September 10

Re: How to prompt user to enter a value for char variable?

Posted 22 September 2010 - 06:33 PM

View Postc.user, on 22 September 2010 - 05:27 PM, said:

fflush() can be used only for output streams, stdin is an input stream


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.
Was This Post Helpful? -1
  • +
  • -

#13 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

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.
To be correct, calling fflush with an input stream as an argument is an undefined operation. Which more or less means don't do it.

It's very few lines to portably and cleanly extract from the stream. Wrap that functionality into your own function and call that instead.
Was This Post Helpful? 0
  • +
  • -

#14 Guest_c.user*


Reputation:

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.

no, it gives undefined behavior (look in C99 and C89)
where did you find that it clears the buffer ?

Attached image(s)

  • Attached Image

Was This Post Helpful? 0

#15 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

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:
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

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2