Loops and Functions

I'm creating a small program that asks you to type in a number to display a characters Bio.

It works great, however I don't want the program to end once you've viewed a bio..

I want to ask the user if they would like to view another character bio.
So I made a function to ask the user if they would like to view another but when I call it, the program already ends and asks me to press any key to continue. Have I used the function wrong?

and also when the user enters "y" I want to clear the screen and reset the layout to the default question again.. I'm assuming I need a loop here but how would I go about that? I know how to use loops but it's my code I'm unsure what I need to loop exactly (sorry if that sounds poorly explained).

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>
#include <string>

//create a class called Bio
class Bio
{
//give access to variables outside the class
public:

	std::string name; //create a string variable called name
	int age;		  //create an integer variable called age
	int weight;		  //create an  integer variable called weight
	float height;	  //create a  float variable called height
	std::string code;

//Constructor 1
	Bio(std::string name2, int age2, int weight2, float height2)
	{

	name = name2;
	age = age2;
	weight = weight2;
	height = height2;

	}

//Constructor 2
	Bio(std::string name3, int age3, float height3, int weight3)
	{

	name = name3;
	age = age3;
	weight = weight3;
	height = height3;

	}

//Constructor 3
	Bio(std::string Sname, int age4, int weight4, float height4, std::string cheat)
	{

	code = cheat;
	name = Sname;
	age = age4;
	weight = weight4;
	height = height4;
	
	}


};

using namespace std;
void selection();
std::string choice;
int main()
{

Bio obj("King Rhizord",350,510,6.11);
Bio obj2("Gozo",210,260,5.8);
Bio obj3("Syxx",103,135,5.5,"Up,Down,Left,Left,Down,Right,Down");

int CharBio;

cout << "A small program to show character bios" << endl;

cout << endl;

cout << "1) King Rhizord\n" << "2) Gozo\n" << "3) Syxx\n" << 
"4) Quit Program\n" << endl;

cout << "Please enter a number to view a characters Bio: ";

cin >> CharBio;

cout << endl;

if(CharBio == 1)
{
cout << "Name: " << obj.name << "\nAge: " << obj.age << "\nWeight: " <<
obj.weight << "\nHeight: " << obj.height << endl;
cout << endl;
selection();

}
else if(CharBio == 2)
{
cout << "Name: " << obj2.name << "\nAge: " << obj2.age << "\nWeight: " <<
obj2.weight << "\nHeight: " << obj2.height << endl;
cout << endl;
selection();

}
else if(CharBio == 3)
{
cout << "Name: " << obj3.name << "\nAge: " << obj3.age << "\nWeight: " <<
obj3.weight << "\nHeight: " << obj3.height << "\nCheat Code: " << 
obj3.code << endl;
cout << endl;
selection();

}
else if(CharBio == 4)
{
exit(0);
}
else
{
cout << "You have entered an incorrect number!" << endl;
}
cout << endl;

	system("pause");
	return 0;
}

void selection()
{
cout << "Would you like to view another characters Bio (y/n): ";
getline(cin, choice);
}

Last edited on
Well first off I think you have more constructors than necessary. Your first two constructors do the same thing.

1
2
3
4
5
6
7
8
9
	Bio(std::string name2, int age2, int weight2, float height2,string cheat=" ";)
	{

	name = name2;
	age = age2;
	weight = weight2;
	height = height2;
        code=cheat;
	}


You can assign a default value for the cheat variable so you can use the constructor without providing that particular argument,and if a cheat argument is provided it will just override the default assignment.

Calling the selection function only brings up the prompt, it does not loop the program.

Now in order for the user to go back to the menu screen after a selection has been made, you will need a while or a do-while loop. I suggest a do-while.

And you should probably get an IDE which does not require "system(pause)" or something similar in order to view output. I suggest Dev-C++ or visual studio. You should also look into switch statements for selection logic like this.

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <string>

//create a class called Bio
class Bio
{
//give access to variables outside the class
public:

	std::string name; //create a string variable called name
	int age;		  //create an integer variable called age
	int weight;		  //create an  integer variable called weight
	float height;	  //create a  float variable called height
	std::string code;


//Constructor 3
	Bio(std::string Sname, int age4, int weight4, float height4, std::string cheat=" ")
	{

	code = cheat;
	name = Sname;
	age = age4;
	weight = weight4;
	height = height4;
	
	}


};

using namespace std;
string Choice;
int main()
{

Bio obj("King Rhizord",350,510,6.11);
Bio obj2("Gozo",210,260,5.8);
Bio obj3("Syxx",103,135,5.5,"Up,Down,Left,Left,Down,Right,Down");

int CharBio;
do
{
cout<<"\n";
cout << "A small program to show character bios" << endl;

cout << endl;

cout << "1) King Rhizord\n" << "2) Gozo\n" << "3) Syxx\n" << 
"4) Quit Program\n" << endl;

cout << "Please enter a number to view a characters Bio: ";

cin >> CharBio;

cout << endl;

if(CharBio == 1)
{
cout << "Name: " << obj.name << "\nAge: " << obj.age << "\nWeight: " <<
obj.weight << "\nHeight: " << obj.height << endl;
cout << endl;
}
else if(CharBio == 2)
{
cout << "Name: " << obj2.name << "\nAge: " << obj2.age << "\nWeight: " <<
obj2.weight << "\nHeight: " << obj2.height << endl;
cout << endl;
}
else if(CharBio == 3)
{
cout << "Name: " << obj3.name << "\nAge: " << obj3.age << "\nWeight: " <<
obj3.weight << "\nHeight: " << obj3.height << "\nCheat Code: " << 
obj3.code << endl;
cout << endl;
}
else if(CharBio == 4)
{
break;//exits any loop
}
else
{
cout << "You have entered an incorrect number!" << endl;
}


cout << "Would you like to view another characters Bio (y/n): ";
cin>>Choice;
}while(Choice=="y");

system("pause");
return 0;
}


Last edited on
You have been very helpful thank you for taking the time to reply!
Topic archived. No new replies allowed.