# Preventing an infinite loop

• 02-21-2010
kk415kk
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 :)
• 02-21-2010
C_Sparky
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?
• 02-22-2010
cyberfish
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 {                         ...                 }         }```
• 02-22-2010
cyberfish
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;                 }         }```
• 02-22-2010
kk415kk
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?