Welcome to Dream.In.Code
Become a C++ Expert!

Join 136,940 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,862 people online right now. Registration is fast and FREE... Join Now!




How to repeat a program after user input?

 
Reply to this topicStart new topic

How to repeat a program after user input?, I'd like a user to choose whether to quit or run again

confuzius
15 Mar, 2008 - 01:32 AM
Post #1

New D.I.C Head
*

Joined: 9 Mar, 2008
Posts: 9


My Contributions
Hi

I'd like to find out how to let a user repeat a console program or terminate it, rather than have them reopen the program every time. I've tried a bit with do ... while ... but can' seem to figure it out. Plus there will have to be a more elegant way of doing it than that, I think.

Here's the code for the program, but this is more of an open question than anything specifically for one program.

CODE
#include <cstdlib>
#include <iostream>
#include <math.h>

using namespace std;

int main(int argc, char *argv[])

{
    
int ap;
cout << "Do you want to work out an area problem or a perimeter problem? If area, type 1, if perimeter, type 2." << endl;
cin >> (ap);

if (ap==1){

     cout << " What kind of shape are you looking at? For a square type 1, for a rectangle type 2, for a triangle type 3 and for a circle type 4."<< endl;
     int shapetype;
     cin >> shapetype;
        if (shapetype==1){
        cout << "What is the length of any side?" << endl;
        float anyside;
        cin >> anyside;
        float area;
        area = (anyside) * (anyside);
        cout << "The area is " << area << " square units" << endl;
        }
     else if (shapetype == 2) {
        cout << "What is the length?" << endl;
        float length;
        cin >> length;
        cout << "What is the width?" << endl;
        float width;
        cin >> width;
        float area;
        area = length * width;
        cout << "The area is " <<  area << " square units" << endl;
        }
     else if (shapetype == 3) {
        cout << "What is the base length?" << endl;
        float base;
        cin >> base;
        cout << "What is the height from base to apex?" << endl;
        float height;
        cin >> height;
        float area;
        area = ((0.5) * base) * height;
        cout << " The area is " << area << " square units." << endl;
        }

     else if (shapetype == 4) {
        cout << "What is the length of the radius?" << endl;
        float radius;
        float pi =3.14159265358979323846264338327950288419716939937510;
        cin >> radius;
        float area;
        area = (pi) * (radius* radius);
        cout << " The area is " << area << " square units." << endl;
        }
    }
  else if (ap == 2) {
  cout << "What kind of shape are you looking at? For a square type 1, for a rectangle type 2, for a triangle type 3 and for a circle type 4. "<< endl;
  int shapetype;
  cin >> shapetype;
    if (shapetype == 1){
        cout << "What is the length of any one side?"<< endl;
        float length;
        cin >> length;
        float perimeter;
        perimeter = length * 4;
        cout << "The perimeter is " << perimeter << " units." << endl;
        }
    else if (shapetype == 2) {
        cout << "What is the length?" << endl;
        float length;
        cin >> length;
        cout << "What is the width?" << endl;
        float width;
        cin >> width;
        float perimeter;
        perimeter = (length * 2) + (width * 2);
        cout << "The perimeter is " << perimeter << " units" << endl;
        }
    else if (shapetype == 3) {
        cout << " What is the length of side 1?" << endl;
        float side1;
        cin >> side1;
        cout << " What is the length of side 2?" << endl;
        float side2;
        cin >> side2;
        cout << " What is the length of side 3?" << endl;
        float side3;
        cin >> side3;
        float perimeter;    
        perimeter = side1 + side2 + side3;
        cout << "The perimeter is " << perimeter << " units" << endl;
        }
    else if (shapetype == 4) {
        cout << "What is the radius?" << endl;
        float radius;
        cin >> radius;
        float diameter;
        diameter = 2 * radius;
        float pi;
        pi = 3.14159265358979323846264338327950288419716939937510;
        float circ;
        circ = pi * diameter;
        cout  << "The circumference or perimeter is " << circ << " units";
        }
    }
      
    system("PAUSE");
    return EXIT_SUCCESS;
}

User is offlineProfile CardPM
+Quote Post

Moezzie
RE: How To Repeat A Program After User Input?
15 Mar, 2008 - 03:15 AM
Post #2

New D.I.C Head
*

Joined: 25 Nov, 2007
Posts: 44


My Contributions
The easiest way would probably be to put your code into a loop and put an if-statement on the end to determine if the user wants to rerun the program. If he doesnt the code inside the if-statement will break the loop and the program will exit.

Kind of like this:
CODE
int main()
{
    int exit;
    while(1==1)
    {
        cout << "Hello"<<endl;
        cout << "Hello"<<endl;
        cout << "Hello"<<endl;
        cout << "Would you like to exit?"<<endl;
        cin >> exit;
        
        if(exit == 1)
        {
            break;
        }                      
    }


return 0;
}

This asks the user if he wants to quit. If he enters 1, the app will quit. Does he enter anything else than 1 it will continue because 1 is always equal to 1.
Good luck mate
User is offlineProfile CardPM
+Quote Post

confuzius
RE: How To Repeat A Program After User Input?
15 Mar, 2008 - 08:23 PM
Post #3

New D.I.C Head
*

Joined: 9 Mar, 2008
Posts: 9


My Contributions
Thanks- I'll give it a whirl, see what happens. But that seems to be exactly what I was trying to do, just without the props to get it right.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 10:55PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month