6 Replies - 12548 Views - Last Post: 12 February 2008 - 03:14 AM Rate Topic: -----

#1 Rstewart79  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-February 08

C++ For, DO while loop questions

Posted 08 February 2008 - 11:49 AM

Need to have this run until a selection is choosen outside (1-4). whenever a selection is chosen outside of those parameters, the user needs to be notified of an invalid selection and prompted to re enter a choice.

#include <iostream> //input and output library
#include <cstdlib>
#include <string>


   using namespace std;

	int main ()
   
   {
	  int years;
	  double yearly_gain;
	  double tax;
	  double inflation;
	  double interest;
	  double principle;
	  double apr;
	  int sel;

	
				   						
					
	  cout << "Welcome to Price Moneybags Financial Wizard!!" << endl;
	  cout << " " <<endl;
	  cout << "1)"<<" "<< "Price Moneybags Mutual Fund" << endl;
	  cout << "2)"<<" "<< "Price Moneybags Tax-Free Bonds" << endl;
	  cout << "3)"<<" "<< "Price Moneybags Bury in Ground Option" << endl;
	  cout << "4)"<<" "<< "Exit the program" << endl; 
   // This is just the begining of the program that shows the user the investment choices they have to choose
   // from. 
	  cout << " " << endl;
	  cout << " Please input a selection between (1-4): " << endl;
	   cin>> sel;
	 
   	  
		/* These are switch statements, I use these to have the compilier take in the user input
		 and depending on what selection the customer uses, it will compute their investment based on their
		 choice.*/
		
	   
		switch (sel)
		 {
		 case 1:
			{
			   cout << "How Much Money will you invest in our Mutual Fund?" << endl;
			   cin>> principle;
			   cout << "How many years will you be investing with us?" <<endl;
			   cin>> years;
			   cout << "What is the average tax rate (in decimal):" << endl;
			   cin>> tax;
			   cout << "What is the average rate of inflation:" << endl;
			   cin>> inflation;  
			   
			   double total_return = principle * inflation * apr * tax;
			   yearly_gain = (total_return / years); 
			   
			   cout << "We Predict that your investment will be worth: " << total_return << " in Todays dollars." << endl;
			   cout << "That is an average annual gain of: " << yearly_gain << ". " << endl;
			   break; 
			   }
		 case 2:
		   {
			   cout << "How Much Money will you invest in our Tax Free Bonds?" << endl;
			   cin>> principle;
			   cout << "How many years will you be investing with us?" <<endl;
			   cin>> years;
			   cout << "This Investment option is free of Tazes" << endl;
			   cout << " " << endl;
			   cout << "What is the average rate of inflation:" << endl;
			   cin>> inflation;  
			   break;
			   }
		 case 3:
			{
			   cout << "How much money will you bury under ground?" << endl;
			   cin>> principle;
			   cout << "How many years will it remain buried?" <<endl;
			   cin>> years;
			   cout << "This Option is Tax Free" << endl;
			   cout <<" " << endl;
			   cout << "What is the average rate of inflation:" << endl;
			   cin>> inflation;  
			  break;
			  }
		 case 4:
		   {
			   cout<< "Thank you for using Price Money Bags investment Software!" << endl;
			   break;
			   }
			default :
					{
					cout << "Invalid Selection!"<< "Please select an investment strategy (1-4):" << endl;		 
					cin>> sel;
					}
					}
						  return 0;
						  }


*edit: Please use code tags in the future. Thanks! :code:

This post has been edited by Martyr2: 08 February 2008 - 11:57 AM


Is This A Good Question/Topic? 0
  • +

Replies To: C++ For, DO while loop questions

#2 manzoor  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 13
  • View blog
  • Posts: 468
  • Joined: 07-August 07

Re: C++ For, DO while loop questions

Posted 08 February 2008 - 12:18 PM

What errors are you getting ?
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,126
  • Joined: 18-April 07

Re: C++ For, DO while loop questions

Posted 08 February 2008 - 12:22 PM

Hi Rstewart79,

You can insert a validation loop when you go to collect the information and validate whether or not it it is in your range of accepted values. This can be done similar to this...

cout << "Welcome to Price Moneybags Financial Wizard!!" << endl;
cout << " " <<endl;
cout << "1)"<<" "<< "Price Moneybags Mutual Fund" << endl;
cout << "2)"<<" "<< "Price Moneybags Tax-Free Bonds" << endl;
cout << "3)"<<" "<< "Price Moneybags Bury in Ground Option" << endl;
cout << "4)"<<" "<< "Exit the program" << endl;
// This is just the begining of the program that shows the user the investment choices they have to choose
// from.

