I'm trying to make a program that asks the user what he/she wants to do, but when invalid inputs are entered, the program does not tell the user right away. Also, the sentinel value does not work. Thanks.

#include <stdio.h>
        
int main ()
                
{
        
        int selection;
        float sum, difference, product, quotient;
        float num1, num2;
        
                
        start:
                
        printf("****************************\n");
        printf("What would you like to do?\n");
        printf("1 = add \n2 = subtract \n3 = multiply \n4 = divide \n5 = exit\n");
        printf("***************\n");
        printf( "Enter your selection:");
        scanf("%d", &selection);
        printf("Please enter the first number:");
        scanf("%f", &num1);
        printf("Please enter the second number:");
        scanf("%f", &num2);
        printf("*************************\n");

        if ( selection==1 || selection==2 || selection==3 || selection==4 ) {

        switch (selection)
        {

        case 1:
                sum = num1 + num2;
                printf("Operation Result: %.2f\n", sum);
                break;
        case 2:
         	difference = num1 - num2;
                printf("Operation Result: %.2f\n", difference);
                break;
        case 3:
                product = num1 * num2;
                printf("Operation Result: %.2f\n", product);
                break;   
        case 4:
                quotient = num1 / num2;
        
                if( num2==0){
                        printf("The denominator cannot be zero.\nRe-enter denominator: ");
                        scanf("%f", num2);
                        printf("Operation Result: %.f\n", quotient);
                        break;}
        
                else printf("Operation Result: %.2f\n", quotient);
                break;
        }
        }
        
        if (selection<0 || selection >5)

                printf("Invalid Operation Selected\n");

        goto start;
         

        if (selection==5){
        printf( "Thanks for using this program.  Bye Bye!!\n");
        }

return 0;
}
#include <stdio.h>

int main ()

{
	int selection;
	float sum, difference, product, quotient;
	float num1, num2;


start:

	printf("****************************\n");
	printf("What would you like to do?\n");
	printf("1 = add \n2 = subtract \n3 = multiply \n4 = divide \n5 = exit\n");
	printf("***************\n");
	printf( "Enter your selection:");
	scanf("%d", &selection);
	printf("Please enter the first number:");
	scanf("%f", &num1);
	printf("Please enter the second number:");
	scanf("%f", &num2);
	printf("*************************\n");

	if ( selection==1 || selection==2 || selection==3 || selection==4 ) 
	{
		switch (selection)
		{

		case 1:
			sum = num1 + num2;
			printf("Operation Result: %.2f\n", sum);
			break;
		case 2:
			difference = num1 - num2;
			printf("Operation Result: %.2f\n", difference);
			break;
		case 3:
			product = num1 * num2;
			printf("Operation Result: %.2f\n", product);
			break;
		case 4:
			quotient = num1 / num2;

	if( num2==0)
	{
		printf("The denominator cannot be zero.\nRe-enter denominator: ");
		scanf("%f", num2);
		printf("Operation Result: %.f\n", quotient);
		break;
	}
	else 
		printf("Operation Result: %.2f\n", quotient);
		break;
	}
	}

	if (selection<0 || selection >5)
		printf("Invalid Operation Selected\n");

goto start;


	if (selection==5)
	{
		printf( "Thanks for using this program. Bye Bye!!\n");
	}

return 0;
}

the way you have your sential set up (goto start) your program will never end without a with a ctrl-c, plus your if statements in the wrong place.

I'm trying to make a program that asks the user what he/she wants to do, but when invalid inputs are entered, the program does not tell the user right away.

Meaning what? Like you enter 6 for selection and have to enter your two numbers before you are told 6 is wrong? If so, test the selection before asking for the two numbers.

Also, the sentinel value does not work.

What sentinel?

Have you read the recommended reading, like the Rules (mentioned when you signed up) and the post titled Read Me: Read This Before Posting. It's titled that for a reason.

i decided to take a different approach. now I cannot get case 4 to work properly. when 0 is entered for the denominator, the program asks for another number, but after the new denominator is entered there is a segmentation error. how can I make it loop only within case 4? Thanks.

#include <stdio.h>
        
int main ()     
        
