Unexpected Output

In my code below, if you enter an incorrect age, it starts the program over.. After you enter a correct input, it will output your name and age, then it will show your name and age = 0... Whatever the age is set to in the private class is what it outputs. Any ideas?

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
//Colton Boyd
//Homework 10.2
#include "stdafx.h" 
#include <cstdlib>
#include <iostream>
#include <string>
#include <cstring>
#include <cmath> 
#include <iomanip>
#include <fstream>
#include <cassert>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <algorithm>
#include <sstream>
#include <time.h>
#include <vector>
#include <locale>
#include <windows.h>

using namespace std;

int getage(struct student age);
int getlevel(struct student level);

class student
{
public:
	void setname(string x){
		name = x;
	}
	string getname(){
		return name;
	}
	void setage(int y){
		age = y;
	}
	int getage(){
		return age;
	}

private:
	string name;
	int age=0;
	char level;
};

void main()
{
	string myname = "Colton Boyd";
	cout << myname << endl << endl;

	string name1;
	string mystring;
	int age1=0;

	student student1;
	cout << "Enter your name-->";
	cin >> name1;
	student1.setname(name1);
		cout << "Enter your age-->";
		cin >> age1;
		if (age1 < 0 || age1  > 120){
			main();
		}
		else
		{
			student1.setage(age1);

	}
	cout << endl << endl;

	cout << student1.getname() << " " << student1.getage() << endl;

	system("Pause");

}

a) main must return an int. void is non standard and considered bad code
b) dont use using namespace std; or with any namespace for that matter. instead use an explicit qualifier (ie std::cout)
c) i would suggest better names for your variables. they should express what you mean. i have no idea what mystring might do.
d) you can't call main, and you shouldnt. idk why you would even need to. just put it in a loop until it is correct
e) your else bracket is off. it looks like its closing student1
f) system is evil. never use it. ever
g) whats up with the two prototypes up top? you dont use them, and you already have members for extracting that info. also, in c++ it isnt neccesary to qualify data types with struct
h) why are you including all of those libraries?
a) Okay, fixed that..
b) I have to use it, this is for my c++ class and it's part of the requirements for each program.
c) I meant to delete mystring before I posted this..
d) What kind of loop?
e) Fixed that.
f) Gotcha!
g) Meant to delete those also. Did I qualify something with struct?
h) Again, requirement for the class.
a) ok
b) i didnt say use the namespace called std. i said that specific line is bad
c) see a
d) while, do-while, for...
e) see c
f) good!
g) no. its just from c
h) you really have to include <algorithm>?
Topic archived. No new replies allowed.