loops

Im having trouble determining what loop to use and how to use it. I have to be able to have a user enter multiple names and ages and the program must tell who is the oldest. I know the code below is incomplete it is just a starting point, is what i have at the moment.

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()
{
	int name=0;
	float quit=0;
	int age=0;
	int oldest = 0;
	
	for(name=0; name!=quit;name++)
	{
		cout << "Enter name:" << endl;
		cin >> name;
		if (name == quit)
			cout << "oldest is" << oldest;



	}
	return 0;
}
Hi,

it seems like you are at the very beginning of programming, you should check out some beginner tutorials first, as example you can not store names (as strings or chars) in an int variable.

B2T:
I think it would be the simplest method to use while, check if the entered age is greater than the highest one recorded, if yes, replace. if not, get next age. There are multiple solutions to your problem, I wrote this as an example.

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

using namespace std;

int main()
{
    int oldest_age = 0;
    int age = 0;

    string oldest_name = "Bill";
    string name;

    cout << "Enter 'stop' as name to stop input and receive oldest person in list!"<<endl;
    while(name != "stop"){
        cout << "Enter name: ";
        cin >> name;

        if(name == "stop"){
            break;
        }

        cout << "Enter age of " << name <<": ";
        cin >> age;

        if (age > oldest_age){
            oldest_age = age;
            oldest_name = name;
        }

    }

    cout << "The oldest person is " << oldest_name << " with " << oldest_age << " years.";


    return 0;
}
Last edited on
for and while overlap, because a while loop is just a for loop without all the parts.

while(something);
is the same as
for(;something;);

but the second statement is odd and it is preferred that you just use a while if you have no preconditions or control updates.

for is preferred over
x = 0;
while (x < 10)
x++;

better to just have this, to handle the loop control all together
for(x = 0; x < 10; x++)

and do-while is to handle loops that you want to execute once no matter what, and more times conditionally. This is usually defined by user input (where user may be a device, or service, or something beyond a human at the keyboard)

do
cin >> x; //you always want to read x once, at least!
other stuff;
while(x != quit)


I think you want the do-while above. But the key to choosing which to use is really how it looks. You can make it work either way in many cases, as seen above, but there are natural fits to your code that make a for or a while the better choice. Do-while also has overlap, but it is usually easier to see when you need it compared to the others.




Ah i see what my problem was. Thank you both for helping me, now i actually know how to use the loops. Also in order to use a string as a variable do you have to do #include <string>
Also whenever i input a name with a space for example john smith it crashes the program it just duplicates the same line infinitely
hello, if you want to get line input, you should use getline() as cin does stop on whitespaces (that may not be the right explanation to cin handling whitespaces differently)(http://en.cppreference.com/w/cpp/string/basic_string/getline)

std::getline(std::cin, variablename);
Topic archived. No new replies allowed.