Infinite Loop problem

closed account (3TkL1hU5)
I've searched around here and on the internet for some solutions to this, but I can't seem to get it figured out. Basically, I am writing a very, very simple calculator-type program. As you'll see in my code I have it setup to give the user 4 options for operators, then it asks for two numbers, completes the operation and outputs, then prompts for another problem.

The problem I keep finding myself with is that if the user inputs a char instead of an integer the program infinitely loops. The problem only happens, however, if the user goes through the program twice.

eg.. I choose 1 for addtion, input 2 and 2. The program outputs 4 and asks if I have another problem. I input 1 for yes, then I input "a". *infinite loop*


This is my first program that I've ever tried on my own. I'm not using the best book (C++ for Dummies.... ) and I'm sure my code will receive a fair amount of criticism. But bring it on, please. I am open to any constructive things you can say.

Thanks
Austin


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;


int main(int nNumberofArgs, char* pszArgs[])
{
   
   //Beginning of the calculator application in C++.
   //Start with statements and the initial options integer.
   cout << "Welcome to Austin's Calculator!"
        << "\n===============================" << endl;
   cout << "At this time it is designed to perform \nbasic "
        << "mathematic functions to two numbers." << endl;
   start:     
   cout << "\n\nWhich function would you like to perform?" << endl;
   cout << "\n\n\nPress 1 for Addition."
        << "\nPress 2 for Subtraction."
        << "\nPress 3 for Multiplication."
        << "\nPress 4 for Division.\n" << endl;
   cout << "Please enter your selection: ";
   
   int sel;
   cin >> sel;
   
   if (sel == 1)
   {
         double xadd;
         double yadd;
         cout << "\nEnter your first number: ";
         cin >> xadd;
         cout << "Enter your second number: ";
         cin >> yadd;
         cout << "\nThe answer is: " << xadd + yadd << endl;
         }
         
   else if (sel == 2)
   {
         double xsub;
         double ysub;
         cout << "\nEnter your first number: ";
         cin >> xsub;
         cout << "Enter your second number: ";
         cin >> ysub;
         cout << "\nThe answer is: " << xsub - ysub << endl;
         }
         
   else if (sel == 3)
   {
         double xmul;
         double ymul;
         cout << "\nEnter your first number: ";
         cin >> xmul;
         cout << "Enter your second number: ";
         cin >> ymul;
         cout << "\nThe answer is: " << xmul * ymul << endl;
         }
         
   else if (sel == 4)
   {
         double xdiv;
         double ydiv;
         cout << "\nEnter your first number: ";
         cin >> xdiv;
         cout << "Enter your second number: ";
         cin >> ydiv;
         cout << "\nThe answer is: " << xdiv / ydiv << endl;
         }
         
   else if (sel != 1,2,3,4)
   { 
       goto end;
       }
    
    //Integer for Repeat represented by "rep"
    int rep;
    cout << "\n Would you like to perform another operation?" << endl;
    cout << "\nPress 1 for Yes." << endl;
    cout << "Press 2 for No." << endl;
    cout << "\nPlease enter your selection: ";
    cin >> rep;
    
    if (rep == 1)
    {
            goto start;
            }
            
            else if (rep == 2)
            { 
                 goto end;
                 }
                 
                 else 
                 {
                      goto end;
                      }
    end:
    cout << "\n\nThank you for using Austin's Calculator!" << endl;
    // Wait until ready to close the program.
    
    system("PAUSE");    
    return 0;
}


Last edited on
The problem is you are putting in non-numerical characters and trying to read them into a double. This causes cin to go into an error state, and all successive reads will fail.

One way to fix this is read into a string and try to convert it manually:
1
2
3
4
5
6
7
8
9
std::string input;
double processed_input;
std::getline(std::cin, input);
std::stringstream ss;
ss<<input;
if(!(ss>>processed_input)) {
    // failed
}
// success 
Another way to fix this is to check if the read was successful and if not, clear the error state:
1
2
3
4
5
6
7
8
9
if(cin >> sel)
{
    // good
}
else
{
    // bad
    cin.clear(); // fix the error
}
1
2
3
4
cout << "\nEnter your first number: ";
         cin >> x;
         cout << "Enter your second number: ";
         cin >> y;
This is common to all operations. Put it just one time.

