i'm wrting a basic C program which the user "talks" to the program, you know the sort. well my main menu looks like this:

int main()
{  char name[50];
  char mood;
printf("hey there, .. whats your name?\n");
  gets(name);
 
printf("%s is it? Well how are you today?\n a.Good\n b.Ok\n c.Bad\n", name);
  scanf("%c", &mood);
  switch(mood)
  {
    case 'a' : getnum(); break;
    case 'c' : bad(); break;
    case 'b' : printf("aw, cool \n"); break;
    default : printf("please choose good bad or ok in lower case 'cause i'm lazy\n");
  }
return 0;
}

that works fine, asks the persons name, displays their input, then goes to the menu. the switch on this works and if you chose a. then it goes to the sum function

void getnum()
{
 int num;
 printf("yay! me too! want to do a sum? enter a number: ");
 scanf("%d", &num);
 printf("%d squared is %d\n", num, square(num) );
 printf("nice chatting to ya!\n");
 
}

if you chose c. (bad) it goes to the bad file, prints the "reply oh no, want to tell me about it? Y or N?\n". now after that i've tried manya differnet techniques to scan the input (as it did for the previous questions), from scanf then a switch. to fgets and using a if.. else statement.. but it always cuts out and finishes, completly ignores the next part of the coding.

a different program i wrote (using coding out of a book) using switch statements had the same problem, just stops after the first or second input, anyone know why? i've tried using a cast to convert the character entered to interger (ASCII value) then using an IF statement, but it still won't give a space to read the input, just flies straight through back to c:\documents and settings etc. (this is all run through the command prompt.

is the answer staring me in the face?

Member Avatar for iamthwee

Welcome to the wonders of 'c' I bet you the whitespace is tripping you up.

Stop using crappy input techniques, and you'll likely discover that the problems you are currently experiencing will vanish. gets is your first mistake.
http://www.cprogramming.com/tips/showTip.php?tip=3&count=30&page=1 scanf leaves a trailing newline in the input buffer. This is bad because other functions will grab the newline left behind, and input functions will be skipped, etc, etc. If you need to grab a single character from the input buffer, use getchar() , but make sure you get rid of the newline after you retrieve the character. Here's a better explanation:
http://www.gidnetwork.com/b-60.html

The proper way to do input is to use functions such as fgets which grab an entire line. Of course, then you end up with a newline in the string, but this is easily removed. Here is a good input tutorial which you should read to learn more about this:
http://www.daniweb.com/tutorials/tutorial45806.html

that screwed it up completly. now the first menu doesn't even work.

printf("%s is it? Well how are you today?\n a.Good\n b.Ok\n c.Bad\n", name);
  getchar(); while (getchar() != '\n');
  switch(mood)

/\ /\ which bit did i screw up? should there be an &mood in the ()?
i used that tutorial before and same problem still..

How come you aren't assigning the return value of getchar() to a variable? I'd say there's a pretty low chance of the results of the function magically jumping into your 'mood' variable.

And post your updated code so that we can see what else is going on.

twhich bit did i screw up? should there be an &mood in the ()?
i used that tutorial before and same problem still..

if you are talking about the switch(mood) you don't need to write the
& operator there.

Did you take a look at the links that Joeprogrammer posted for you?.
They will help you to understand why is not working.

If you did. Could you post the changes you did to your code? That would help a lot.

ok i got:

int main()
{  char name[50];
  char mood;
printf("hey there, .. whats your name?\n");
  gets(name);
 
printf("%s is it? Well how are you today?\n a.Good\n b.Ok\n c.Bad\n", name);
  mood = getchar(); while (getchar() != '\n');
  switch(mood)
  {
    case 'a' : getnum(); break;
    case 'c' : bad(); break;
    case 'b' : printf("aw, cool \n"); break;
    default : printf("please choose good bad or ok in lower case 'cause i'm lazy\n");
  }
return 0;
}

and

void bad()
{ char text, neg;
 char pos[500];
fputs("oh no, want to tell me about it? Y or N?\n", stdout);
text =getchar(); while (getchar() != '\n');
switch(text)
{case 'Y' : fputs("go ahead..\n", stdout); fgets(pos, 500, stdin); break;
case 'N' : printf("ok well feel like a sum?\n"); neg =getchar(); while (getchar() != '\n'); break;
default : printf("please enter Y or N\n");} 
}

and it worked! waho, thanks for the help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.