cout << " " << endl;
cout << "Please input a selection between (1-4): " << endl;

// Read in the value into sel as part of the loop. Validate it to also make sure it is not less than 1 or greater than 4.
// If invalid, clear the error flag of cin, ignore what was typed into the buffer (up to 100 chars or until newline is met)
// Put out the prompt again and wait for their next choice.
while (!(std::cin >> sel) || (sel < 1) || (sel > 4)) {
	 cin.clear();
	 cin.ignore(100, '\n');
	 cout << "Please input a selection between (1-4): " << endl;
}



Once they enter a valid choice then it will come out of the loop and be ready for your switch statement.

Enjoy!

"At DIC we be data collection code ninjas!" :snap:
Was This Post Helpful? 0
  • +
  • -

#4 Rstewart79  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-February 08

Re: C++ For, DO while loop questions

Posted 08 February 2008 - 12:31 PM

ok I did this, however when I run the program I can only error check once meaning, it will say its invalid once and then prompt the user again but after I run one switch case and hit another invalid option it does not prompt me for a valid input, it just exits

while (!(std::cin >> sel) || (sel < 1) || (sel > 4))
{
cin.clear();
cin.ignore(100, '\n');
cout << "Please input a selection between (1-4): " << endl;
}




/* These are switch statements, I use these to have the compilier take in the user input
and depending on what selection the customer uses, it will compute their investment based on their
choice.*/


switch (sel)
{
case 1:
{
cout << "How Much Money will you invest in our Mutual Fund?" << endl;
cin>> principle;
cout << "How many years will you be investing with us?" <<endl;
cin>> years;
cout << "What is the average tax rate (in decimal):" << endl;
cin>> tax;
cout << "What is the average rate of inflation:" << endl;
cin>> inflation;

double total_return = principle * inflation * apr * tax;
yearly_gain = (total_return / years);

cout << "We Predict that your investment will be worth: " << total_return << " in Todays dollars." << endl;
cout << "That is an average annual gain of: " << yearly_gain << ". " << endl;
break;
}
case 2:
{
cout << "How Much Money will you invest in our Tax Free Bonds?" << endl;
cin>> principle;
cout << "How many years will you be investing with us?" <<endl;
cin>> years;
cout << "This Investment option is free of Tazes" << endl;
cout << " " << endl;
cout << "What is the average rate of inflation:" << endl;
cin>> inflation;
break;
}
case 3:
{
cout << "How much money will you bury under ground?" << endl;
cin>> principle;
cout << "How many years will it remain buried?" <<endl;
cin>> years;
cout << "This Option is Tax Free" << endl;
cout <<" " << endl;
cout << "What is the average rate of inflation:" << endl;
cin>> inflation;
break;
}
case 4:
{
cout<< "Thank you for using Price Money Bags investment Software!" << endl;
break;
}

}} return 0;
}
Was This Post Helpful? 0
  • +
  • -

#5 Rstewart79  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-February 08

Re: C++ For, DO while loop questions

Posted 08 February 2008 - 12:37 PM

View Postmanzoor, on 8 Feb, 2008 - 12:18 PM, said:

What errors are you getting ?



no errors just wont work correctly.. I would like to write a loop where the program continues to run until an invalid selection is input (ie a number outside 1-4) and then once 4 is chosen program exits
Was This Post Helpful? 0
  • +
  • -

#6 Coal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 11-February 08

Re: C++ For, DO while loop questions

Posted 11 February 2008 - 03:19 PM

I'm not sure how long these topic's lives are, but I'll post a solution that works for me if you haven't already found one yourself.

From what I understand, you are just looking for a way for the program to loop until selection 4 is chosen, with invalid choices being included in this process. After looking at your code, one way to do this is using a do/while loop with a bool variable telling the loop to stop and then exiting the program. Here's your code with my additions to it, which makes the program run for me the way I think you want it to.

