GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-May-2008, 12:30
lilballeer lilballeer is offline
New Member
 
Join Date: May 2008
Posts: 16
lilballeer is on a distinguished road

A do While loop.. how to loop correctly? for a calc prog.


Hello,
This program is suppsoe to display a menu and the user is suppose to select an option from the menu, then perform the operation...I have figured out how to get it to select the option and such, just cant get the program to continue to the menu selection after an operation was picked....

it should only quit when Q is selected, but it stays in Addition or subtraction till forever. How do I get it to go back to the menu selection??

Here is the code that I have:

CPP / C++ / C Code:
#include <iostream>
#include <cstdlib>
#include <ctime> 

using namespace std;

int main ()

{

	double x;
	double y;
	char choice;
	double youranswer; 
	double answer; 


	x = (rand() + time(0)) % 10;


	cout << "The 'random number' is: " ; 
		cout << x << endl;
y = (rand() + time(0)) % 10;


	cout << "The 'random number' is: " ; 
		cout << y << endl;

cout << "Please select one of the following options" << endl;
cout << "A - for addition" << endl;
cout << "D - for division" << endl;
cout << "M - for multiplication" << endl;
cout << "S - for subtraction" << endl;
cout << "Q - to quit the program" << endl;

cin>>choice; 

do 
if (choice == 'A')
{
cout << "Add" << x << "+" << y << endl;
answer = x + y;
cin >> youranswer;
if (youranswer == answer)
	cout << "your right"<< endl;
	else 
		cout << "Incorrect Answer: the correct answer is: " << answer << endl; 

}
else
if (choice == 'S')
{
	cout << "Subtract: " << x << "-" << y << endl;
	answer = x - y;
	cin >> youranswer;
	if (youranswer == answer)
		cout << "your right" << endl;
	else
		cout <<"Incorrect Answer: the correct answer is: " << answer << endl;
}


while (choice != 'Q'); 
cout << "Goodbye" << endl;

return 0; 


}

  #2  
Old 11-May-2008, 13:44
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: A do While loop.. how to loop correctly? for a calc prog.


Quote:
Originally Posted by lilballeer
Hello,
This program is suppsoe to display a menu and the user is suppose to select an option from the menu, then perform the operation...it stays in Addition or subtraction

Well, look (yes, look) at the code.

Here's what I see:
Code:
1. Generate numbers x and y for the problem. 2. Print menu prompt. 3. Get user input into variable choice. 4. Repeat the following loop until the value of choice is equal to 'Q': Use the value of choice print the values of x and y and to prompt the user for the answer. Get user input for answer. Print message indicating whether answer is correct or not. 5. Leave the loop when choice is equal to 'Q'. 6. Print 'Goodbye". 7. Exit the program.

Once you enter the loop, there is no new problem, no more menu, no more prompt, no more user input for the operation choice.

Shouldn't your loop be more like the following, so that you can repeatedly create a new problem and print the menu and get user selection?
CPP / C++ / C Code:
    do {
        x = (rand() + time(0)) % 10;
        cout << "The 'random number' is: ";
        cout << x << endl;
        y = (rand() + time(0)) % 10;

        cout << "The 'random number' is: ";
        cout << y << endl;
.
.
.
    } while (choice != 'Q');
    cout << "Goodbye" << endl;
.
.
.

Regards,

Dave

Footnote: For the message indicating the correct answer, "your right" is grammatically incorrect. The contraction for "You are" is "You're"
  #3  
Old 11-May-2008, 13:46
Mararia Mararia is offline
New Member
 
Join Date: Dec 2006
Location: Tenerife, Canary Islands, Spain
Posts: 25
Mararia is on a distinguished road

Re: A do While loop.. how to loop correctly? for a calc prog.


Hi! Your program keeps doing just one thing, because you only tell it to do one thing... As you have it, you never change the value of 'choice'. For it to do what you want it to do, you should insert the choice menu in the do..while loop. That way, it'll keep asking what to do, and doing it (untill you press 'Q')

OOPS.. someone was quicker than me...
 
 

Recent GIDBlogFirst week of IA training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Text-Based Roulette Game mfm1983 CPP / C++ Forum 5 29-Nov-2006 12:20

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:18.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.