Functions does not return new value..

New to this site!

In my program below, in the getage and get level functions, if an incorrect input is entered, then the correct one is entered after, it still returns the bad input back to main.. Any suggestions?

Thanks ahead of time!

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
#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>

using namespace std;

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

struct student
{
	string name;
	int age;
	char level;
};

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

	struct student student1;
	cout << "Enter your name-->";
	cin >> student1.name;
	student1.age = getage(student1);	
	student1.level = getlevel(student1);

	system("cls");

	cout << student1.name << setw(10) << student1.age << setw(5) << student1.level << endl << endl;

	system("pause");
}

int getage(student a)
{

		cout << "Enter your age-->";
		cin >> a.age;

		if (a.age < 0){
			cout << "Incorrect input for age, please try again.." << endl;
			getage(a);
		}
		else if (a.age >120){
			cout << "Incorrect input for age, please try again.." << endl;
			getage(a);
		}
		else {
			
		}
		return a.age;
}

int getlevel(student l)
{
	cout << "Enter your grade level, in terms of:" << endl << "f for freshman., s for sophomore, j for junior, and r for senior.";
	cin >> l.level;
        cout << endl;
	switch (l.level)
	{
	case 'f':
		cout << "You are a freshman.";
		break;
	case 's':
		cout << "You are a sophomore.";
		break;
	case 'j': 
		cout << "You are a junior.";
		break;
	case 'r':
		cout << "You are a senior.";
		break;
	default:
		cout << endl << "You didn't enter a valid selection, please try again." << endl << endl;
		getlevel(l);
		break;
	}
	_sleep(1000);
	return toupper(l.level);
}
Last edited on
lines 55-57. If a.age is < 0, you make a recursive call to getage (). That's fine except for the fact the getage() returns the age, which you discard at line 57. Line 57 should read:
1
2
 
  return getage(a);


Lines 59-61: Same problem.
Line 90: Same problem with level.

Okay, so after that, how do I get another input?
The getlevel function is currently returning an int but level is a char - did you mean that function to return char instead?
Yes, it returns it as a char back to the main function, but yes I need to change that to void. Thanks!
how do I get another input?

Use a loop in main.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{	string myname = "Colton Boyd";
	cout << myname << endl << endl;

	struct student student1;
	while (true)
	{   cout << "Enter your name-->";
                     cin >> student1.name;
		if (student1.name.empty())
			break;
		student1.age = getage(student1);	
		student1.level = getlevel(student1);
		system("cls");
		cout << student1.name << setw(10) << student1.age << setw(5) << student1.level << endl << endl;
	}
	system("pause");
}


BTW, you're including way too many headers. Only include the ones you need (string, iostream,iomanip).




Last edited on
So just redo the whole process?

My teacher makes me use all of those... I know, only a few are used.
redo the whole process?

No. Just add a loop, which is straight-forward.

The while at line 6. The { at line 7.
The if/break at lines 9 and 10.
The } at line 15.

Everything else in main is the same.




Topic archived. No new replies allowed.