473,386 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Need to add a loop to exit or repeat program

Hi all,

I have written a C ++ program and now need to add a way to make it exit on command, or start over from the beginning. I have very little programming knowledge; Here is my code:
Thanks in advance for any help.

Expand|Select|Wrap|Line Numbers
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////                                                                                      //                                                                
  2. //Program Discription, Show name and number of Stock for purchase                                //
  3. //Calculate 100 shares by 63.25 and add .02 cent commission per share.                           //
  4. //Calculate Commission at 39.95 if Subtotal at or over 39.95                                     //
  5. ///////////////////////////////////////////////////////////////////////////////////////////////////                                        
  6. //Version Control:                                                                               // 
  7. //Beta ver set up main Algorythm and function,initalize variables                                //
  8. //perform calculations:                                                                          //
  9. //a=1-100 shares, b=cost per share, c=Subtotal(a*b),d=2cent commission rate                      //
  10. //e=Commission rate* number of shares,f=39.95,g=grandTotal(Commission rate Due + Subtotal Sales) //
  11. //Version 2:                                                                                     //
  12. //1/27/07/                                                                                       //
  13. //Modify code to request and accept user input for number of shares wanted, price of each share  //
  14. //and Grand Total of final Transaction. Add user loop-back structer for new data.                //
  15. ///////////////////////////////////////////////////////////////////////////////////////////////////
  16. #include <iostream>
  17. #include <math.h>
  18. #include <iomanip>
  19. using namespace std;
  20. int main()
  21. {
  22.  
  23.     int a=0;
  24.     double b=0,c=0,d=0.02,e=0,f=0;
  25.     cout<<fixed<<showpoint;
  26.  
  27.           cout<<"\n Market Shares For Sale! \n"  <<endl;
  28.  
  29.  
  30.         // user enters number of shares to buy
  31.  
  32.         cout<<"\t Cocoa-Cola Stock. Select the number of shares  (1-1000):$  \n"<<flush;
  33.                  cin>>a;
  34.                    if(a<1 ||a>1000){
  35.                       cout<<"\t Not a valid Entry. Must be 1 to 1000.\n"<<flush<<endl;
  36.                       }
  37.             cout<<endl;
  38.         //user enters cost per share
  39.             cout<<"\t What is the Price Per Share:$  \n"<<flush;
  40.                   cin>>b;
  41.                   cout<<endl;
  42.                   cout<<endl;
  43.                //Find amount of Commission due
  44.  
  45.                  cout<<"\t This is the amount of Commission Due at 2cent a share:$  \n"<<(a*d)<<endl;
  46.                    e=a*d;
  47.                   if (e>39.95){
  48.                         cout<<"\t Commission plus Sub-Total=Grand Total.Please Pay this Amount:$ \n"<<(c+e)<<f;
  49.                         }   
  50.                   else if(e<39.95){        //calculate the price of shares by the number of shares 
  51.                      cout<<"\t This is the Total of your purchase=:  \n"<<(a*b)<<c<<endl;
  52.                   }
  53.  
  54.  
  55. return 0;
  56.  
  57. }
Jan 28 '07 #1
8 29103
Banfa
9,065 Expert Mod 8TB
call/look up the function

void exit(int);
Jan 29 '07 #2
Hi Benfa,


First thanks for the reply.
After some looking/research I'm still not sure this is the answer to my problem.

I found this example and think I can modify it to suit my needs.

int main()

