Jul 24, 2014 at 8:51pm UTC
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 Jul 24, 2014 at 8:54pm UTC
Jul 24, 2014 at 9:06pm UTC
Okay, so after that, how do I get another input?
Jul 24, 2014 at 9:09pm UTC
The getlevel function is currently returning an int but level is a char - did you mean that function to return char instead?
Jul 24, 2014 at 9:10pm UTC
Yes, it returns it as a char back to the main function, but yes I need to change that to void. Thanks!
Jul 25, 2014 at 1:35pm UTC
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 Jul 26, 2014 at 10:01pm UTC
Jul 26, 2014 at 9:02pm UTC
So just redo the whole process?
My teacher makes me use all of those... I know, only a few are used.