C programming help (using a switch)

Joined
5 Aug 2006
Posts
11,419
Location
Derbyshire
Trying to do the following tutorial question, but got a bit stuck!
The program needs to repeatedly ask the user for a choice, then it should keep looping back to the start unless the user types 'end'.

Here is what I have so far - I cannot have user_reply at the while bit as it is an integer (I have done this currently but unsure how to correct it!)

#include <stdio.h>

int main(void)
{

char user_reply;

do
{
fprintf(stdout," Select option (0, 1, 2, 3 or 4):");
scanf("%d", &user_reply);

switch(user_reply)
{
case 0: fprintf(stdout,"You chose option 0\n");
break;
case 1: fprintf(stdout,"You chose option 1\n");
break;
case 2: fprintf(stdout,"You chose option 2\n");
break;
case 3: fprintf(stdout,"You chose option 3\n");
break;
case 4: fprintf(stdout,"You chose option 4\n");
break;

default: printf,(stdout,"Error, invalid selection\n");
}
}while (user_reply !="end");

return(0);
}
 
wush, your C is many more times advanced than mine!!!!:)
Looking at yours, using what dan uk said and looking at a sample programin my text book I came up with this, for some reason the default message never shows though!
Typing END will end the program, but typing anything else at all will carry on (Yes is not required, just mashing some random keys will do:p)
Thanks for the quick help:)


#include <stdio.h>
#include <string.h>

int main(void)
{

char reply[4];
int number;

do
{
fprintf(stdout," Select option (0-4):");
fscanf(stdin,"%d", &number);

switch(number)
{
case 0: fprintf(stdout,"You chose option 0\n");
break;
case 1: fprintf(stdout,"You chose option 1\n");
break;
case 2: fprintf(stdout,"You chose option 2\n");
break;
case 3: fprintf(stdout,"You chose option 3\n");
break;
case 4: fprintf(stdout,"You chose option 4\n");
break;

default: fprintf,(stdout,"Error, invalid selection\n");
}


fprintf(stdout,"Type YES to continue, type END to end\n");
fscanf(stdin,"%s",&reply);

}while (strncmp(reply,"end",3) != 0);

return(0);
}
 
Back
Top Bottom