Preventing an infinite loop

This is a discussion on Preventing an infinite loop within the C++ Programming forums, part of the General Programming Boards category; Well, I've just started learning C++ yesterday, and it's the first language I've ever learned. I'm following some tutorials online, ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    Preventing an infinite loop

    Well, I've just started learning C++ yesterday, and it's the first language I've ever learned.

    I'm following some tutorials online, and I was messing around to try and create a simple calculator. However, I've hit two dead ends. Well, here's the source code first:

    Code:
    #include <iostream>
    
    using namespace std;
    
    
    
    //This is for adding x and y.
    int addition(int x, int y) {
    	return x + y;
    }
    
    
    //This is for multiplying x and y.
    int multiplication(int x, int y) {
    	return x * y;
    }
    
    
    //This is for dividing x and y.
    int division(int x, int y) {
    	return x / y;
    }
    
    
    //This is for subtracting x and y.
    int subtraction(int x, int y) {
    	return x - y;
    }
    
    
    
    
    //This is the main interface for the calculator.
    int main() {
    	//The below 3 lines define the two numbers that will be entered by the user and the operation (addition, subtraction, etc).
    	int x;
    	int y;
    	char z;
    
    
    	cout << "Welcome to KK's calculator. Press any key to continue..." <<endl;
    	cin.get();
    
    	while (true) {
    	cout << "Input your first number: " << endl;
    	cin >> x;
    	cin.ignore();
    	cout << "Input your operation (+, -, *, /): " << endl;
    	cin >> z;
    	cin.ignore();
    	cout << "Input your second number and press Enter to get your solution: " << endl;
    	cin >> y;
    	cin.ignore();
    	/*if (y != 0) {
    	} else {
    		cout<< "Invalid operation. 0 cannot be the divisor." << endl;
    	}*/
    	
    
    	if (z == '+') {
    		cout << "Answer: " << addition(x,y) << endl << endl;
    	} else if (z == '-') {
    		cout << "Answer: " << subtraction(x,y) << endl << endl;
    	} else if (z == '*') {
    		cout << "Answer: " << multiplication(x,y) << endl << endl;
    	} else if (z == '/') {
    		cout << "Answer: " << division(x,y) << endl;
    	} else {
    		cout << "Invalid operation. Please try again." << endl << endl;
    	}
    
    	}
    
    	
    	cin.get();
    	return 0;
    }

    The first problem is that I want to prevent my program from going into an infinite loop if someone accidentally inputs a letter instead of a number.

    The second is, I want to make sure you can't divide by 0, otherwise an error comes up.
    I've tried to solve the 0 problem (it's in comments atm), but there's still a problem with it. It pops up my error message, but the program continues, and it breaks the application.

    I would appreciate any tips and help. Suggestions to improve any existing code are welcome as well

  2. #2
    Grey Wizard C_Sparky's Avatar
    Join Date
    Sep 2009
    Posts
    50
    use cin.getline() to prevent the program from crashing when a user enters something other than a number or multiple numbers.

    use a for loop to check the cin buffer:

    Code:
    for(int i=0; buf[i] != '\0'; ++i)
    {
          if(!isdigit(buf[i]))
          {
                 // generate error
          }
           // add code here to make sure it's not a decimal
    }
    convert the cin buffer to an integer with atoi()

    as for dividing by zero, check that only if dividing, and if the second number is equal to zero, generate an error and continue the main loop. Or you leave dividing by zero up to Chuck Norris

    and shouldn't you be using floats instead of ints?

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    You can do input checking by reading one line at a time, and try to parse it. I recommend leaving that for later, though, since it looks like you are just starting and it's a little complicated.

    Checking for 0 in the divisor is easier. You can do something like -
    Code:
    	} else if (z == '/') {
    		if (y != 0) {
    			cout << "Answer: " << division(x,y) << endl;
    		} else {
    			...
    		}
    	}

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Sidenote -
    We usually also indent loops -
    Code:
    	while (true) {
    		cout << "Input your first number: " << endl;
    		cin >> x;
    		cin.ignore();
    		cout << "Input your operation (+, -, *, /): " << endl;
    		cin >> z;
    		cin.ignore();
    		cout << "Input your second number and press Enter to get your solution: " << endl;
    		cin >> y;
    		cin.ignore();
    		/*if (y != 0) {
    		} else {
    			cout<< "Invalid operation. 0 cannot be the divisor." << endl;
    		}*/
    	
    
    		if (z == '+') {
    			cout << "Answer: " << addition(x,y) << endl << endl;
    		} else if (z == '-') {
    			cout << "Answer: " << subtraction(x,y) << endl << endl;
    		} else if (z == '*') {
    			cout << "Answer: " << multiplication(x,y) << endl << endl;
    		} else if (z == '/') {
    			cout << "Answer: " << division(x,y) << endl;
    		} else {
    			cout << "Invalid operation. Please try again." << endl << endl;
    		}
    
    	}

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    2
    Ohh, I don't think I learned those functions yet (except the float thing, that was my mistake). When I get home, I'll take a more in-depth look at your reply and test it out Thanks!

    Oh, and any improvements you would suggest to make the program more efficient? Or better programming style/habits?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 03:45 AM
  3. Infinite Loop with GetAsyncKeyState
    By guitarist809 in forum Windows Programming
    Replies: 1
    Last Post: 04-18-2008, 12:09 PM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM
  5. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21