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;
}

Dani AI

Generated

Nice progress — the thread already contains two useful directions: cleaned up the variable use and control flow, and showed how to let the user run the menu repeatedly. To make the program robust and maintainable, refactor rather than copy/paste: one loop that shows the menu and dispatches to small, focused routines for each operation.

A simple, practical workflow:

  • print the menu and read a choice,
  • validate the choice (range and type),
  • call a short function that reads its operands, computes the result, and prints it,
  • repeat until the user chooses Quit.

Use descriptive names (for example length, width, num1, num2) and a single numeric type such as double for all arithmetic. Move repeated logic (menu display, input-validation) into helper routines so each operation only contains its own logic.

Watch out for two common runtime/compile issues not fully addressed in the replies:

  • stray tokens or malformed declarations (the original contained things like int; 2;) will cause compile errors — remove those and declare variables properly.
  • invalid input and division by zero. After cin >> x, check std::cin.fail() and recover with std::cin.clear() and std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n') before re-prompting. For division, check the denominator and ask again rather than letting the program produce undefined or surprising results.

Debug incrementally: get the menu loop compiling first, then add one operation at a time and test it. Combining ’s cleaner layout with ’s loop and the input-safety checks above gives a simple, reliable console calculator that is easy to extend.

Recommended Answers

All 6 Replies

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;
}

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.