Thread: Going back in code

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    203

    Going back in code

    I have a simple calculator that checks that (a) for subtraction second number is smaller than first number and (b) for division, denominator is not zero. If (a) or (b) occurs, the code currently flashes a warning and program ends. Code is below.
    How can I tweak this code to bring us back to the "Enter first number ..." line so that users are given opportunities to keep entering numbers until they get it right? Many thanks

    Code:
    #include<iostream>
    using namespace std;
    int main(){
    	int num1, num2;
    	char op;
    	cout<<"Enter first number: "<<endl;
    	cin>>num1;
    	cout<<"Enter second number: "<<endl;
    	cin>>num2;
    	cout<<"Enter arithmetic operation you'd like to perform: "<<endl;
    cin>>op;
    
    
    switch(op){
    
    
    case'+': cout<<" "<<(num1 + num2)<<endl;
    	break;
    case'-': if (num2 > num1){
    		cout<<"Subtraction only allowed when first number is greater than second number"<<endl;
    		}
    		else{
    		cout<< (num1 - num2)<<endl;
    	break;
    case'*': cout<<(num1 * num2)<<endl;
    	break;		
    case'/': if (num2 == 0){
    			cout<<"Division is not allowed by zero"<<endl;
    		}
    		else{
    		cout<<(num1 / num2)<<endl;
    default: cout<<"\n\n"<<endl;
    			}
    		}
    	}
    }

  2. #2

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    Many thanks, here's a snippet of code for the subtraction case if anyone's interested:

    Code:
    case'-': while (num2 > num1){
    		cout<<"Subtraction only allowed when first number is greater than second number"<<endl;
    		cout<<"Enter second number: "<<endl;
    		cin>>num2;
    		}
    		while(num2 < num1){
    		cout<< (num1 - num2)<<endl;
    		break;}// first break statement stops the while loop;
    		break;// second break statement stops the next switch from being implemented;

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,615
    Code:
    while(num2 < num1)
    {
            cout<< (num1 - num2)<<endl;
            break;
    }// first break statement stops the while loop
    If you are only going to do something once, then don't loop it.

    I would make sure the numbers are good before you go into the switch as well.

  5. #5
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    Yes, I see what you mean. Thanks v much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert a.out back to C code??
    By Rob4226 in forum C Programming
    Replies: 3
    Last Post: 09-16-2009, 10:33 PM
  2. Replies: 7
    Last Post: 09-08-2006, 11:57 AM
  3. How would you go back to a section of code?
    By XenoForce in forum C++ Programming
    Replies: 23
    Last Post: 10-20-2004, 10:51 PM
  4. Some woman back ended my car today, and back hurts
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 08-20-2003, 12:42 AM
  5. how to loop back for this code ?
    By Jasonymk in forum Windows Programming
    Replies: 3
    Last Post: 02-28-2003, 12:40 PM

Tags for this Thread