Yes No Option C# Start Program Again While Loop

#one

  • D.I.C Head

Reputation: 2

  • View blog
  • Posts: 156
  • Joined: 31-January 12

Adding Yes/No Option

Posted 09 September 2012 - 08:49 AM

#include<iostream> #include<conio.h> using namespace std; int chief() {	 	 	int x; 	cout<<"Choose a Plan\north"; 	cout<<"[1] Test 1\n"; 	cout<<"[2] Test 2\n"; 	cout<<"[3] Test 3\northward"; 	cout<<"Enter your choice: "; 	cin>>ten; 	 	 	if(x==i){ 	int sum=0; 	bladder ave; 	cout<<"TEST\n"; 	int test[2][iii]={{one,ii,iii}, 					{iv,five,six}}; 	for(int x=0;x<=1;ten++){ 		for(int y=0;y<=2;y++){ 		cout<<test[x][y]<<" "; 		sum=sum+test[x][y]; 		} 		} 	ave=sum/6; 	cout<<"The sum is: "<<sum;  } 	cin.ignore(); 	cin.get(); 	return 0; 	 }            

Afterward choosing 1 and executing the codes within how do i add "Practice yous want to continue [Y/North]" to brand it get dorsum to the carte.

Thanks in accelerate


Is This A Good Question/Topic? 0

  • +

#two jimblumberg User is offline

Reputation: 5916

  • View blog
  • Posts: 17,933
  • Joined: 25-December 09

Re: Adding Yes/No Option

Posted 09 September 2012 - 08:52 AM

Utilise a while loop controlled past an if statement?

Jim

#3 Untalented User is offline

  • D.I.C Caput

Reputation: ii

  • View blog
  • Posts: 156
  • Joined: 31-Jan 12

Re: Adding Yes/No Option

Posted 09 September 2012 - 09:02 AM

Something like

while(true){ if(condition){ blah blah } if(condition){ blah blah } }            

I yet dont know how to add the yes no loop i read some post that it needs bool quit=false;

#include<iostream> #include<conio.h> using namespace std; int main() {	 	char choice; 	bool quit = false; 	int x; 	cout<<"Choose a Program\n"; 	cout<<"[1] Test 1\north"; 	cout<<"[2] Test 2\n"; 	cout<<"[3] Test 3\n"; 	cout<<"Enter your choice: "; 	cin>>x; 	 	while(true) 	{ 	if(ten==one){ 	int sum=0; 	float ave; 	cout<<"Test\n"; 	int exam[2][3]={{1,2,3}, 					{4,5,6}}; 	for(int 10=0;x<=1;x++){ 		for(int y=0;y<=2;y++){ 		cout<<exam[x][y]<<" "; 		sum=sum+test[x][y]; 		} 		} 	ave=sum/6; 	cout<<"The sum is: "<<sum;  }  cout<<"Practice y'all want to continue [Y/N]";  cin>>choice;  if (option !='Y')  { 		quit = truthful; 		pause; 		} 	cin.ignore(); 	cin.get(); 	return 0; 	 } }            

This post has been edited past Untalented: 09 September 2012 - 09:05 AM

#iv jimblumberg User is offline

Reputation: 5916

  • View blog
  • Posts: 17,933
  • Joined: 25-December 09

Re: Adding Yes/No Selection

Posted 09 September 2012 - 09:thirteen AM

Quote

I don't think you need two if statements, and maybe instead of while(true) you could use something like:

while(ch == 'y')            

Where ch is a character that has been assigned a value of 'y' to start over, annihilation else to exit. But don't forget that 'y' is not the same as 'Y'.

Also you may want to include your menu in the while loop, if you want to exist able to select a dissimilar functioning.

Jim

#5 Untalented User is offline

  • D.I.C Caput

Reputation: two

  • View blog
  • Posts: 156
  • Joined: 31-January 12

Re: Calculation Yep/No Pick

Posted 09 September 2012 - 09:26 AM

#include<iostream> #include<conio.h> using namespace std; int primary() {	 	char ch; 	bool quit = false; 	int x; 	cout<<"Choose a Program\northward"; 	cout<<"[1] Test 1\northward"; 	cout<<"[two] Exam 2\n"; 	cout<<"[3] Exam iii\northward"; 	cout<<"Enter your selection: "; 	cin>>x;  	 	while(ch == 'y' || ch == 'Y') 	{ 	cout<<"Choose a Program\n"; 	cout<<"[i] Exam 1\n"; 	cout<<"[2] Test 2\n"; 	cout<<"[three] Test three\north"; 	cout<<"Enter your choice: "; 	cin>>ten; 	if(10==1){ 	int sum=0; 	bladder ave; 	cout<<"TEST\n"; 	int test[ii][3]={{1,2,iii}, 					{4,5,6}}; 	for(int 10=0;x<=1;x++){ 		for(int y=0;y<=ii;y++){ 		cout<<test[x][y]<<" "; 		sum=sum+test[10][y]; 		} 		} 	ave=sum/6; 	cout<<"The sum is: "<<sum;  }  cout<<"Practise y'all desire to keep [Y/N]";  cin>>ch;  if (ch !='Y' || ch !='y')  { 		quit = true; 		break; 		} 	cin.ignore(); 	cin.get(); 	return 0; 	 } }            

Later pressing i the compiler simply exits.

Am i getting shut or further?

#6 jimblumberg User is offline

Reputation: 5916

  • View blog
  • Posts: 17,933
  • Joined: 25-December 09

Re: Adding Yes/No Option

Posted 09 September 2012 - 09:32 AM

You need to find an indentation style you like and apply information technology consistently. The manner your code is shortly formatted information technology is hard to tell where 1 control structure starts and ends. Past properly indenting your code it volition make post-obit your logic much easier. Then please properly reformat your lawmaking and repost the modified results.

Jim

#vii Untalented User is offline

  • D.I.C Caput

Reputation: ii

  • View blog
  • Posts: 156
  • Joined: 31-January 12

Re: Adding Yep/No Option

Posted 09 September 2012 - 09:37 AM

Thank you for helping Mr. Jim i actually appreciate it. I will do this again tomorrow, its already 12:37AM here i need to rest.

Thanks again..

#eight shurd User is offline

  • D.I.C Head

Reputation: 37

  • View blog
  • Posts: 162
  • Joined: 05-Feb 12

Re: Adding Yeah/No Option

Posted 09 September 2012 - 07:14 PM

Ok here's the matter you got that boolean quit variable which you lot are not using at all, it probably was from an older revision just y'all don't need information technology now anymore, take information technology out.
Y'all can also lose the kickoff office of your code, lines ix to 14, the menu. By using while(ch == 'y' || ch == 'Y') at the start instead of using a practise{}while(ch == 'y' || ch == 'Y') you have to ready your ch variable to y or Y, as it will never enter the loop equally information technology was not initialized equally a valid rule for the while(), if you prefer you can let the variable as it is and utilize a exercise{}while() loop which will get you the aforementioned results.
Take lines 42 to 46 out of the code as well, concluding but not to the lowest degree your loop should finish right after the cin>>ch.

Hope that helped

larsenwomess53.blogspot.com

Source: https://www.dreamincode.net/forums/topic/291407-adding-yesno-option/

0 Response to "Yes No Option C# Start Program Again While Loop"

Enregistrer un commentaire

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel