Hey everyone, I'm just now learning c++ and was wondering if there was a way to loop this program so that I don't have to copy and paste it. I got it to work, but I was just curious if there was another way to do it. it just does simple math really. Thanks in advance everyone! oh, and it runs in the command prompt window, because i can't do anything else yet haha. I have all of the semi-colons when i declared the variables because it wouldn't compile without them for some reason. Sorry it's so long.

    /*
    Pick a problem and solve
    */


    #include <iostream>
    using namespace std;


    int main()
    {


    int perimiter;
    int area;
    double addition;
    double multiplication;
    double subtraction;
    double division;
    double a;
    double b;
    double c;
    double d;
    double e;
    double f;
    double g;
    double h;
    double i;
    double j;
    double k;
    double l;
    int; 2;
    double result;
    int x;
    int; 0;
    int; 3;
    int; 4;
    int; 5;
    int; 1;
    int; 6;
    int; 7;



    cout << "Hello, please pick a type of problem from the list. Please use the number of n each: ";
    cout << "1. perimiter, 2. area, 3. addition, 4. multiplication, 5. subtraction,n 6. division, 7. Quit program. ";
    cin >> x ;



    if(x == 1) cout << "Enter the length: ";
    if(x==1) cin >> a;


    if(x==1)cout << "enter the width: ";
    if(x==1)cin >> b;


    if(x==1) cout << "The perimiter is: ";
    if(x==1) cout << a+a+b+b;


    if(x == 2)
    cout << "enter the length: ";
    if(x == 2) cin >> c;


    if(x == 2) cout << "Enter the width: ";
    if(x == 2) cin >> d;


    if(x == 2)cout << "The area is: ";
    if(x == 2)cout << c*d;


    if(x == 3) cout << "Enter the first number: ";
    if(x == 3) cin >> e;
    if(x == 3) cout << "Enter the second number: ";
    if(x == 3) cin >> f;
    if(x == 3) cout << "The solution is: ";
    if(x == 3) cout << e+f;


    if(x == 4) cout << "Enter the first number: ";
    if(x == 4) cin >> g;
    if(x == 4) cout << "Enter the second number: ";
    if(x == 4) cin >> h;
    if(x == 4) cout << "The solution is: ";
    if(x == 4) cout << g*h;


    if(x == 5) cout << "Enter the first number: ";
    if(x == 5) cin >> i;
    if(x == 5) cout << "Enter the second number: ";
    if(x == 5) cin >> j;
    if(x == 5) cout << "The solution is: ";
    if(x == 5) cout << i-j;


    if(x == 6) cout << "Enter the top/first/outer number: ";
    if(x == 6) cin >> k;
    if(x == 6) cout << "Enter the bottom/second/inner number: ";
    if(x == 6) cin >> l;
    if(x == 6) cout << "The answer is: ";
    if(x == 6) cout << k/l;



    while(x<7){cout << "n1. perimiter, 2. area, 3. addition, 4. multiplication, 5. subtraction, 6. division, 7. Quit program. ";
    cin >> x ;



    if(x == 1) cout << "Enter the length: ";
    if(x==1) cin >> a;


    if(x==1)cout << "enter the width: ";
    if(x==1)cin >> b;


    if(x==1) cout << "The perimiter is: ";
    if(x==1) cout << a+a+b+b;


    if(x == 2)
    cout << "enter the length: ";
    if(x == 2) cin >> c;


    if(x == 2) cout << "Enter the width: ";
    if(x == 2) cin >> d;


    if(x == 2)cout << "The area is: ";
    if(x == 2)cout << c*d;


    if(x == 3) cout << "Enter the first number: ";
    if(x == 3) cin >> e;
    if(x == 3) cout << "Enter the second number: ";
    if(x == 3) cin >> f;
    if(x == 3) cout << "The solution is: ";
    if(x == 3) cout << e+f;


    if(x == 4) cout << "Enter the first number: ";
    if(x == 4) cin >> g;
    if(x == 4) cout << "Enter the second number: ";
    if(x == 4) cin >> h;
    if(x == 4) cout << "The solution is: ";
    if(x == 4) cout << g*h;


    if(x == 5) cout << "Enter the first number: ";
    if(x == 5) cin >> i;
    if(x == 5) cout << "Enter the second number: ";
    if(x == 5) cin >> j;
    if(x == 5) cout << "The solution is: ";
    if(x == 5) cout << i-j;


    if(x == 6) cout << "Enter the top/first/outer number: ";
    if(x == 6) cin >> k;
    if(x == 6) cout << "Enter the bottom/second/inner number: ";
    if(x == 6) cin >> l;
    if(x == 6) cout << "The answer is: ";
    if(x == 6) cout << k/l;}




if(x == 7) return 0;
}

Edited 3 Years Ago by happygeek: fixed formatting

Here is your program written better. You could have used switch statements, but I wasn't sure if you knew them.

Use code-tags.

/*
Pick a problem and solve
*/

#include <iostream>
using namespace std;