#include <iostream> //input and output library
#include <cstdlib>
#include <string>


   using namespace std;

	int main ()

   {
	  int years;
	  double yearly_gain;
	  double tax;
	  double inflation;
	  double interest;
	  double principle;
	  double apr;
	  int sel;
	  bool exit = false;




	  cout << "Welcome to Price Moneybags Financial Wizard!!" << endl;
	  cout << " " <<endl;
	  cout << "1)"<<" "<< "Price Moneybags Mutual Fund" << endl;
	  cout << "2)"<<" "<< "Price Moneybags Tax-Free Bonds" << endl;
	  cout << "3)"<<" "<< "Price Moneybags Bury in Ground Option" << endl;
	  cout << "4)"<<" "<< "Exit the program" << endl;
   // This is just the begining of the program that shows the user the investment choices they have to choose
   // from.
	  cout << " " << endl;

	  do
	  {

	  cout << " Please input a selection between (1-4): " << endl;
	   cin>> sel;


		/* These are switch statements, I use these to have the compilier take in the user input
		 and depending on what selection the customer uses, it will compute their investment based on their
		 choice.*/

		switch (sel)
		 {
		 case 1:
			{
			   cout << "How Much Money will you invest in our Mutual Fund?" << endl;
			   cin>> principle;
			   cout << "How many years will you be investing with us?" <<endl;
			   cin>> years;
			   cout << "What is the average tax rate (in decimal):" << endl;
			   cin>> tax;
			   cout << "What is the average rate of inflation:" << endl;
			   cin>> inflation;

			   double total_return = principle * inflation * apr * tax;
			   yearly_gain = (total_return / years);

			   cout << "We Predict that your investment will be worth: " << total_return << " in Todays dollars." << endl;
			   cout << "That is an average annual gain of: " << yearly_gain << ". " << endl;
			   break;
			   }
		 case 2:
		   {
			   cout << "How Much Money will you invest in our Tax Free Bonds?" << endl;
			   cin>> principle;
			   cout << "How many years will you be investing with us?" <<endl;
			   cin>> years;
			   cout << "This Investment option is free of Tazes" << endl;
			   cout << " " << endl;
			   cout << "What is the average rate of inflation:" << endl;
			   cin>> inflation;
			   break;
			   }
		 case 3:
			{
			   cout << "How much money will you bury under ground?" << endl;
			   cin>> principle;
			   cout << "How many years will it remain buried?" <<endl;
			   cin>> years;
			   cout << "This Option is Tax Free" << endl;
			   cout <<" " << endl;
			   cout << "What is the average rate of inflation:" << endl;
			   cin>> inflation;
			  break;
			  }
		 case 4:
		   {
			   cout<< "Thank you for using Price Money Bags investment Software!" << endl;
			   exit = true;
			   break;
			   }
			default :
					{
					cout << "Invalid Selection!"<< endl;
					}
					}
	  } while (exit == false);

						  return 0;
						  }




I've added "bool exit = false;" to your variable declarations and initialized it to false so the do/while loop knows what condition it should be looking for to continue looping.

I added the do loop right before you prompt for the user's selection so that part will always be displayed when the loop runs and added the while command after the end of your switch. The variable exit goes before the break in selection four, changing its value to true and telling the loop to exit.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#7 vinodj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-February 08

Re: C++ For, DO while loop questions

Posted 12 February 2008 - 03:14 AM

View PostRstewart79, on 8 Feb, 2008 - 11:49 AM, said:

Need to have this run until a selection is choosen outside (1-4). whenever a selection is chosen outside of those parameters, the user needs to be notified of an invalid selection and prompted to re enter a choice.

