# Integer Error

• (2 Pages)
• 1
• 2

## 20 Replies - 2938 Views - Last Post: 11 May 2009 - 03:42 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=104306&amp;s=fd9fb0ae130a7d8fcff752f08548933f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 CC2082

Reputation: 0
• Posts: 16
• Joined: 09-May 09

# Integer Error

Posted 09 May 2009 - 08:38 PM

Greetings Dream.In.Code members. I needed some assistance with my code I was doing:

Quote

Write a program with a loop that lets the user enter a series of integers, followed by -99 to signal the end of the series. After all the numbers have been entered, the program should display the largest and smallest numbers entered.

Now I got the code working properly except it displays the smallest interger was -4194932

Here's what I'm working with:

#include <iostream>
using namespace std;

int main()
{

int min;
int max;
int input;

do{
cout << "Enter an integer (-99 to quit): " << endl;
cin >> input;
if(input < min) {
min = input;
}
if(input > max) {
max = input;
}
} while (input != -99);

cout << "The largest integer entered was " << max << endl;
cout << "The smallest integer entered was " << min << endl;
cout << "Cody Carson\n\n";

return 0;
}

Is This A Good Question/Topic? 0

## Replies To: Integer Error

### #2 antgalb

Reputation: 0
• Posts: 6
• Joined: 09-September 08

## Re: Integer Error

Posted 09 May 2009 - 09:01 PM

try initializing your variables to 0, also it looks like there is a logic error for how you are checking the for the smallest integer.

### #3 r.stiltskin

• D.I.C Lover

Reputation: 2032
• Posts: 5,435
• Joined: 27-December 05

## Re: Integer Error

Posted 09 May 2009 - 09:15 PM

antgalb, on 9 May, 2009 - 11:01 PM, said:

try initializing your variables to 0, also it looks like there is a logic error for how you are checking the for the smallest integer.

There is no logic error, but you do have to initialize min and max to appropriate values -- you will have to figure out what is "appropriate" for each one.

Note that if you don't initialize them, they can start out with virtually any garbage value -- in your case apparently min just happened to start out with -4194932 as its initial value.

This post has been edited by r.stiltskin: 09 May 2009 - 09:17 PM

### #4 CC2082

Reputation: 0
• Posts: 16
• Joined: 09-May 09

## Re: Integer Error

Posted 09 May 2009 - 09:26 PM

I'm not sure how to initialize my variables, I'm just starting out with C++. Could you help me out?

### #5 antgalb

Reputation: 0
• Posts: 6
• Joined: 09-September 08

## Re: Integer Error

Posted 09 May 2009 - 09:32 PM

Change from this:

int min;
int max;
int input;

To this:

int min = 0;
int max = 0;
int input = 0;

### #6 CC2082

Reputation: 0
• Posts: 16
• Joined: 09-May 09

## Re: Integer Error

Posted 09 May 2009 - 09:42 PM

When I and corrected and ran it, its using -99 as the smallest integer instead. Thanks for you help though.

### #7 antgalb

Reputation: 0
• Posts: 6
• Joined: 09-September 08

## Re: Integer Error

Posted 09 May 2009 - 09:56 PM

Thats because you are assigning -99 to input and then passing it through your if statements and it will end up being the smallest.
To fix this put a if statement that says if input is not equal to -99 then run your min and max if statements.

### #8 r.stiltskin

• D.I.C Lover

Reputation: 2032
• Posts: 5,435
• Joined: 27-December 05

## Re: Integer Error

Posted 09 May 2009 - 10:05 PM

"Initialize a variable" simply means assign a value to it before you use it in your program. So, yes, writing
int min = <some value>;
is one way to initialize the variable min.

But you don't seem to have given any thought to what I posted. If you initilaize min to 0, you will continue to get incorrect results. This has nothing to do with being new to C++. It is not a C++ issue. It is a simple logic issue.

Look -- if the initial value of min is 0, and the input is 5, what are you going to end up with as the minimum, 0 or 5?

Edit: and by the way, there is no need to initialize input, because in your program, the actual value of input is always provided by the user before it is used.

This post has been edited by r.stiltskin: 09 May 2009 - 10:09 PM

### #9 CC2082

Reputation: 0
• Posts: 16
• Joined: 09-May 09

## Re: Integer Error

Posted 09 May 2009 - 10:18 PM

I think its starting to make sense now. So I believe I just have the issue of getting the -99 not to be part of the input with a if statement.

### #10 r.stiltskin

• D.I.C Lover

Reputation: 2032
• Posts: 5,435
• Joined: 27-December 05

## Re: Integer Error

Posted 09 May 2009 - 10:24 PM

CC2082, on 10 May, 2009 - 12:18 AM, said:

I think its starting to make sense now. So I believe I just have the issue of getting the -99 not to be part of the input with a if statement.

Wrong.

You do have to fix that (you can fix it with an if, or you can fix it with a break), but even after you fix that, the program will still not work correctly, for the reason that I already stated.

### #11 CC2082

Reputation: 0
• Posts: 16
• Joined: 09-May 09

## Re: Integer Error

Posted 09 May 2009 - 10:32 PM

So the 1st step I have to do is understand my input?

Example:
if the initial value of min is 0, and the input is 5, what are you going to end up with as the minimum, 0 or 5?

Would this be 0 as the minimum. Im guessing my error that would is if I enter -2 and something like 3, then when I enter -99 to quit, I would still get the error. Im kinda confused right here because if I enter -98 and 2 would it treat -99 still as the smallest interger?

### #12 r.stiltskin

• D.I.C Lover

Reputation: 2032
• Posts: 5,435
• Joined: 27-December 05

## Re: Integer Error

Posted 09 May 2009 - 10:37 PM

Forget about the -99 issue for now. That's a separate matter.

Just look at your code for determining the minimum input value:
if(input < min) {
min = input;
}

What do you need as the starting value for min in order for that to work?

Write down a few sample input values & try applying your code to them (not with the computer, just in your head).

This post has been edited by r.stiltskin: 09 May 2009 - 10:39 PM

### #13 CC2082

Reputation: 0
• Posts: 16
• Joined: 09-May 09

## Re: Integer Error

Posted 09 May 2009 - 11:08 PM

I'm not sure what I would I need for that statement to work. Im guessing "0", but then again it could be any negative number or positive number except -99.

### #14 antgalb

Reputation: 0
• Posts: 6
• Joined: 09-September 08

## Re: Integer Error

Posted 09 May 2009 - 11:16 PM

think about what would happen if you set min = max.
min = max;

if(input < min) {
min = input;
}

### #15 CC2082

Reputation: 0
• Posts: 16
• Joined: 09-May 09

## Re: Integer Error

Posted 09 May 2009 - 11:45 PM

would it = the input?

This post has been edited by CC2082: 09 May 2009 - 11:51 PM