Reputation: 11
Good day everyone
I am new to programming and C++.
I have been having some trouble with my program. I want the function culcAverageYearMark to use the variables from the function getMarks but no matter what I have tried it always throw any error like "expected primary expression before 'int' " I have tried everything that I possibly can but no lucky.
#include <iostream>
using namespace std;
void studentDetails()
{
string name, surName, schoolName;
cout << "Enter your name: \n";
cin >> name;
cout << "Enter your surname: \n";
cin >> surName;
cout << "Enter your school name: \n";
cin >> schoolName;
}
void getMarks()
{
int english, mathematics, lifeOrientation, history, computerLiteracy, geography;
cout << "Enter your mark for English: \n";
cin >> english;
while (!(english >0 && english <= 100))
{
cout << "Invalid mark \n";
cout << "Please enter your mark for English: \n";
cin >> english;
}
cout << "Enter your mark for Mathematics: \n";
cin >> mathematics;
while (!(mathematics >0 && mathematics <= 100))
{
cout << "Invalid mark \n";
cout << "Please enter your mark for Mathematics: \n";
cin >> mathematics;
}
cout << "Enter your mark for Life Orientation: \n";
cin >> lifeOrientation;
while (!(lifeOrientation >0 && lifeOrientation <= 100))
{
cout << "Invalid mark \n";
cout << "Please enter your mark for lifeOrientation: \n";
cin >> lifeOrientation;
}
cout << "Enter your mark for History: \n";
cin >> history;
while (!(history >0 && history <= 100))
{
cout << "Invalid mark \n";
cout << "Please enter your mark for History: \n";
cin >> history;
}
cout << "Enter your mark for Computer Literacy: \n";
cin >> computerLiteracy;
while (!(computerLiteracy >0 && computerLiteracy <= 100))
{
cout << "Invalid mark \n";
cout << "Please enter your mark for Computer Literacy: \n";
cin >> computerLiteracy;
}
cout << "Enter your mark for Geography: \n";
cin >> geography;
while (!(geography >0 && geography <= 100))
{
cout << "Invalid mark \n";
cout << "Please enter your mark for Geography: \n";
cin >> geography;
}
}
void calcAverageYearMark();
int main()
{
studentDetails();
getMarks();
calcAverageYearMark();
return 0;
}
void calcAverageYearMark()
{
getMarks(int english, int mathematics, int lifeOrientation, int history, int computerLiteracy, int geography)
float average = (english + mathematics + lifeOrientation + history + computerLiteracy + geography)/6;
}
Upvotes: 1
Views: 136
Reputation: 71
Yes this is a scope issue.
If you want to calculate something in one function then get that value and send it to another function you want to use this code example...
int calculationA()
{
int a;
//do calulation to a
return a;
}
int calculateB(int b)
{
//b was sent from the main method
//we can use it here
return b;
}
int main()
{
int x = calculationA();
int output = calculationB(x);
//output variable now contains the caclulations after going through both methods
return 0;
}
you cannot access the variable without either making it global or sending it as a parameter. You are experiencing what is called a scope error
Upvotes: 1
Reputation: 85
The problem you are having really has a couple different causes.
First of all, your function get_marks()
has a void
return type. That means that it does not return any value when it is run. If you want to get a value out of it you will need to give it a return type. For you needs it would need to return either a reference to an array or a pointer. If you don't know what those are then you should rethink how you are structuring your code so that you aren't getting multiple values out of a single function.
Another big issue is that you are calling get_marks
incorrectly. The way you have defined it does not take any argument, so you will run into compilation issues because you are trying to pass it arguments when it does not expect any. You are also not properly passing arguments in the first place. You should not put a datatype before arguments.
Here is a simple rewrite of your program that should get the results you want:
#include <iostream>
using namespace std;
void studentDetails()
{
string name, surName, schoolName;
cout << "Enter your name: \n";
cin >> name;
cout << "Enter your surname: \n";
cin >> surName;
cout << "Enter your school name: \n";
cin >> schoolName;
}
int getMark(const string &class)
{
int english, mathematics, lifeOrientation, history, computerLiteracy, geography;
cout << "Enter your mark for " << class << " \n";
cin >> val;
while (!(val >0 && val <= 100))
{
cout << "Invalid mark \n";
cout << "Please enter your mark for English: \n";
cin >> val;
}
return val
}
float calcAverageYearMark()
{
int english, mathematics, lifeOrientation, history, computerLiteracy, geography;
english = getMark("english");
mathematics = getMark("mathematics");
lifeOrientation = getMark("lifeOrientation");
history = getMark("history");
computerLiteracy = getMark("computerLiteracy");
geography = getMark("geography");
float average = (english + mathematics + lifeOrientation + history + computerLiteracy + geography)/6.0;
return average;
}
int main()
{
studentDetails();
getMarks();
calcAverageYearMark();
return 0;
}
Please note that I have not tested this code and it is more meant to demonstrate a better structure to accomplish what you are trying to do.
Upvotes: 0