Need some more C programming help

Posted:
in Genius Bar edited January 2014
I've been making some strides in learning C, and for some reason I cannot make a simple switch statement work properly. I've tried writing two different programs that are simple letter-guessing games.



Every time, you answer the first question properly, it sends you to question 2 and then directly to the default: statement.



Answering the first question incorrectly displays the default: statement twice before dropping you back to the first question.



Any help would be immensely appreciated.I'm new at this, so please forgive my ignorance and mistakes.



[code]



#include <stdio.h>



int main()



{



char q1_input = 0;

char q2_input = 0;

short main_count = 0;



printf("This is a very simple letter-guessing quiz");



Question1:

{

printf("\

\

What letter am I thinking of?");

scanf("%c", &q1_input);



switch (q1_input)

{

case 'C': case 'c':

printf("\

Excellent! You guessed right!");

main_count += 5;

goto Question2;

break;



default:

printf("\

Sorry, try again!");

goto Question1;

break;

}

}



Question2:

{

printf("\

You have %d points", main_count);

printf("\

What letter am I thinking of now?");

scanf("%c", &q2_input);



switch (q2_input)

{

case 'D': case 'd':

printf("\

\

Correct!");

main_count += 5;

goto End;

break;



default:

printf("\

Nein!");

break;

}

}

End:

{

printf("\

\

You ended up with %d points!", main_count);

}

}

</pre><hr></blockquote>

Comments

  • Reply 1 of 8
    programmerprogrammer Posts: 3,458member
    I strongly suggest that you remove the gotos and restructure your code so that you don't need them... and then forget that you ever learned that "goto" exists in C/C++.



    The source of your problem is likely to be something to do with how scanf works. If I recall correctly scanf doesn't return until you end the input line, and then it will read the key you typed plus the end-of-line character(s). scanf("%c\

    ", &input); might work better, but I don't have that much recent experience with console input in C.
  • Reply 2 of 8
    Trying scanf("%c\

    ", &q1_input);
    produced some interesting results. I tried swapping the position of %c and \

    and made it work.



    Thanks for your help! That little tidbit of info you gave me is nowhere to be found in the book I've been reading.
  • Reply 3 of 8
    instead of case 'C': case 'c':

    look at the to_upper function in stdio.h
  • Reply 4 of 8
    Which book is recommending goto? Dijkstra would be rolling over in his grave.



    [ 11-03-2002: Message edited by: Rick1138 ]</p>
  • Reply 5 of 8
    [quote]Which book is recommending goto?<hr></blockquote>



    Beginning C by Ivor Horton.



    It only gets a breif mention, not exactly a reccomendation but it doesn't offer any alternatives.



    I need to learn some.



    Why all the hate for goto, anyway?
  • Reply 6 of 8
    mcqmcq Posts: 1,543member
    I was taught in my AP courses that goto was something that should almost never be done, and is usually never needed. I wasn't even aware that C++ had a goto function available. I think it ruins program flow, and make it harder for someone else to follow the code/program execution. I could very well be wrong on this, so I'm sure someone else could give a better reasoning why it's frowned upon.
  • Reply 7 of 8
    It should only be used to break out of deeply nested loop structures,and only to return control to the code immediately following said loop.The problem with goto and its cousin gosub,as you said is that their use creates code that is difficult to follow,and much worse difficult to change and maintain.Throw away any book that uses goto,seriously.Kernighan and Ritchie's The C Programming Language is an excellent book,though all the code exercises and examples are written in a somewhat outdated bytemaster style,they are still excellent to learn from.These are the guys who wrote the language-when you want to know something go directly to the masters.



    [ 11-04-2002: Message edited by: Rick1138 ]</p>
  • Reply 8 of 8
    I also think you need to add some color to your error messages.Instead of "Sorry, try again!" do something like "Wrong,you stupid asshole.What the **** have you been smoking?"
Sign In or Register to comment.