#include <iostream> //input and output library
#include <cstdlib>
#include <string>


   using namespace std;

	int main ()
   
   {
	  int years;
	  double yearly_gain;
	  double tax;
	  double inflation;
	  double interest;
	  double principle;
	  double apr;
	  int sel;

	
				   						
					
	  cout << "Welcome to Price Moneybags Financial Wizard!!" << endl;
	  cout << " " <<endl;
	  cout << "1)"<<" "<< "Price Moneybags Mutual Fund" << endl;
	  cout << "2)"<<" "<< "Price Moneybags Tax-Free Bonds" << endl;
	  cout << "3)"<<" "<< "Price Moneybags Bury in Ground Option" << endl;
	  cout << "4)"<<" "<< "Exit the program" << endl; 
   // This is just the begining of the program that shows the user the investment choices they have to choose
   // from. 
	  cout << " " << endl;
	  cout << " Please input a selection between (1-4): " << endl;
	   cin>> sel;
	 
   	  
		/* These are switch statements, I use these to have the compilier take in the user input
		 and depending on what selection the customer uses, it will compute their investment based on their
		 choice.*/
		
	   
		switch (sel)
		 {
		 case 1:
			{
			   cout << "How Much Money will you invest in our Mutual Fund?" << endl;
			   cin>> principle;
			   cout << "How many years will you be investing with us?" <<endl;
			   cin>> years;
			   cout << "What is the average tax rate (in decimal):" << endl;
			   cin>> tax;
			   cout << "What is the average rate of inflation:" << endl;
			   cin>> inflation;  
			   
			   double total_return = principle * inflation * apr * tax;
			   yearly_gain = (total_return / years); 
			   
			   cout << "We Predict that your investment will be worth: " << total_return << " in Todays dollars." << endl;
			   cout << "That is an average annual gain of: " << yearly_gain << ". " << endl;
			   break; 
			   }
		 case 2:
		   {
			   cout << "How Much Money will you invest in our Tax Free Bonds?" << endl;
			   cin>> principle;
			   cout << "How many years will you be investing with us?" <<endl;
			   cin>> years;
			   cout << "This Investment option is free of Tazes" << endl;
			   cout << " " << endl;
			   cout << "What is the average rate of inflation:" << endl;
			   cin>> inflation;  
			   break;
			   }
		 case 3:
			{
			   cout << "How much money will you bury under ground?" << endl;
			   cin>> principle;
			   cout << "How many years will it remain buried?" <<endl;
			   cin>> years;
			   cout << "This Option is Tax Free" << endl;
			   cout <<" " << endl;
			   cout << "What is the average rate of inflation:" << endl;
			   cin>> inflation;  
			  break;
			  }
		 case 4:
		   {
			   cout<< "Thank you for using Price Money Bags investment Software!" << endl;
			   break;
			   }
			default :
					{
					cout << "Invalid Selection!"<< "Please select an investment strategy (1-4):" << endl;		 
					cin>> sel;
					}
					}
						  return 0;
						  }


*edit: Please use code tags in the future. Thanks! :code:




#include <iostream> //input and output library
#include <cstdlib>
#include <string>


using namespace std;

int main ()

{
int years;
double yearly_gain;
double tax;
double inflation;
double interest;
double principle;
double apr;
int sel;




cout << "Welcome to Price Moneybags Financial Wizard!!" << endl;
cout << " " <<endl;
cout << "1)"<<" "<< "Price Moneybags Mutual Fund" << endl;
cout << "2)"<<" "<< "Price Moneybags Tax-Free Bonds" << endl;
cout << "3)"<<" "<< "Price Moneybags Bury in Ground Option" << endl;
cout << "4)"<<" "<< "Exit the program" << endl;
// This is just the begining of the program that shows the user the investment choices they have to choose
// from.
cout << " " << endl;

do
{
cout << " Please input a selection between (1-4): " << endl;
cin>> sel;


/* These are switch statements, I use these to have the compilier take in the user input
and depending on what selection the customer uses, it will compute their investment based on their
choice.*/

switch (sel)
{
case 1:
{
cout << "How Much Money will you invest in our Mutual Fund?" << endl;
cin>> principle;
cout << "How many years will you be investing with us?" <<endl;
cin>> years;
cout << "What is the average tax rate (in decimal):" << endl;
cin>> tax;
cout << "What is the average rate of inflation:" << endl;
cin>> inflation;

double total_return = principle * inflation * apr * tax;
yearly_gain = (total_return / years);

cout << "We Predict that your investment will be worth: " << total_return << " in Todays dollars." << endl;
cout << "That is an average annual gain of: " << yearly_gain << ". " << endl;
break;
}
case 2:
{
cout << "How Much Money will you invest in our Tax Free Bonds?" << endl;
cin>> principle;
cout << "How many years will you be investing with us?" <<endl;
cin>> years;
cout << "This Investment option is free of Tazes" << endl;
cout << " " << endl;
cout << "What is the average rate of inflation:" << endl;
cin>> inflation;
break;
}
case 3:
{
cout << "How much money will you bury under ground?" << endl;
cin>> principle;
cout << "How many years will it remain buried?" <<endl;
cin>> years;
cout << "This Option is Tax Free" << endl;
cout <<" " << endl;
cout << "What is the average rate of inflation:" << endl;
cin>> inflation;
break;
}
case 4:
{
cout<< "Thank you for using Price Money Bags investment Software!" << endl;
getchar();
exit(0);
break;
}
default :
{
cout << "Invalid Selection!"<< endl;
}
}
}while(1);
return 0;
}
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1