Really confused to whats going on

In the code below, the method getLowerLimit outputs the prompt but does not wait for user input. I can't figure out why it's doing this since I've done the same procedure in previous code without a problem.

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
#include <cmath>
#include <iostream>
#include <string>

using namespace std;

int xLow = 0;
int xHigh = 0;
int interval = 0;
int width = 0;
int error = 0;
int oldResult = 0;
int result = 0;

class SimpsonsRule {
	public:
		SimpsonsRule();
		int getUpperLimit();
		int getLowerLimit();
};

SimpsonsRule::SimpsonsRule() {
}

int SimpsonsRule::getUpperLimit() {
	cout << "Enter the upper limit: " << endl;
	cin >> xHigh;
}

int SimpsonsRule::getLowerLimit() {
	cout << "Enter the lower limit: " << endl;
	cin >> xLow;
}

int main() {
	SimpsonsRule test;
	test.getUpperLimit();
	test.getLowerLimit();
	cout << "All tests passed" << endl;
}
Wait, this program compiles? It shouldn't and it does not for me. getLowerLimit as well as getUppetLimit are both integer functions, meaning they must return an integer.

Im not sure what you want to do with the program but my guess is that, you want to return both xHigh and xLow, and then "catch" them from main.

return xHigh;

 
int highCatcher = test.getUpperLimit();


You can do the same with getLowerLimit. Then you can (Im guessing) do an if statement in main which determines whether or not all tests are passed.
Last edited on
All this program is meant to do so far is take an input for xHigh and xLow. But xLow is not waiting for user input. There's going to be more to it but that's all I want it to do so far.
Well, if you're not going to listen to what I just wrote. Then that means your two functions should not be of integer. make them void functions, and it should work fine.
I don't think you're understanding my problem. I know what to do because I've done it in the past. I cout a prompt for user input. I take the user's input and pass it into a variable. The problem I'm having is that after the first prompt and input for xHigh, the program does not wait for any more input.
I understand your problem perfectly fine.

> I know what to do because I've done it in the past.

Are you sure about that? Becuase Im not. as Ive said before, your INTEGER functions has to return an INTEGER, period. I did that with your program, and everything worked like a charm. I also tried what other thing I suggested, using VOID functions instead of int, also worked perfectly fine. If you're not willing to listen then dont ask in the first place.

P.s. Yes, working like a charm and worked perfectly fine does mean that the program waits and asks for the lower limit after the higher one.
Last edited on
After fixing the code to have those functions actually return something, the program behaves exactly how I would expect it to (both input prompts work fine).


When it doesn't work for you, what input are you giving it?
@Disch I think his problem is that he hasnt even tried what Ive suggested above, 2 clear methods that both work fine.
I'm going under the assumption that his compiler is allowing that code to compile but it still is not behaving as he expects.

And yes, I agree his code is working fine. But for him it isn't, which is why I'm asking about the input he's providing. If he's giving invalid input it might cause behavior he's seeing.
@Disch I figured out the problem. I was entering values with decimals such as 2.5 and passing them into a variable of type int. I didn't see my mistake at first but realized that I may be entering invalid input. After changing the variables to type float it worked. Thank you for your help.
Even if it was caused by invalid input it doesnt change the fact that his code is very incorrect.

@Nathan Alvarez glad it worked, but please fix your code with the suggestions. Im honestly surprised it even compiles.
Last edited on
Topic archived. No new replies allowed.