Simple(?) loop question.

I am completely blanking on what obvious thing I need to do to make this loop work. Here's what I have:



---------------------------
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
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int sum;
int diff;
int prod;
int div;
int power;
int quit;
int ans;
int y;
int z;
int remain;
double real;
int choice;

cout << "Enter the first operation type (sum, diff, prod, div, power, or quit): ";
cin >>choice;


while (choice!=quit)
{
cout << "Enter the first (integer) number: ";

cin >> y;

cout << "Enter the second (integer) number: ";

cin >> z;

if (choice==sum)
{
ans = y + z;

cout << "The sum is " << ans << endl;
}

else if (choice==diff)
{
ans = y - z;

cout << "The difference is " << ans << endl;
}

else if (choice==prod)
{
ans = y * z;

cout << "The product is " << ans << endl;
}

else if (choice==div)
{
ans = y / z;
remain = y % z;
real = y / z;

cout << "The quotient is " << ans << endl
     << "The remainder is " << remain << endl
     << "The real quotient is " << real << endl;	 
}

else if (choice==power)
{

ans = (int) pow((double) y, z);

cout << "The power is " << ans << endl;
}

else if (choice==quit)
{ 
return 0;
}

else
cout << "invalid operation code";

cout << "Enter the operation type (sum, diff, prod, div, pow, or quit): ";
cin >> choice;
}

}

------------------------------------

Ignore the whole sum = 1 stuff....

I had it as while (choice=!quit) and if (choice==sum) etc. but they keep throwing me into an infinite loop where it asks for the first (integer) number.


What about the format of the loop is set up wrong?

How do I set the conditions of the "else if" statements properly?

For reference, here are the instructions I have been given:
http://www.cs.niu.edu/~mcmahon/cs240/Assign/as2240f08.html

Thanks for anyone who can help
Last edited on
Input choice again right before the end of the loop.
I updated the post with what I have now. Needing choice again at the end makes sense, so I put that in.

But, why does my program not pause for inputting of the two integer numbers?

The way it is now, it asks for the operation type, and if I put in " sum " then it goes through "enter first number: enter second number: invalid operation code"
and quits.

Whats wrong with the loop?
Use a switch, it saves alot of typing of the if else stuff and its easier to read, also use the [code] which makes ur code easier to read.
Last edited on
That's a problem with std::cin. std::cin.ignore(1,'\n') before the std::cin that's being supposedly skipped.
I am supposed to use the if/else loop method in this assignment.

I don't understand what you mean, helios. What can I do about it not stopping for input, and not recognizing when I put "sum" to go to the sum part of the loop?
I was forced to submit this, but please someone tell me how it could have been fixed for future reference. I know it has to be something minor because all of it makes sense other than it not recognizing to go to the proper function.
http://www.cplusplus.com/forum/beginner/1988/page4.html
Read Duoas' post of "Sep 5, 2008 at 10:28am" (just do a search of that string in the page).
Wow, we haven't gotten into anything like that. I guess I'll try that later and see if my program actually works beyond that issue.

Thanks.
Topic archived. No new replies allowed.