This is my final assignment before the semester is over. For this program I am supposed to create a switch within a loop. The switch works but the program will not loop. Can you please help me.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

 


int main(void) 

{
	double counter, userResult, f, g, Result, tries;
	char opp, tryanother;


	unsigned seed = time(0);

	srand(seed);

	f = 1 + rand() % 10;

	g = 1 + rand() % 10;

	counter = 0;

	tries = 0;

	tryanother = 'y';

	cout<<"Please select a kind of operaton:"<<endl;
	cout<<"+ for addition"<<endl;
	cout<<"- for subtraction"<<endl;
	cout<<"* for multiplication"<<endl;
	cout<<"/ for division"<<endl;
	cout<<"================================================================================"<<endl;
	cin>>opp;

	cout<<"Are you ready? 'y' for Yes, 'n' for No:"<<endl;
	cin>>tryanother;

	while (tryanother = 'y'){

	  counter = counter + 1;


		switch (opp){
					
			case '+': cout<< f << opp << g <<endl;
						
				{
							
					double userResult, tries;

							cin>> userResult;

								Result = f + g;

							while ((tries <= 2) && (userResult != Result)) {

								cout<< "Incorrect, please try again" <<endl;

								cout<< f << opp << g <<endl;

								cin>>userResult;

								cout<<endl;

								tries ++;}

							if (tries > 2)

										cout<<"The correct answer is " << f << opp << g <<" = "<< Result <<endl;

							else cout<<"Correct! Good Job!"<<endl;

					cout<<"Try another one 'y' for Yes, 'n' for No:"<<endl;
					cin>>tryanother;

				}

					
						
						break;


			case '-': cout<< f << opp << g <<endl;
						
					{
							
						double userResult, tries;

							cin>> userResult;

								Result = f - g;

							while ((tries <= 2) && (userResult != Result)) {

								cout<< "Incorrect, please try again" <<endl;

								cout<< f << opp << g <<endl;

								cin>>userResult;

								cout<<endl;

								tries ++;}

							if (tries > 2)

										cout<<"The correct answer is " << f << opp << g <<" = "<< Result <<endl;

							else cout<<"Correct! Good Job!"<<endl;

					cout<<"Try another one 'y' for Yes, 'n' for No:"<<endl;
					cin>>tryanother;

					}

							break;


			case '*': cout<< f << opp << g <<endl;
						
					{
							
						double userResult, tries;

							cin>> userResult;

								Result = f * g;

							while ((tries <= 2) && (userResult != Result)) {

								cout<< "Incorrect, please try again" <<endl;

								cout<< f << opp << g <<endl;

								cin>>userResult;

								cout<<endl;

								tries ++;}

							if (tries > 2)

										cout<<"The correct answer is " << f << opp << g <<" = "<< Result <<endl;

							else cout<<"Correct! Good Job!"<<endl;

					cout<<"Try another one 'y' for Yes, 'n' for No:"<<endl;
					cin>>tryanother;

					}

						break;


			case '/': cout<< f << opp << g <<endl;
						
				{
						
					double userResult, tries;

						cin>> userResult;

							Result = f / g;

						while ((tries <= 2) && (userResult != Result)) {

							cout<< "Incorrect, please try again" <<endl;

							cout<< f << opp << g <<endl;

							cin>>userResult;

							cout<<endl;

							tries ++;}

						if (tries > 2)
									
									cout<<"The correct answer is " << f << opp << g <<" = "<< Result <<endl;

						else cout<<"Correct! Good Job!"<<endl;

					cout<<"Try another one 'y' for Yes, 'n' for No:"<<endl;
					cin>>tryanother;
				}

						break;

			default: cout<<"That is an invalid opperation"<<endl;}

	cout<<"Are you ready? 'y' for Yes, 'n' for No:"<<endl;
	cin>>tryanother;

			}

			while (counter!=0){

				if (tryanother = 'n')
		
						cout<<"Please select a kind of operaton:"<<endl;
						cout<<"+ for addition"<<endl;
						cout<<"- for subtraction"<<endl;
						cout<<"* for multiplication"<<endl;
						cout<<"/ for division"<<endl;
						cin>>opp;
						cout<<"================================================================================"<<endl;
			}
		
					 return 0;

}

Recommended Answers

All 3 Replies

while (tryanother [B]=[/B] 'y')

Change = to ==

while (tryanother = 'y'){
if (tryanother = 'n')

The equality operator is ==. The = operator is for assignment. Those two conditions are the same as these:

while ((tryanother = 'y') != 0){
if ((tryanother = 'n') != 0)

It finally works!!! Thanks you guys.

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.