int main()
{

    int x;
    float a,b;

    cout << "Hello, please pick a type of problem from the list. Please use the number of  each: \n\n"; 
    cout << "1. perimiter\n";
    cout <<"2. area\n";
    cout <<"3. addition\n";
    cout <<"4. multiplication\n";
    cout <<"5. subtraction\n";
    cout <<"6. division\n";
    cout <<"7. Quit program. \n";
    cin >> x ;

    //if x is less tha 1 or greater than 7 then throw error
    while(x<1 || x >7)
    {
        cout << "Hello, please pick a type of problem from the list. Please use the number of each: \n\n"; 
        cout << "1. perimiter\n";
        cout <<"2. area\n";
        cout <<"3. addition\n";
        cout <<"4. multiplication\n";
        cout <<"5. subtraction\n";
        cout <<"6. division\n";
        cout <<"7. Quit program. \n";
        cin >> x ;  
    }

    if(x == 1) 
    {

        cout << "Enter the length: "; 
        cin >> a;
        cout<<"\n\n";
        cout << "enter the width: ";
        cin >> b; 
        cout<<"\n\n";
        cout << "The perimiter is: ";
        cout << 2*(a+b);
    }

    else if( x == 2)
    {
        cout << "enter the length: ";
        cin >> a;
        cout<<"\n\n";
        cout << "Enter the width: ";
        cin >> b;
        cout<<"\n\n";
        cout << "The area is: ";
        cout << a*b;
    }


    else if(x == 3)
    {
        cout << "Enter the first number: ";
        cin >> a;
        cout<<"\n\n";
        cout << "Enter the second number: ";
        cin >> b;
        cout<<"\n\n";
        cout << "The solution is: ";
        cout << a+b;
    }
    else if(x == 4)
    {
        cout << "Enter the first number: ";
        cin >> a;
        cout<<"\n\n";
        cout << "Enter the second number: ";
        cin >> b;
        cout<<"\n\n";
        cout << "The solution is: "; 
        cout << a*b;
    }

    else if(x == 5) 
    {
        cout << "Enter the first number: ";
        cin >> a; 
        cout<<"\n\n";
        cout << "Enter the second number: ";
        cin >> b;
        cout<<"\n\n";
        cout << "The solution is: ";
        cout << a-b;
    }
    else if(x == 6) 
    {
        cout << "Enter the top/first/outer number: ";
        cin >> a;
        cout<<"\n\n";
        cout << "Enter the bottom/second/inner number: ";
        cin >> b;
        cout<<"\n\n";
        cout << "The answer is: ";
        cout << a/b;
    }


  return 0;
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Here is your program written better. You could have used switch statements, but I wasn't sure if you knew them.

Basing the code off of what firstperson has above, if you want to loop the program, you could use a while loop like so:

#include <iostream>
using namespace std;

int main()
{
     int x=0;
     float a,b;
 	
    while(x != 7)
    {
	cout << "Hello, please pick a type of problem from the list. Please use the number of  each: \n\n"; 
	cout << "1. perimiter\n";
	cout <<"2. area\n";
	cout <<"3. addition\n";
	cout <<"4. multiplication\n";
	cout <<"5. subtraction\n";
	cout <<"6. division\n";
	cout <<"7. Quit program. \n";
	cin >> x ;
	
	//if x is less tha 1 or greater than 7 then throw error
	while(x<1 || x >7)
	{
		cout << "Hello, please pick a type of problem from the list. Please use the number of each: \n\n"; 
		cout << "1. perimiter\n";
		cout <<"2. area\n";
		cout <<"3. addition\n";
		cout <<"4. multiplication\n";
		cout <<"5. subtraction\n";
		cout <<"6. division\n";
		cout <<"7. Quit program. \n";
		cin >> x ;	
	}

	if(x == 1) 
	{
		
		cout << "Enter the length: ";	
		cin >> a;
		cout<<"\n\n";
		cout << "enter the width: ";
		cin >> b; 
		cout<<"\n\n";
		cout << "The perimiter is: ";
		cout << 2*(a+b);
	}

	else if( x == 2)
	{
		cout << "enter the length: ";
		cin >> a;
		cout<<"\n\n";
		cout << "Enter the width: ";
		cin >> b;
		cout<<"\n\n";
		cout << "The area is: ";
		cout << a*b;
	}


	else if(x == 3)
	{
		cout << "Enter the first number: ";
		cin >> a;
		cout<<"\n\n";
		cout << "Enter the second number: ";
		cin >> b;
		cout<<"\n\n";
		cout << "The solution is: ";
		cout << a+b;
	}
	else if(x == 4)
	{
		cout << "Enter the first number: ";
		cin >> a;
		cout<<"\n\n";
		cout << "Enter the second number: ";
		cin >> b;
		cout<<"\n\n";
		cout << "The solution is: "; 
		cout << a*b;
	}

	else if(x == 5) 
	{
		cout << "Enter the first number: ";
		cin >> a; 
		cout<<"\n\n";
		cout << "Enter the second number: ";
		cin >> b;
		cout<<"\n\n";
		cout << "The solution is: ";
		cout << a-b;
	}
	else if(x == 6) 
	{
		cout << "Enter the top/first/outer number: ";
		cin >> a;
		cout<<"\n\n";
		cout << "Enter the bottom/second/inner number: ";
		cin >> b;
		cout<<"\n\n";
		cout << "The answer is: ";
		cout << a/b;
	}
    }
 
  return 0;
}

That way the user can use the program as many times as they want without having to restart it until they choose 7 from the list of choices.

Hope that helps.

-D

Basing the code off of what firstperson has above, if you want to loop the program, you could use a while loop like so <snippet removed>

You know you should hint the answer and not give it out. Maybe give
him an example of how to use while loops first?

You know you should hint the answer and not give it out. Maybe give
him an example of how to use while loops first?

Sorry about that. I was only trying to help. I'll try to be more aware of this moving forward.

Thanks everyone! This really does help a lot! Sorry for not using the right formatting, I'm still a little new haha but I'll be sure to from now on. Thanks for all your guys' help!

This question has already been answered. Start a new discussion instead.