Help me with this "while" loop

Write your question here.

"Q. Write a C++ program that asks the user to enter the name and time for 5 athletes, then prints the name and time of the winner. All times are measured in minutes. The winner has the lowest time."

I got the answer already on my own, using "while" loops as this question was supposed to be solved that way.

But the big question is.. whats the logic of this code. I really don't know how it worked. Like how's it possible for "if" statement to compare "time" with "minimum" which is not associated with any value. Nevertheless, the code is 100% correct, and again I just want to understand how it works. Thank you.

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
#include<iostream>
#include<string>
using namespace std;
int main()
{
	int time, count=0, minimum;
	string name;
	

	while(count<5)
	{
	cout<<"Enter the name and time: \n";
	cin>>name>>time;
		if(time<minimum)
		minimum=time;
		

		count++;
	}
	if(count != 0)
	cout<<"winner is: "<<minimum;
	else
	cout<<"No winner";
	return 0;
}
Last edited on
closed account (1vRz3TCk)
You wrote the code and don't know how it works?

Nevertheless, the code is 100% correct
are you sure about that?
prints the name and time of the winner
You're partially right that minimum isn't associated with any value at the beginning. It is associated with a value, but we have no idea what it is, because you've declared 'minimum' and used it without instantiating it. Your program happens to work by luck that the initial value of 'minimum' is not less than the actual minimum time the user inputs.

You need to initialize minimum in some way before you use it. Something like this before entering your loop is safe assuming the user inputs valid data.
1
2
cin>>name>>time;
minimum = time;

minimum = INT_MAX; is another way to initialize. It will set 'minimum' to the maximum possible value allowed by 'int'. This will almost always be safe. You have to #include <climits> to use this.

You should probably not be using 'int' for the time. Times will often be decimals. You should use a floating point data type.

You're ignoring the name of the winner. You need a separate variable to keep track of the winner as well, or you can bind the winners name to his time:
1
2
pair <string, int> athlete;
cin >> athlete.first >> athlete.second;
Last edited on
All right I have initialized minimum before entering the loop and thus had to subtract the while condition by 1.

Also, I had to apply the if statement two times to avoid wrong answers. please take a look

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<iostream>
#include<string>
using namespace std;
int main()
{
	float  time, minimum;
        int  count=0;
	string name, ch;
	
	cout<<"Enter the name and time: \n";
	cin>>name>>time;
	minimum=time;
	ch=name;

	while(count<4)
	{
	
		if(time<minimum)
		{
		minimum=time;
		ch=name;	
		}
	
		
		cout<<"Enter the name and time: \n";
        	cin>>name>>time;
		if(time<minimum)
		{
		minimum=time;
		ch=name;	
		}
	
		count++;
	}
	if(count != 0)
	cout<<"winner is: "<<ch<<" with a score of: "<<minimum<<" minutes";
	else
	cout<<"No winner";
	return 0;
}


I know this code can be executed in an easier way, for instance, using the way you suggested: "minimum = INT_MAX;". But I'm trying to solve the code within the material we're going to be examined with which is limited to the while loop and if statement.
Last edited on
What is
1
2
3
4
5
if(time<minimum)
		{
		minimum=time;
		ch=name;	
		}

Doing before the cout statement and inside the while loop?

Also what is the point of
1
2
3
4
if(count != 0)
	cout<<"winner is: "<<ch<<" with a score of: "<<minimum<<" minutes";
	else
	cout<<"No winner";

When is count = 0?

using the way you suggested: "minimum = INT_MAX;
Why would you do that?

After you've removed those two purposeless if statements then you need to worry about exceptions, otherwise you're good to go.

Oh yeah it's much better now, thanks

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
#include<iostream>
#include<string>
using namespace std;
int main()
{
	int time, count=0, minimum;
	string name, ch;
	
	cout<<"Enter the name and time: \n";
	cin>>name>>time;
	minimum=time;
	ch=name;

	while(count<4)
	{	
		cout<<"Enter the name and time: \n";
     	cin>>name>>time;
		if(time<minimum)
		{
		minimum=time;
		ch=name;	
		}
	
		count++;
	}
	cout<<"winner is: "<<ch<<" with a score of: "<<minimum<<" minutes";
	return 0;
}
Topic archived. No new replies allowed.