Stuck on c++ Homework

Hey I have a homework assignment that seemed to be going well but then i got stuck. Here is the assignment


WProgram Description:

Write a program to generate a report based on input received from a text file. Suppose the input text file student_status.txt contains the student’s name (lastName, firstName middleName), id, number of credits earned as follows :

Doe, John K.
3460 25
Andrews, Susan S.
3987 72
Monroe, Marylin
2298 87
Gaston, Arthur C.
2894 110

Generate the output in the following format :

John K. Doe 3460 25 Freshman
Susan S. Andrews 3987 40 Sophomore
Marylin Monroe 2298 87 Junior
Arthur C. Gaston 2894 110 Senior

The program must be written to use the enum class_level :

enum class_level {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR } ;

and define two namespace globalTypes (tys and fys) for the function :

class_level deriveClassLevel(int num_of_credits) ;

The function deriveClassLevel should derive the class_level of the student based on the number of credits earned.

The first namespace globalType tys should derive the class level based on a two year school policy.
and the second namespace globalType fys should derive the class level based on a four year school policy.

So I basically did it in parts and got everything working and then had to make the namespace so I had this:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

enum class_level { FRESHMAN, SOPHOMORE, JUNIOR, SENIOR };

int main()
{
string uplevel, name;
int noc, id, i;


class_level level = FRESHMAN;

ifstream transferSchoolFile;
transferSchoolFile.open("student_status.txt", ios::in);



//check for error
if (transferSchoolFile.fail()){
cerr << "Error opening file" << endl;
exit(1);

}

for (i = 0; i < 4; i++)
{


getline(transferSchoolFile, name);
transferSchoolFile >> id >> noc;
transferSchoolFile.ignore();


if (noc < 30)
{
level = FRESHMAN;
}
else if (noc < 60 && noc > 29)
{
level = SOPHOMORE;
}
else if (noc > 59 && noc < 90)
{
level = JUNIOR;
}
else
level = SENIOR;
switch (level)
{
case FRESHMAN:
uplevel = "Freshman";
break;

case SOPHOMORE:
uplevel = "Sophomore";
break;

case JUNIOR:
uplevel = "Junior";
break;

default:
uplevel = "Senior";



}


cout << name << " " << id << " " << noc << " " << uplevel << endl;

}

system("pause");
return 0;
}

I know I have to clean it up and change it but it ran like it was suppose to. Then I tried adding the global namespaces and I this:
#include <iostream>
#include <fstream>
#include <string>



using namespace std;

enum class_level { FRESHMAN, SOPHOMORE, JUNIOR, SENIOR };
class_level classLevel;


namespace tys

{

class_level deriveClassLevel(int noc)
{
if (noc >= 0 && noc <= 29)
{
classLevel = FRESHMAN;

}
else
{
classLevel = SOPHOMORE;


}




}
namespace fys
{
class_level deriveClassLevel(int noc)
{
if (noc >= 0 && noc <= 29)
{
classLevel = FRESHMAN;

}
else if (noc >= 30 && noc <= 59)
{
classLevel = SOPHOMORE;

}
else if (noc >= 60 && noc <= 89)
{
classLevel = JUNIOR;

}
else
{
classLevel = SENIOR;

}


}
using namespace tys;
int main()
{
string uplevel, name;
int noc, id, i;

class_level level = FRESHMAN;

ifstream transferSchoolFile;
transferSchoolFile.open("student_status.txt", ios::in);


//check for error
if (transferSchoolFile.fail()){
cerr << "Error opening file" << endl;
exit(1);

}

for (i = 0; i < 4; i++)
{

getline(transferSchoolFile, name);
transferSchoolFile >> id >> noc;
transferSchoolFile.ignore();

switch (level)
{
case FRESHMAN:
uplevel = "Freshman";
break;

case SOPHOMORE:
uplevel = "Sophomore";
break;

case JUNIOR:
uplevel = "Junior";
break;

default:
uplevel = "Senior";



}

cout << name << " " << id << " " << noc << " " << uplevel << endl;
}

system("pause");
return 0;
}
}
}
with this i keep getting an error saying tys::deriveClassLevel: must return a value and tys::fys::deriveClassLevel: must return a value. I have been messing around with this part and struggling I thought I used the namespace to run the if statements with the criteria for the years of school. Basically I have been stuck for awhile and trying to change things around but I cant seem to get it to work. If anyone could help me get past this road block I would greatly appericate it since I dont have class till Tuesday and I dont want to wait that long for help.
Last edited on
You forgot to put return classLevel; at the end of those functions. When you tell your compiler that a function is going to return something, it expects you to return it.
There's an awful lot of benefit in taking time to format your code. . .

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
namespaces and I this:
#include <iostream>
#include <fstream>
#include <string>



using namespace std;

enum class_level { FRESHMAN, SOPHOMORE, JUNIOR, SENIOR };
class_level classLevel;

namespace tys
		
{
	Class_level deriveClassLevel(int noc)
	{
		if (noc >= 0 && noc <= 29) 
		{
			classLevel = FRESHMAN;
		}
		else 
		{
			classLevel = SOPHOMORE;
		}
		// in your function declarations you said you were going to return a Class_level. . .where is it?
	}


namespace fys
{
	class_level deriveClassLevel(int noc)
	{
		if (noc >= 0 && noc <= 29)
		{
			classLevel = FRESHMAN;
		}
		else if (noc >= 30 && noc <= 59)
		{
			classLevel = SOPHOMORE;
		}
		else if (noc >= 60 && noc <= 89)
		{
			classLevel = JUNIOR;
		}
		else
		{
			classLevel = SENIOR;

		}

	}


using namespace tys;


It's trivial, when the code is formatted, to see glaring omissions. . .
Topic archived. No new replies allowed.