Hello Usama911,
In addition to what
Furry Guy has said.
Not having a closing brace ( } ) for "main" makes the functions that should follow "main" be part of main and that does not work.
It looks like it makes no difference where you have put the prototypes, but personally I like to put them before any actual functions. Also based on the way you have written your program I would put the "welstudent" function after "main" with the others.
When you get to "main" I have done this so far:
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
|
#include <iostream>
//using namespace std; // <--- Best not to use. // <--- Uncomment if you feel that you must use this.
// The most recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/
// Prototypes.
int calfee();
int calgpa();
int section();
void welstudent()
{
std::cout << "\n Welcome to University Dear Students" << std::endl;
}
int main()
{
int fee{}, gpa{}, choice{};
welstudent();
std::cout << "\n 1 to Claculate"
<< "\n 2 to Calculate GPA"
<< "\n 3 to Allocate Section"
<< "\n 4 to Exit"
<< "\n Enter selection: ";
std::cin >> choice;
switch (choice)
{
case 1:
fee = calfee();
break;
|
It is a good idea to initialize your variables when you define them. If only for the peace of mind that they do not contain garbage before they are used.
I also changes the names to something that makes more sense. It doe make the code easier to follow and understand.
So far I have only dealt with "case 1". In your original code you sent the variable "fee" to the function. At the function
int calfee(int nofc)
not only do you change the name of this variable it is for a different use, but it is only a copy of "fee" and any changes are lost when the function ends. Also the function returns an "int", (fee), that you never capture back in "main". Hard to understand what the point is of the return value since the function does everything that you need.
Also you wrote:
1 2 3 4
|
int nofc, fee1 = 21000;
int calfee(int nofc)
{
|
First you should try to avoid using global variables as it would allow anything that follows to be able to change their values and then it becomes hard to find where it went wrong.
Next defining "nofc" as a parameter makes this variable local to the function which overshadows the global variable and thus it would not receive any new value.
These variables are better defined inside the function where they are used.
What would work better is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
int calfee()
{
constexpr int FEE1{ 21000 };
int numOfCoursec{}, fee{};
std::cout << "\n Enter no of Courses: ";
std::cin >> numOfCoursec;
fee = FEE1 * numOfCoursec;
std::cout << "\n Fee of the course(s) is $" << fee << ".00" << std::endl;
return fee;
}
|
To me "FEE1" feels like it should be a constant value that the function should never try to change.
After running the "calfee" function a few times I concluded the it would just as well this way
void calfee()
taking no parameters and returning nothing. Since a returned value is never used in "main" at this time.
The same concepts would apply to the "calgpa" function.
A quick look at the "section" function looks OK for now although I have not tested that part yet.
Andy