Finding highest value from user input

Nov 12, 2016 at 7:12pm
I have to write a program that finds the largest value from 15 random values entered by the user. The values can be either whole numbers or decimals and there is no limit as to how large they can be. The program can't make use of arrays, vectors, or any of that more complicated stuff, only a loop. I have done my best to piece together what I could on my own, and I read some of the older threads on this topic. Here is what I have up to this point. The program compiles but it doesn't work properly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main ()
{
   double num;
   cout << "Please enter 15 numbers:\n";
   cin >> num;

   double highest;
    
   double inNum;// for reading the rest of the values
   for(int i = 0; i >= highest; i++ )
     {
       cin >> inNum;// read input
       if( inNum > highest )// compare to highest yet
		highest = inNum;// replace highest
     }

   cout << "The highest number was: " << highest;
   return 0;
}
Nov 12, 2016 at 8:01pm
line 10 - highest is not initialized to a valid value before the code starts to try to use it in the comparison on line 16.

line 13 - The number of iterations of the for loop shouldn't depend on the value of highest - you're collecting a fixed number of values (15 total, including the one you collect outside the loop).
Nov 12, 2016 at 10:04pm
Hi wildblue, thanks for your help. I got the program running perfect. For anyone else who may stumble across this thread, here is what I did. In line 10, I set double highest=0 and in line 13 I redid the for loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main ()
{
   double num;
   cout << "Please enter 15 numbers:\n";
   cin >> num;

   double highest=0;
    
   double inNum;// for reading the rest of the values
   for(int i = 0; i <14; i++ )
     {
       cin >> inNum;// read input
       if( inNum > highest )// compare to highest yet
	 highest = inNum;// replace highest
     }

   cout << "The highest number was: " << highest <<endl;
   return 0;
}
Last edited on Nov 12, 2016 at 10:04pm
Nov 12, 2016 at 10:06pm
double highest=0;

What if the first number entered at cin >> num is greater than the other numbers entered in the loop? I think I'd initialize highest to that first number in num.
Nov 13, 2016 at 12:26am
What exactly do you mean? Are you saying the program would malfunction under certain circumstances?
Nov 13, 2016 at 12:31am
What if at line 8 they enter 100 as the first number, then in the loop, all the numbers are less than 20? The highest number they entered is 100, but that's ignored if you initialize 'highest' to 0 instead of the first number they just entered.
Nov 13, 2016 at 1:19am
So what would have to be done to fix it? I have no idea how to fix this issue.
Nov 13, 2016 at 1:57pm
I think I'd initialize highest to that first number entered in num at line 8. It's the highest number they've entered so far.
Nov 13, 2016 at 4:49pm
Ok, instead of setting double highest=0 I set double highest=num. I tested several batches of numbers and the program works fine now. I just wanted to ask how that one change made the program work (so I can better understand c++ logic). How does the compiler know which one of the numbers entered by the user should be assigned to highest?


#include <iostream>
using namespace std;

int main ()
{
double num;
cout << "Please enter 15 numbers:\n";
cin >> num;

double highest=num;

double inNum;// for reading the rest of the values
for(int i = 0; i < 14; i++ )
{
cin >> inNum;// read input
if( inNum > highest )// compare to highest yet
highest = inNum;// replace highest
}

cout << "The highest number was: " << highest <<endl;
return 0;
}
Topic archived. No new replies allowed.