1

Possible Duplicate:
Program doesn’t wait for user input with scanf(“%c”,&yn);

I've just started programming in C, and something strange (which I didn't encounter in C++) is popping up.

In my header file, I declare a variable (see onemoretime in code for .h file), whose value is later set using user input through scanf(). For some reason, the conditionals which use the variable wouldn't work properly, either returning an infinitely repeating result, or just not doing anything. I set some breakpoints, and found that the first time onemoretime is called in the main() function, the value of onemoretime is, for some reason, set to \n.

This isn't a value scanf() is supposed to recognize, but the input is treated like a new line (obviously). When I enter y, which should trigger the else if conditional and loop back to the beginning of main(), Xcode's debugger simply says, error: 'y' is not a valid command.

Here's my .h file:

#ifndef C1_Header_h
#define C1_Header_h

float num1, num2;
char op, onemoretime;

#endif

... And here's the relevant code from the .c file:

#include <stdio.h>
#include <math.h>
#include "Header.h"

int main(int argc, const char * argv[])
{
    printf("Enter a two-number calculation below:\n");
    while (1)
    {
        scanf("%f%c%f", &num1, &op, &num2);

        // addition
        if (op == '+')
        {
            printf("%f\n", num1+num2);
            printf("\nDone. Would you like to solve another math problem (y/n)?\n");
            scanf("%c", &onemoretime);
            while (1)
            {
                if (onemoretime == 'n') 
                {
                    printf("Ok. Terminating program.");
                    return 0;
                }
                else if (onemoretime == 'y') 
                {
                    // loop back to start main function
                }
            }
        }
    }
        // rest of code, irrelevant here
}

If it's a factor at all, hovering over the onemoretime variable in Xcode (showing its basic properties), every instance of the variable after the initial declaration in the header file shows the value to be \n.

What am I doing wrong? How can the value of onemoretime be set by the user, rather than being set by the program before the user can get to it?

Community
  • 1
  • 1
Jules
  • 14,200
  • 13
  • 56
  • 101
  • "What am I doing wrong?" - two things at first glance: 1 you started with C++ and you're trying to learn C afterwards, 2. you're getting user input using `scanf()`, which in turn doesn't do what you think it does. –  Feb 02 '13 at 06:29
  • What's wrong with learning C alongside C++? – Jules Feb 02 '13 at 06:32
  • 1
    nC has some concepts that you definitely need to have a grasp on in order not to make mistakes. C++ has these as well, but with a twist, and since C++ is a more complex (and in fact, more difficult-to-get-right) language, I generally encourage people to master C before diving deep into C++ or they'll have problems like this one. –  Feb 02 '13 at 06:35
  • 2
    Just to disagree with H2CO3 - If you want to learn C++ only, there is no reason to learn C first. It will just give you another set of problems when relearning. If you want to learn any combination of C, C++, C#, and Java, you will have problems with looks-the-same-works-differently whichever order you try to learn them. – Bo Persson Feb 02 '13 at 10:36
  • 1
    H2CO3: ... and what happens when one finds out that the modulo operator in C++ doesn't act the same as the modulo operator in C, after they've written twenty-thousand lines of code that makes the assumption that they act the same? – autistic Feb 02 '13 at 11:33

2 Answers2

4

[\n] isn't a value scanf() is supposed to recognize, […]

That's true in general, but "The normal skip over white-space characters is suppressed in [the case of %c]" [link].

To get op and onemoretime, I'd recommend using fgetc (link) rather than scanf, and handling the whitespace-skipping yourself.

ruakh
  • 175,680
  • 26
  • 273
  • 307
0

Whenever you want to get one single character with scanf() you must always use something to absorb the newline character.You could do something like this scanf("%c\n",&onemoretime);