{
        
        int selection;
        float sum, difference, product, quotient;
        float num1, num2;
                
                
        start:
                
        printf("****************************\n");
        printf("What would you like to do?\n");
        printf("1 = add \n2 = subtract \n3 = multiply \n4 = divide \n5 = exit\n");
        printf("***************\n");
        
                
        printf( "Enter your selection:");
        scanf("%d", &selection);
                        
        if (selection==1 || selection==2 || selection==3 || selection==4){
                printf("Please enter the first number:");
                scanf("%f", &num1);
                printf("Please enter the second number:");
                scanf("%f", &num2);
                printf("*************************\n");
                }
                
        switch (selection)      {
        
        case 1:
                sum = num1 + num2;
                printf("Operation Result: %.2f\n", sum);
 		break;
                        
        case 2:
                difference = num1 - num2;
                printf("Operation Result: %.2f\n", difference);   
                break;
                
        case 3:
                product = num1 * num2;
                printf("Operation Result: %.2f\n", product);
                break;
        
        case 4:
                quotient = num1 / num2;
        
                if( num2==0){
			printf("The denominator cannot be zero.\nRe-enter denominator: ");
                        scanf("%f", num2);
                        quotient = num1 / num2;
                        printf("Operation Result: %.2f\n", quotient);
                        break;}
                
                else printf("Operation Result: %.2f\n", quotient);
                break;
                
        case 5:
                printf( "Thanks for using this program.  Bye Bye!!\n");
                break;
                
        default:
                printf("Invalid Operation Selected\n");
        }
                
        while (selection!=5) 
        goto start;
                        
                        
return 0;
}
case 4:
quotient = num1 / num2;

if( num2==0){

Shouldn't you be checking to see if num2 is 0 before you divide? After all, that's most likely what's causing your program to segmentation fault. And you should change your if() statement into a while() statement so that it keeps looping until the user enters a nonzero value. Only after this has been achieved should you finally divide num1 by num2 to calculate the quotient.

And why are you using goto ? That's usually a no-no for C programmers. A perfectly good alternative would be to create a while() loop that continues until a bool variable is false. When the user enters '5', your program will run case 5 in your switch() statement. This is where you set the bool variable to false , and your program will now end.

And why are you using goto ? That's usually a no-no for C programmers. A perfectly good alternative would be to create a while() loop that continues until a bool variable is false. When the user enters '5', your program will run case 5 in your switch() statement. This is where you set the bool variable to false , and your program will now end.

Exactly. An alternative to setting a specific bool varialbe, the while can simply test selection with 5. Either way works fine.

And I don't want to correct your code tags a third time. How to use them is in the links I posted before AND on the background of the box you've entered both your posts.

Will give you a suggestion. Instead of using seperate if statement for non possible values you can use default in switch case.

USAGE:
default:
printf("\nwrong choice:");
break;

now it will go to default if any value other than 1,2,3,4 is selected!

and one more advice buddy, i just saw your programs again and saw that you have used 'goto' statement in your program. Atleast for C programs it is not advisible to use 'goto' statemet. Its usage should be strictly avoided. Instad you can use 'Do while' for repeating. if you want code see an example:

do
{
//ur total code between keyword of goto and goto statement
printf("do you want to continue?[Y/N]");
getch(ans); /* or getche(ans); will also work */
//'ans' is a character variable
}while(ans == 'y' || ans == 'Y');

getch(ans); /* or getche(ans); will also work */

Is that so? I'll bet it gives a compiler error with VS2005.
getchar() would be the standard alternative.

Niek

I use borland turbo c++3.0 for last 3 n half years for c and it has never given error for getch or getchar it works very great. It automatically yakes the input for getch. i.e. after giving input you dont have to press enter. Try it with borland!

Is that so? I'll bet it gives a compiler error with VS2005.
getchar() would be the standard alternative.

Niek

It does work with borland turbo c++3.0 IDE, use for 'C'.

That's right, but turbo 3 is outdated. The standard is getchar() and getch & getche won't work on any compiler that I know of (except turbo).
"If it only works in Turbo, it doesn't work"

thanks everyone for your help and advice. I figured out the problem and it was by far one of the silliest you can possibly make- i forgot the '&' in the scanf function.

and ill be sure to post code correctly next time. :)

That's right, but turbo 3 is outdated. The standard is getchar() and getch & getche won't work on any compiler that I know of (except turbo).
"If it only works in Turbo, it doesn't work"

ya you are probable right. The things should be universal and generic.
I will try it with another IDEs. Thanks.

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.