Problem with returning from first if to second (nested if statement)

Hello everyone i`m new to this forum but not new to programming i was bored so i start making a program to enter your bill then program ask u to if u want to + or - the tva on the bill then after all this it ask u to enter the value of the tva.

the program works perfect but there is a small problem if u enter a wrong value above 100 then i have to start it all over . here the program.

#include <iostream>
using namespace std;

int main()
{
	int bill;
	float TVA,value;
	char o; //operator (+ or -)
	cout<<"___________________________________________________"<<endl;
	cout<<"Please enter your bill                             |"<<endl;
	cout<<"___________________________________________________|"<<endl;
	cin>>bill;
	system("cls");
	cout<<"would you like to '+' or '-' the tva"<<endl;
	cout<<"write (+) for raise tva or (-) for discount"<<endl;
	cin>>o;
	if(o !='+' && o !='-') //if u didn`t enter + or - then i dunno how to make it return to the same if
	{
		cout<<"wrong operator please write '+' or '-'"<<endl;
		return main(); 
	}
	else
	{
		system("cls");
     	        cout<<"___________________________________________________"<<endl;
                cout<<"please enter the value of the tva                  |"<<endl;
	        cout<<"___________________________________________________|"<<endl;
		cin>>value;
		if(value < 0 || value > 100) //if u didn`t enter less than 0 or greater than 100 then i dunno how to make it return to the same if
		{
			cout<<"wrong value please enter between 0 and 100"<<endl;
			return main();
		}
		else
		{
        	        system("cls");
			if(o == '+')
			{
				TVA=bill*(value/100);	
				cout<<"Bill Value:"<<bill<<endl;
				cout<<"bill value + TVA= "<<bill+TVA<<" with "<<value<<"%"<<endl;
			}
			else if(o == '-')
			{
				TVA=bill*(value/100);
				cout<<"Bill Value(L.L):"<<bill<<endl;
				cout<<"bill value - TVA= "<<bill-TVA<<" with "<<value<<"%"<<endl;
			}
		}
	}
	system("pause");
	return 0;
}


so now the problem in those lines

1
2
3
4
5
	if(o !='+' && o !='-') //if u didn`t enter + or - then i dunno how to make it return to the same if
	{
		cout<<"wrong operator please write '+' or '-'"<<endl;
		return main(); 
	}


and

1
2
3
4
5
if(value < 0 || value > 100) //if u didn`t enter less than 0 or greater than 100 then i dunno how to make it return to the same if
		{
			cout<<"wrong value please enter between 0 and 100"<<endl;
			return main();
		}


i don`t want to start it over by ordering the program to return to main, so is there any other way to make it return only to if statement?

Thanks for your help
What you're interested in is the "loop".

Try something like this:
1
2
3
4
5
6
7
8
cout<<"write (+) for raise tva or (-) for discount"<<endl;
cin>>o;
	
while (o !- '+' && o != '-')
{
        cout << "Invalid input, try again:"
        cin >> o;'
} 


Check this link out:
http://cplusplus.com/doc/tutorial/control/
Last edited on
You meant return to the cin? I don't see which if you'd want to return to.

Anyway.

Rather than recursion you should be using loops, like this:
1
2
3
4
5
6
7
cout << "enter something";
while(true) {
   cin >> value;
   if(satisfies_some_condition(value)) break;
   else cout << "wrong, try again";
}
do_stuff_with(value);
Thanks i fixed it using while.
Topic archived. No new replies allowed.