Trying to write a simple C program...having an issue

Discussion in 'Programmer's Corner' started by mbxs3, Feb 2, 2013.

  1. mbxs3

    Thread Starter Active Member

    Oct 14, 2009
    Unfortunately, I do not know many of the technical terms that would probably allow me to explain my issue a lot easier...but here is what my problem is...

    I want my program to take a character input and depending on the char value, it will determine an integer value to be used in an equation.

    My issue is that my program prints the correct statement to direct the user but does not allow the user to input a character.

    Code ( (Unknown Language)):
    2. //example
    4. {
    5. float final_value
    6. char multiplier
    8. printf("To multiply value by 2, enter (A), to multiply value by 20, enter (B)\n");
    9. scanf("%c", &multiplier);
    11. if(multiplier == 'A')
    12.   final_value =  2 * 20;
    14. if(multiplier == 'B')
    15.   final_value = 20 * 20;
    16. }
    This is just a snippet of my actual program. My program compiles fine and runs fine up until this string.
    Last edited by a moderator: Feb 3, 2013
  2. Ian Rogers


    Dec 12, 2012
    All C statements MUST end with ";" even the variable declarations.

    Sorry I assume this is a typo as the program compiles..

    The other thing here is scanf(); waits for a carraige return...

    So type A then enter.
    Last edited: Feb 3, 2013
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    Which string is that?
  4. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    Are you pressing enter?
  5. DiodeMan

    New Member

    Feb 3, 2013
    The only problem I can see as well is missing the ; after declaring the variables. I copied and pasted this snippet into Visual Studio, added a final printf to make sure it works properly, and I am having no problems.

    Maybe if you paste the entire code up here, we could be a bit more help?
  6. WBahn


    Mar 31, 2012
    Aside from the missing semicolons (typos in transcribing the code to your post?), the first thing I thought of was, "Is he expecting to just hit the A or B key and have something happen?". If so, then the problem, as already pointed out, is that scanf(), and the other ANSI-compliant input functions, work with the contents of a keyboard buffer and nothing gets put in that buffer until you hit ENTER.

    And, actually, that wasn't the first thing I thought of. The first thing I thought was, "Boy, is he in for a wakeup someday when he discovers, the hard way, that scanf() is a notoriously bad way for obtaining input, particularly live input from a human user. The problem is that if the input is not completely compatible with the format string, all bets -- well, most bets -- are off on how things will behave. The best way, in my opinion, to get input (if you are staying ANSI-compliant) is with fgets(). This allows you to get a string from the user while ensuring that you don't overflow your string buffer. You then have the ability to validate the contents of the string before doing any conversions or other manipulations. There may be better options under C99 or C11.
  7. killivolt

    Active Member

    Jan 10, 2010
    Post removed
    Last edited: Feb 9, 2013
  8. WBahn


    Mar 31, 2012
    That's an option, provided the bad things didn't happen while executing the scanf() function in the first place. This is one of the functions that can hold the door wide open for someone to figure out a way to execute a buffer overflow attack and take over your machine entirely. Now, if you are just using it to get a single char, then it is real hard to exploit that, so keep in mind I am talking about in general usage.