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);
}
 
Bit of a mismatch there because a char is a single character and the thing you're reading in may well be multiple characters long (a char array, or string). You can't compare two strings using == or != (see the strcmp function instead). Also look in to the different between the formatting tokens for functions like scanf and printf (%d vs. %c vs. %s, etc.)

This works but may well be poor C :)
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define buffer_length 20

int main(void)
{
	// Expecting more than one character as input, create a character array.
	char input[buffer_length] = {0};

	// Loop forever.
	while(1)
	{
		printf("Select option (0, 1, 2, 3 or 4): ");
		scanf_s("%s", input, buffer_length);

		// Compare the inputed string with "end". A result of 0 means they are identical.
		if (strcmp(input, "end") == 0)
		{
			// Break out of the enclosing while loop.
			break;
		}
		else
		{
			// Convert ASCII-to-Integer for the first char in the input string.
			int num = atoi(&input[0]);

			if ((num >= 0) && (num <= 4))
			{
				printf("You chose option %d\n", num);
			}
			else
			{
				printf("Error, invalid selection\n");
			}
		}
	}

	return 0;
}
 
Your case statements are expecting for integers, yet user_reply is a char. Try something like:-

switch(user_reply)
{
case '0': fprintf(stdout,"You chose option 0\n");
break;

etc..
 
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