1
2
3
4
else if (sel != 1,2,3,4){ //this should be
else if(sel!=1 && sel!=2 && sel!=3 && sel!=4){
//or better
else {
There is no need to put the complementary check, and you are doing it wrong.

goto end; Avoid goto. Use loops instead.

system("PAUSE"); Avoid system (is system dependent). Read http://www.cplusplus.com/forum/beginner/1988/
closed account (3TkL1hU5)
Thanks for all of the help everyone. I'm going to re-work it later this afternoon when I get home. I really appreciate the advice.

-Austin
closed account (3TkL1hU5)
Okay, so I haven't really studied too much further into C++ but I revised my code completely and came up with this. The problem I'm having now is when it asks which operation the user wants then you have to input your choice twice...

I know it's because of the loop I added to test the input at lines 49 - 59.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//Austin Aryain

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <limits>


int main(int nNumberofArgs, char* pszArgs[])
{

    //The Class To Enter Data For The Equations
    class EnterData
    {
        public:
        double x;
        double y;
        double z;
    };


    //Integer yn declared for questioning user if they wish to continue
    int yn;
    //Standard calculator question...
    std::cout << "\tWelcome to Austin's Revised Calculator!" << std::endl;
    std::cout << "\t=======================================" << std::endl;
    std::cout << "    It is designed to perform basic math operations." << std::endl;
    std::cout << "\n\nWould you like to perform an operation?" << std::endl;
    std::cout << "[1] Yes or [2] No: ";
    std::cin >> yn;

//begins my while loop to keep the program running after operation is complete
    while (yn == 1)
    {

        //integer choice declared for operator selection
        int choice;
        std::cout << "\n\nWhich operation would you like to perform?" << std::endl;
        std::cout << "[1] Addition" << std::endl
                  << "[2] Subtraction" << std::endl
                  << "[3] Multiplication" << std::endl
                  << "[4] Division" << std::endl
                  << "[5] Squares"  << std::endl
                  << "[6] SquareRoot" << std::endl;
        std::cout << "\nWhat is your choice: ";
        std::cin >> choice;


	while ( ! (std::cin >> choice))
    {
        // Enter this loop if input fails because of invalid data.

        std::cout << "Invalid selection!  Try again: ";
        std::cin.clear ();   // reset the "failure" flag

		std::cin.ignore (1000, '\n');
          // Skip to next newline or 1000 chars,
                                  // whichever comes first. 
    }


        if (choice == 1)
        {


            EnterData ed;
            std::cout << "\nEnter your first number: ";
            std::cin >> ed.x;
            std::cout << "Enter your second number: ";
            std::cin >> ed.y;
            ed.z = ed.x + ed.y;
            std::cout << "\nThe answer is: ";
            std::cout << ed.z;

        }
        else if (choice == 2)
        {


            EnterData ed;
            std::cout << "\nEnter your first number: ";
            std::cin >> ed.x;
            std::cout << "Enter your second number: ";
            std::cin >> ed.y;
            ed.z = ed.x - ed.y;
            std::cout << "\nThe answer is: ";
            std::cout << ed.z;

        }
        else if (choice == 3)
        {


            EnterData ed;
            std::cout << "\nEnter your first number: ";
            std::cin >> ed.x;
            std::cout << "Enter your second number: ";
            std::cin >> ed.y;
            ed.z = ed.x * ed.y;
            std::cout << "\nThe answer is: ";
            std::cout << ed.z;

        }
        else if (choice == 4)
        {


            EnterData ed;
            std::cout << "\nEnter your first number: ";
            std::cin >> ed.x;
            std::cout << "Enter your second number: ";
            std::cin >> ed.y;
            ed.z = ed.x / ed.y;
            std::cout << "\nThe answer is: ";
            std::cout << ed.z;

        }
        else if (choice == 5)
        {


            EnterData ed;
            std::cout << "\nEnter the number to be squared: ";
            std::cin >> ed.x;
            ed.z = ed.x*ed.x;
            std::cout << "\nThe answer is: ";
            std::cout << ed.z;

        }
        else if (choice == 6)
        {


            EnterData ed;
            std::cout << "\nEnter the number to be SquareRooted: ";
            std::cin >> ed.x;
            ed.z = sqrt(ed.x);
            std::cout << "\nThe answer is: ";
            std::cout << ed.z;

        }

        else
        {
            std::cin.clear();
            break;
        }



        int loop;
        std::cout << "\n\nWould you like to perform another operation?" << std::endl;
        std::cout << "<1> Yes or <2> No: ";
        std::cin >> loop;

        if (loop == 1)
        {
            continue;
        }

        else if (loop == 2)
        {
            break;
        }
  }


        std::cout << "\n\nThanks for using Austin's Calculator!" << std::endl;
        //Wait until the user is ready to close the program
        std::cout << "\n\nPress ENTER to continue...";
        std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
    return 0;

}


Last edited on
I got rid of 49-59 and made some other changes to get it to work. No doubt there is probably a better method for lines 61 & 66??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//Austin Aryain

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <limits>


int main(int nNumberofArgs, char* pszArgs[])
{

    //The Class To Enter Data For The Equations
    class EnterData
    {
        public:
        double x;
        double y;
        double z;
    };


    //Integer yn declared for questioning user if they wish to continue
    int yn;
    //Standard calculator question...
    std::cout << "\tWelcome to Austin's Revised Calculator!" << std::endl;
    std::cout << "\t=======================================" << std::endl;
    std::cout << "    It is designed to perform basic math operations." << std::endl;
    std::cout << "\n\nWould you like to perform an operation?" << std::endl;
    std::cout << "[1] Yes or [2] No: ";
    std::cin >> yn;

	if  (yn == 2)
    {
		std::cin.ignore(256, '\n');
		std::cout<<"Thanks for using Austin's Calculator!\n";
		std::cout << "\nPress ENTER to continue...";
		std::cin.get();

		exit(0);   //might be some possible evil in using exit function??  if so someone will mention it!!
	}

//begins my while loop to keep the program running after operation is complete
    while (yn == 1)
    {

        //integer choice declared for operator selection
        int choice;
		do 
		{
        std::cout << "\n\nWhich operation would you like to perform?" << std::endl;
        std::cout << "[1] Addition" << std::endl
                  << "[2] Subtraction" << std::endl
                  << "[3] Multiplication" << std::endl
                  << "[4] Division" << std::endl
                  << "[5] Squares"  << std::endl
                  << "[6] SquareRoot" << std::endl;
        std::cout << "\nWhat is your choice: ";
        std::cin >> choice;


		if ((choice != 1)&&(choice != 2)&&(choice != 3)&&(choice != 4)&&(choice != 5)&&(choice != 6)) 
		{
			std::cout<<"Invalid selection!  Try again:\n";
		}

		} while ((choice != 1)&&(choice != 2)&&(choice != 3)&&(choice != 4)&&(choice != 5)&&(choice != 6)) ;
		
		
		
		EnterData ed;
            std::cout << "\nEnter your first number: ";
            std::cin >> ed.x;
            std::cout << "Enter your second number: ";
            std::cin >> ed.y;



        if (choice == 1)
        {
            ed.z = ed.x + ed.y;
            std::cout << "\nThe answer is: ";
            std::cout << ed.z;

        }
        else if (choice == 2)
        {
            ed.z = ed.x - ed.y;
            std::cout << "\nThe answer is: ";
            std::cout << ed.z;

        }
        else if (choice == 3)
        {

            ed.z = ed.x * ed.y;
            std::cout << "\nThe answer is: ";
            std::cout << ed.z;

        }
        else if (choice == 4)
        {

            ed.z = ed.x / ed.y;
            std::cout << "\nThe answer is: ";
            std::cout << ed.z;

        }
        else if (choice == 5)
        {
			ed.z = ed.x*ed.x;
			std::cout << "\nThe answer is: ";
            std::cout << ed.z;

        }
        else if (choice == 6)
        {

            ed.z = sqrt(ed.x);
            std::cout << "\nThe answer is: ";
            std::cout << ed.z;

        }

        else
        {
            std::cin.clear();
            break;
        }



        int loop;
        std::cout << "\n\nWould you like to perform another operation?" << std::endl;
        std::cout << "<1> Yes or <2> No: ";
        std::cin >> loop;

        if (loop == 1)
        {
            continue;
        }

        else 
        {
          	std::cin.ignore(256, '\n');
		std::cout<<"Thanks for using Austin's Calculator!\n";
		std::cout << "\nPress ENTER to continue...";
		std::cin.get();
		exit(0); 

        }
  }
  
       
        std::cin.ignore().get();
    return 0;

}
closed account (3TkL1hU5)
Ah blew my mind just now. ne555 had said that 70-74 was common to all operations and to put it only once, but for some reason my brain didn't even compute this. I was considering going back to pseudo-code which probably would have helped.

I'm still getting the loop though. If you go through one operation, then press 1 to choose again and THEN enter a letter it loops. I appreciate the help though. :D (still can't believe I missed that...)
Realised lines 134-138 (below) only needed once so 32-40 removed.
Got rid of 124-128. 126-130 added instead.
Changed [1] and [2] option to 'y' and 'n'. ie; line 116 int to char. line 121 if (loop = 'y') etc;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//Austin Aryain

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <limits>


int main(int nNumberofArgs, char* pszArgs[])
{

    //The Class To Enter Data For The Equations
    class EnterData
    {
        public:
        double x;
        double y;
        double z;
    };


    //Integer yn declared for questioning user if they wish to continue
    char yn;
    //Standard calculator question...
    std::cout << "\tWelcome to Austin's Revised Calculator!" << std::endl;
    std::cout << "\t=======================================" << std::endl;
    std::cout << "    It is designed to perform basic math operations." << std::endl;
    std::cout << "\n\nWould you like to perform an operation?" << std::endl;
    std::cout << "[y] Yes or [n] No: ";
    std::cin >> yn;

	

//begins my while loop to keep the program running after operation is complete
    while (yn == 'y')
    {

        //integer choice declared for operator selection
        int choice;
		do 
		{
        std::cout << "\n\nWhich operation would you like to perform?" << std::endl;
        std::cout << "[1] Addition" << std::endl
                  << "[2] Subtraction" << std::endl
                  << "[3] Multiplication" << std::endl
                  << "[4] Division" << std::endl
                  << "[5] Squares"  << std::endl
                  << "[6] SquareRoot" << std::endl;
        std::cout << "\nWhat is your choice: ";
        std::cin >> choice;


	if ((choice != 1)&&(choice != 2)&&(choice != 3)&&(choice != 4)&&(choice != 5)&&(choice != 6)) 
		{
			std::cout<<"Invalid selection!  Try again:\n";
		}

		} while ((choice != 1)&&(choice != 2)&&(choice != 3)&&(choice != 4)&&(choice != 5)&&(choice != 6)) ;
		
		
		
		EnterData ed;
            std::cout << "\nEnter your first number: ";
            std::cin >> ed.x;
            std::cout << "Enter your second number: ";
            std::cin >> ed.y;



        if (choice == 1)
        {
            ed.z = ed.x + ed.y;
            std::cout << "\nThe answer is: ";
            std::cout << ed.z;

        }
        else if (choice == 2)
        {
            ed.z = ed.x - ed.y;
            std::cout << "\nThe answer is: ";
            std::cout << ed.z;

        }
        else if (choice == 3)
        {

            ed.z = ed.x * ed.y;
            std::cout << "\nThe answer is: ";
            std::cout << ed.z;

        }
        else if (choice == 4)
        {

            ed.z = ed.x / ed.y;
            std::cout << "\nThe answer is: ";
            std::cout << ed.z;

        }
        else if (choice == 5)
        {
			ed.z = ed.x*ed.x;
			std::cout << "\nThe answer is: ";
            std::cout << ed.z;

        }
        else if (choice == 6)
        {

            ed.z = sqrt(ed.x);
            std::cout << "\nThe answer is: ";
            std::cout << ed.z;

        }
		
        char loop;
        std::cout << "\n\nWould you like to perform another operation?" << std::endl;
        std::cout << "<y> Yes or <n> No: ";
        std::cin >> loop;

        if (loop == 'y')
        {
            continue;
        }

        else 
        {
			break;
          
        }
  }
 
 
		std::cin.ignore(256, '\n');
		std::cout<<"Thanks for using Austin's Calculator!\n";
		std::cout << "\nPress ENTER to continue...";
		std::cin.get();
		exit(0);   
	  
       
        std::cin.ignore().get();
    return 0;

}
Topic archived. No new replies allowed.