Loop shows last line instead of all relevant

i need my output to display both names of people who inputted the word "Fairytale" under UrType variable instead of only reading the last loop. i know i got lost somewhere but dont know where exactly

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

class Oz
{
private:
string urType;
string urTypeN;

public:

void getOzDetails()
{
int count = 1;
while (count<=2)
{
count = count+1;
cout <<"Please enter your name?";
cin>>urTypeN;
cout<<"Do you believe in Fairytales or you Realistic?";
cin>>urType;
}
}
string dreamers()
{
int count = 1;
while(count<=2)
{
count =count + 1;
if(urType !="Fairytales")
{cout<<"Not a believer";}
else
{cout<<"Name: "<<urTypeN;}
}
return 0;}
};

int main()
{
Oz el;
el.getOzDetails();
el.dreamers();

}
Last edited on
First off, some distinctive formatting might help. Here's your code, reformatted, but otherwise unchanged:

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
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class Oz
{
private:
    string urType;
    string urTypeN;

public:
    void getOzDetails()
    {
        int count = 1;
        while (count<=2)
        {
            count = count+1;
            cout <<"Please enter your name?";
            cin>>urTypeN;
            cout<<"Do you believe in Fairytales or you Realistic?";
            cin>>urType;
        }
    }
    
    string dreamers()
    {
        int count = 1;
        while(count<=2)
        {
            count =count + 1;
            if(urType !="Fairytales")
            {
                cout<<"Not a believer";
            }
            else
            {
                cout<<"Name: "<<urTypeN;
            }
        }
        
        return 0;
    }
};

int main()
{
    Oz el;
    el.getOzDetails();
    el.dreamers();
}


Secondly it's not clear (to me) what you're trying to achieve, therefore, it's not clear what is going wrong. Could you describe, in human language, the actual requirement as well, not just what you think is wrong, but what you're trying to do with the code you posted.
I am trying to get all the names of people who inputed "Fairytales" in the loop of 2 instead of the code reading only the last line. So lets assume that both inserted Fairytale and the

output would be:

Name: tipaye
Name: silentOne
Okay, you need to store the input. At the moment you're overwriting the first input with the second. You have many choices, for this.
Use 2 variables, concatenate the inputs, use a container, etc. I'll show an example below, using a native array. This example is simple and complete, but is not how you should do it. I imagine you're learning, and in time will figure out better ways of doing it, I just don't want you to think this is the recommended way, and at the same time, I don't want to bore you with long explanations of features you haven't learnt yet...

Also, you're returning 0 from a function that is expected to return a string - and you don't use the return value anyway, so I got rid of that.
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
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
#include <iomanip>
#include <vector>

using namespace std;

class Oz
{
private:
	static const size_t num_users = 2;
	string urType[num_users];
	string urTypeN[num_users];
	// you should learn about vector, pair and map

public:
	void getOzDetails()
	{
		for (size_t count = 0; count < num_users; ++count)
		{
			string name;
			cout <<"Please enter your name?";
			cin>>urTypeN[count];
			cout<<"Do you believe in Fairytales or you Realistic?";
			cin>>urType[count];
		}
	}
	
	void dreamers()
	{
		for (size_t count = 0; count < num_users; ++count)
		{
			if(urType[count] !="Fairytales")
			{
				cout<<"Not a believer";
			}
			else
			{
				cout<<"Name: "<< urTypeN[count] << endl;
			}
		}
	}
};

int main()
{
	Oz el;
	el.getOzDetails();
	el.dreamers();
}


Thanx a milli. It worked.
Topic archived. No new replies allowed.