{

char selection = 'y';



while (tolower(selection) == 'y') // loop while selection is 'y'

{

cout << "Do you want to continue (y/n):\n";

cin >> selection;

if (cin.fail() || !(tolower(selection) == 'y' || tolower(selection) == 'n'))

{

cout << "You entered an invalid input, program will restart.\n";

selection = 'y'; //make sure we continue the loop

continue; //jump back to the start of the while loop

}

cin.clear(); //clear the error flag

cin.ignore(100,'\n');// toss out the corrupted input buffer, up to 100 chars



if (tolower(selection) == 'n')

return 0;

cout << "here we add some code to do some stuff\n";



}


What I still need to know is: do I place the modification at the end of my code or before my code? Or does it even matter?

Thank you so much for the help. I needed a wise pair of eyes!
Jan 30 '07 #3
Hi Benfa,


First thanks for the reply.
After some looking/research I'm still not sure this is the answer to my problem.

I found this example and think I can modify it to suit my needs.

int main()

{

char selection = 'y';



while (tolower(selection) == 'y') // loop while selection is 'y'

{

cout << "Do you want to continue (y/n):\n";

cin >> selection;

if (cin.fail() || !(tolower(selection) == 'y' || tolower(selection) == 'n'))

{

cout << "You entered an invalid input, program will restart.\n";

selection = 'y'; //make sure we continue the loop

continue; //jump back to the start of the while loop

}

cin.clear(); //clear the error flag

cin.ignore(100,'\n');// toss out the corrupted input buffer, up to 100 chars



if (tolower(selection) == 'n')

return 0;

cout << "here we add some code to do some stuff\n";



}


What I still need to know is: do I place the modification at the end of my code or before my code? Or does it even matter?

Thank you so much for the help. I needed a wise pair of eyes!



Benfa,

my program runs through the code once and when y(for Yes) is entered the program will repeat from the beginning, but only once more. How do I get it to
repeat until the condition q(for Quit) is met?
Feb 2 '07 #4
RedSon
5,000 Expert 4TB
Benfa,

my program runs through the code once and when y(for Yes) is entered the program will repeat from the beginning, but only once more. How do I get it to
repeat until the condition q(for Quit) is met?
Look closely at the flow of the program after it enters into the loop. Why would entering in 'y' then run your program one more time and quit? What is your code doing after it takes the input from the user?
Feb 2 '07 #5
Hi Red S.
I am not sure what you mean. If I pick up the block of code and place it in a second if statement it will run another time. What I want to know is there a way to keep the loop going without making some many blocks of the same code?
Feb 3 '07 #6
Banfa
9,065 Expert Mod 8TB
I have to say I see no problem with your last posted code, it requests input, makes sure that the input is valid then if the input is 'n' it returns else it runs the following code block.
Feb 3 '07 #7
RedSon
5,000 Expert 4TB
Hi Red S.
I am not sure what you mean. If I pick up the block of code and place it in a second if statement it will run another time. What I want to know is there a way to keep the loop going without making some many blocks of the same code?
I apologize, I thought you were trying to get it to stop after you asked the user for input. They way you have it now if the user says no the program will run one more time then the exit condition for the while loop is met and the code exits. I see now that you want it to continue indefinetly until the user says something. So you want your code to look like this.
Expand|Select|Wrap|Line Numbers
  1. bool done = false;
  2. while (!done)
  3. {
  4.  
  5. ...your code goes here...
  6.  
  7.   cout << "Do you want to quit?";
  8.   cin >> selection;
  9.  
  10.   if (selection == yes)
  11.   {
  12.      done = true;
  13.   }
  14. }
  15.  
Feb 5 '07 #8
Thank you for getting back to me and seeing what the real problem was.
I was able to fix the problem with this.



I apologize, I thought you were trying to get it to stop after you asked the user for input. They way you have it now if the user says no the program will run one more time then the exit condition for the while loop is met and the code exits. I see now that you want it to continue indefinetly until the user says something. So you want your code to look like this.
Expand|Select|Wrap|Line Numbers
  1. bool done = false;
  2. while (!done)
  3. {
  4.  
  5. ...your code goes here...
  6.  
  7.   cout << "Do you want to quit?";
  8.   cin >> selection;
  9.  
  10.   if (selection == yes)
  11.   {
  12.      done = true;
  13.   }
  14. }
  15.  
Feb 8 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
8
by: Microsoft | last post by:
I have the following loop the length contains somewhere around 1000+ items: For elemIndex = 0 To len - 1 elem = CType(doc.all.item(elemIndex), mshtml.IHTMLElement) If elem.tagName = "A" Then If...
4
by: Arnold | last post by:
Hi there, Here's the situation--there is a text field in a form in which students will key in data. On the keypress event, I'd like for different sounds to be played for each character typed,...
12
by: asif929 | last post by:
I am trying to write a program which creates four triangles. The program begins with prompting a user " Enter the size of triangles", number from 1 to N is the size of four triangles For Example if...
1
by: peterggmss | last post by:
This is a slot machine game, 2 forms. One is the actual game (frmMachine) and the other is in the background and randomizes the images shown on frmMachine. I need to make frmMachine wait for...
0
by: south622 | last post by:
I'm taking a beginning Java course and I'm stuck in week eight of a nine week course. If anyone could help me I would greatly appreciate it. This assignment was due yesterday and each day I go past...
14
namcintosh
by: namcintosh | last post by:
Hello, everyone. Well, let me cut to the chase and explain my problem. I am trying to devise a menu plan that uses the if/else if and the while loop. The program calculates the user's weight...
18
by: Vijaykumar Dave | last post by:
I have a program for base X power N as under. The problem is that when the range specified in loop is given it works well, but when any character is pressed, it goes to infinite loop. Second...
3
by: 100grand | last post by:
Modify the Inventory Program to use a GUI. The GUI should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.