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;
}
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
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.
Related
I'm writing a code to enter subjects' information where I put void function and array as an object. But not sure when I wanna loop it, it doesn't come until the end. Have a look at the code.
void calculateCGPA::getGPA() {
cout << "Enter the the name of the subject: ";
cin >> subjectName;
cout << "Enter the credit hour:";
cin >> credithour;
cout << "Enter the grade: ";
cin >> grade;
}
int main () {
for (year=1; year<=4; year++) {
for (sem=1; sem<=2; sem++) {
cout << "Enter total subject you take in Year " << year << " Semester " << sem <<": ";
cin >> totalSubjectSem;
calculateCGPA ob[totalSubjectSem];
for(int i = 1; i <= totalSubjectSem; i++) {
cout << "Subject " << i << ": \n";
ob[i].getGPA();
}
}
}
return 0;
}
Here's the error. You can see the compiler only shows until entering the subject name but credit hour and grade are omitted. What should I do?
Expected: It should everything in void function until 3 (since I put 3) and then start over again "Enter total subject you take in Year 1 sem 2" but it also omits that
Take note that in C++ 0 is the first element, and n-1 is the last element. By looping to n, you cause a buffer overflow, hence resulting an error.
A solution would be as follows
void calculateCGPA::getGPA() {
cout << "Enter the the name of the subject: ";
cin >> subjectName;
cout << "Enter the credit hour:";
cin >> credithour;
cout << "Enter the grade: ";
cin >> grade;
}
int main () {
for (year=0; year<4; year++) {
for (sem=0; sem<2; sem++) {
cout << "Enter total subject you take in Year " << year << " Semester " << sem <<": ";
cin >> totalSubjectSem;
calculateCGPA ob[totalSubjectSem];
for(int i = 0; i < totalSubjectSem; i++) {
cout << "Subject " << i << ": \n";
ob[i].getGPA();
}
}
}
}
calculateCGPA ob[totalSubjectSem];
It's GCC extension called Variable-length automatic arrays and not valid C++ (because totalSubjectSem is not const). Use std::vector<calculateCGPA> instead.
for(int i = 1; i <= totalSubjectSem; i++) {
Indices start from 0 and going to array_length - 1. On last iteration you reading out of array bounds and program is crashing.
I have the following code
for (int i = 0; i < courses; i++)
{
cout << "Please Enter Letter Grade: ";
cin >> grade1;
cout << "Please Enter Course Weighting: ";
cin >> weight1;
}
Now, lets say the loop runs 3 times and the values entered by the user for grade1 and weight1 are different each time. I want to store these different values so I can do some calculations with them. How would I proceed to do so?
Here is how use an array:
int grade[courses]; // this is an array with size of courses
double weight[courses];
for (int i = 0; i < courses; i++) {
cout << "Please Enter Letter Grade: ";
cin >> grade[i];
cout << "Please Enter Course Weighting: ";
cin >> weight[i];
}
Array is collection of data of the same type stored sequentially in computer memory. Syntax for array is as follow:
<type> <name>[<size>];
for example
int numberOfStudents[100];
is int array with maximum of 100 elements.
Hope This Helps
group grade and weight into a struct and store them in a vector.
code: (doesnt handle all potential errors)
#include <iostream>
#include <vector>
struct grade_weight
{
int grade;
int weight;
};
int main()
{
int courses = 5;
std::vector<grade_weight> result;
// potential optimization if you want
//result.reserve(courses);
for (int i = 0; i < courses; i++)
{
int grade, weight;
std::cout << "Please Enter Letter Grade: ";
std::cin >> grade;
std::cout << "Please Enter Course Weighting: ";
std::cin >> weight;
result.push_back({grade, weight});
}
std::cout << "you input\n";
for(auto& gw: result)
{
std::cout << "grade: " << gw.grade << ", weight: " << gw.weight << '\n';
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
the program works fine and executes all options, but in the 3rd and 4th option, in 3rd when i search for a student the program doesn't go beyond the data input part while in 4th when i search for data and print it, it gives garbage values in the variables with int datatype while doesn't display string or char type variables. any help would be much appreciated. EDIT: sorry for the inconvenience, I've now separated the functions form the main code. Hope its its more understandable now.
#include<iostream>
#include<string>
using namespace std;
int ncr;
int me,n=0,u,y,l;
char opt1,opt2,opt3,opt4;
struct student
{
string Name;
int DoB;
int Age;
string Address;
string Course[5];
char grade[5];
};
void add();
void remove();
void update();
void search();
int main()
{ int opt;
student s[n];
while(1)
{
cout<<" select what you want to do \n\n 1. Add new record \n2. Remove record\n3. Update Record\n4. Search for particular student\n" ;
cin>>opt;
if(opt==1)
{add();}
else if(opt==2)
{remove();}
else if(opt==3)
{update();}
else if(opt==4)
{search();}
}
}
.
void add(){
n++;
student s[n];
do
{ cout<<"enter name of student ";
cin>>s[n-1].Name;
cout<<"enter DoB of student ";
cin>>s[n-1].DoB;
cout<<"enter age of student ";
cin>>s[n-1].Age;
cout<<"enter address of student ";
cin>>s[n-1].Address;
cout<<"enter courses and grades (max 5) \n";
for(int x=0;x<5;x++){
cout<<"course:";
cin>>s[n-1].Course[x];
cout<<"grade:";
cin>> s[n-1].grade[x];}
cout<<"Roll No. : "<<n<<"\nName :"<<s[n-1].Name<<"\nDoB :"<<s[n-1].DoB<<"\nAge :"<<s[n-1].Age<<"\nAddress :"<<s[n-1].Address;
for(int x=0;x<5;x++){
cout<<"\n"<<s[n-1].Course[x];
cout<<" grade : "<< s[n-1].grade[x]<<"\n";}
cout<<"repeat for another student 'y' or 'n' ?";
cin>>opt1;
}
while(opt1=='y'|| opt1=='Y');
}
.
void remove(){student s[n];
do
{
cout<<"enter student roll no to remove data";cin>>l;
for(int i=l;i<n;i++)
{ s[l-1].Name=s[l].Name;
s[l-1].DoB= s[l].DoB;
s[l-1].Age=s[l].Age;
s[l-1].Address=s[l].Address;
for(int j=0;j<5;j++){
s[l-1].Course[j]=s[l].Course[j];
s[l-1].grade[j]=s[l].grade[j];}
}
cout<<"Record Removed\n";
n--;
cout<<"repeat ? 'y' or 'n' ";
cin>>opt2;
}
while(opt2 == 'y' || opt2 == 'Y');}
.
void update(){
student s[n];
do
{
cout<<"enter the roll no of student you want to update data";
cin>>u;
cout<<"Roll No. : "<<u;
cout<<"\nName : ";
cout<<s[u-1].Name;
cout<<"\nDoB : ";
cout<<s[u-1].DoB;
cout<<"\nAge : ";
cout<<s[u-1].Age;
cout<<"\nAddress :";
cout<<s[u-1].Address;
cout<<"\nCourses and Grades\n";
for(int r=0;r<5;r++)
{
cout<<s[u-1].Course[r];
cout<<"\t"<< s[u-1].grade[r]<<endl;
}
cout<<"enter name of student ";
cin>>s[u-1].Name;
cout<<"enter DoB of student ";
cin>>s[u-1].DoB;
cout<<"enter age of student ";
cin>>s[u-1].Age;
cout<<"enter address of student ";
cin>>s[u-1].Address;
cout<<"enter courses and grades (max 5) \n";
for(int x=0;x<5;x++){
cout<<"course:";
cin>>s[u-1].Course[x];
cout<<"grade:";
cin>> s[u-1].grade[x];}
cout<<"Roll No. : "<<u;
cout<<"\nName : ";
cout<<s[u-1].Name;
cout<<"\nDoB : ";
cout<<s[u-1].DoB;
cout<<"\nAge : ";
cout<<s[u-1].Age;
cout<<"\nAddress :";
cout<<s[u-1].Address;
cout<<"\nCourses and Grades\n";
for(int r=0;r<5;r++)
{ cout<<s[u-1].Course[r];
cout<<"\t"<< s[u-1].grade[r]<<endl; }
cout<<"repeat ? 'y' or 'n' ";
cin>>opt3;
}
while(opt3 == 'y' || opt3 == 'Y');
}
.
void search(){
student s[n];
do
{
cout<<"enter the roll no of student you want to search data of";
cin>>y;
cout<<"Roll No. : "<<n<<"\nName :"<<s[y-1].Name<<"\nDoB :"<<s[y-1].DoB<<"\nAge :"<<s[y-1].Age<<"\nAddress :"<<s[y-1].Address<<"\nCourses and Grades\n";
for(int r=0;r<5;r++)
{
cout<<s[y-1].Course[r];
cout<<"\t"<< s[y-1].grade[r]<<endl;
}
cout<<"repeat ? 'y' or 'n' ";
cin>>opt4;
}
while(opt4 == 'y' || opt4 == 'Y');}
This should look a bit more like a code intended for humans to read :)
#include<iostream>
#include<string>
using namespace std;
int ncr;
int me,n=0,u,y,l;
char opt1,opt2,opt3,opt4;
struct student
{
string Name;
int DoB;
int Age;
string Address;
string Course[5];
char grade[5];
};
void add();
void remove();
void update();
void search();
int main()
{
int opt;
student s[n];
while(1)
{
cout << " select what you want to do \n\n 1. Add new record \n2. Remove record\n3. Update Record\n4. Search for particular student\n" ;
cin >> opt;
if(opt==1)
{
add();
}
else if(opt==2)
{
remove();
}
else if(opt==3)
{
update();
}
else if(opt==4)
{
search();
}
}
}
void add()
{
n++;
student s[n];
do
{
cout << "enter name of student ";
cin >> s[n-1].Name;
cout << "enter DoB of student ";
cin >> s[n-1].DoB;
cout << "enter age of student ";
cin >> s[n-1].Age;
cout << "enter address of student ";
cin >> s[n-1].Address;
cout << "enter courses and grades (max 5) \n";
for(int x=0;x<5;x++)
{
cout<<"course:";
cin>>s[n-1].Course[x];
cout<<"grade:";
cin>> s[n-1].grade[x];
}
cout << "Roll No. : " << n << "\nName :" << s[n-1].Name << "\nDoB :" << s[n-1].DoB << "\nAge :" << s[n-1].Age << "\nAddress :" << s[n-1].Address;
for( int x = 0; x < 5; x++ )
{
cout << "\n"<<s[n-1].Course[x];
cout << " grade : "<< s[n-1].grade[x]<<"\n";}
cout << "repeat for another student 'y' or 'n' ?";
cin >> opt1;
}
while(opt1=='y'|| opt1=='Y');
}
void remove()
{
student s[n];
do
{
cout << "enter student roll no to remove data";
cin >> l;
for( int i=l; i < n; i++ )
{
s[l-1].Name=s[l].Name;
s[l-1].DoB= s[l].DoB;
s[l-1].Age=s[l].Age;
s[l-1].Address=s[l].Address;
for(int j=0;j<5;j++)
{
s[l-1].Course[j]=s[l].Course[j];
s[l-1].grade[j]=s[l].grade[j];
}
}
cout<<"Record Removed\n";
n--;
cout << "repeat ? 'y' or 'n' ";
cin >> opt2;
}
while(opt2 == 'y' || opt2 == 'Y');
}
void update()
{
student s[n];
do
{
cout << "enter the roll no of student you want to update data";
cin >> u;
cout << "Roll No. : " << u;
cout << "\nName : ";
cout << s[u-1].Name;
cout << "\nDoB : ";
cout << s[u-1].DoB;
cout << "\nAge : ";
cout << s[u-1].Age;
cout << "\nAddress :";
cout << s[u-1].Address;
cout << "\nCourses and Grades\n";
for( int r=0; r < 5; r++ )
{
cout << s[u-1].Course[r];
cout << "\t"<< s[u-1].grade[r] << endl;
}
cout << "enter name of student ";
cin >> s[u-1].Name;
cout << "enter DoB of student ";
cin >> s[u-1].DoB;
cout << "enter age of student ";
cin >> s[u-1].Age;
cout << "enter address of student ";
cin >> s[u-1].Address;
cout << "enter courses and grades (max 5) \n";
for(int x=0; x < 5; x++ )
{
cout<<"course:";
cin>>s[u-1].Course[x];
cout<<"grade:";
cin>> s[u-1].grade[x];
}
cout << "Roll No. : " << u;
cout << "\nName : ";
cout << s[u-1].Name;
cout << "\nDoB : ";
cout << s[u-1].DoB;
cout << "\nAge : ";
cout << s[u-1].Age;
cout << "\nAddress :";
cout << s[u-1].Address;
cout << "\nCourses and Grades\n";
for( int r = 0; r < 5; r++)
{
cout << s[u-1].Course[r];
cout << "\t"<< s[u-1].grade[r]<<endl;
}
cout << "repeat ? 'y' or 'n' ";
cin >> opt3;
}
while(opt3 == 'y' || opt3 == 'Y');
}
void search()
{
student s[n];
do
{
cout << "enter the roll no of student you want to search data of";
cin >> y;
cout << "Roll No. : " << n << "\nName :" << s[y-1].Name << "\nDoB :" << s[y-1].DoB << "\nAge :" << s[y-1].Age << "\nAddress :" << s[y-1].Address << "\nCourses and Grades\n";
for( int r=0; r < 5; r++ )
{
cout << s[y-1].Course[r];
cout << "\t" << s[y-1].grade[r]<<endl;
}
cout << "repeat ? 'y' or 'n' ";
cin >> opt4;
}
while( opt4 == 'y' || opt4 == 'Y' );
}
The problem you're having is with the scope of your programme.
In the main() function you declare an array, which presumably should be holding all the records. However for every option (function add/remove/update/search) you declare a new one. These arrays are local to those functions and are being "forgotten" after the function is finished.
In order to fix that, you have to pass the original array (the one from main()) to those functions.
The next problem, which is more severe is the fact, that you can't change the size of an array in the runtime! The elements you were accessing in the 3rd option were some random bytes in the memory and this can cause a lot of trouble.
Either create an array with e.g. 100 fields ( student s[100]; ), or even better try using std::vector, which can size you can adjust according to the number of records.
Third thing, remember that formatting code is extremely important. Hopefully it was just a copy-paste problem that made your code look unreadable, but if it's not the case then try to change. Well formatted code is easier to understand and will make it easier for you to debug it.
I am writing a program and it is a long program and I have just started. I just tested it for running, Please tell me why it is not having user input:
#include <iostream>
using namespace std;
struct courses{
int CLO1, CLO2, CLO3, CLO4, CLO5;
int tcounter;
};
int main(){
cin.clear();
courses C1;
C1.CLO1;
C1.CLO2;
C1.CLO3;
int counter = 0;
char name;
cout << "Enter your Name: ";
cin >> name;
cout << "For C1, we have three CLOs that CLO1,CLO2 and CLO3. CLO1 and CLO3 are linked to PLO1 and CLO2 is linked to PLO2 " << endl;
cout << "Enter your marks in CLO1 for C1:(Out of 10) ";
cin >> C1.CLO1;
if (C1.CLO1 >= 5){
counter++;
}
cout << "Enter your marks in CLO2 for C1:(Out of 10) ";
cin >> C1.CLO2;
if (C1.CLO2 >= 5){
counter++;
}
cout << "Enter your marks in CLO3 for C1:(Out of 10) ";
cin >> C1.CLO3;
if (C1.CLO3 >= 5){
counter++;
}
cout << counter;
return 0;
}
As per me the program is fine.
Three things you need to change:
variable type of name from char to string
C1.CLO1;C1.CLO2;C1.CLO3; remove these from your code it doesn't make any sense.
Print the values and check :P
Can you tell what is the mistake? It compiles and runs ok but the program won't end.
I am using dev c++. The code is:
#include <iostream>
using namespace std;
main()
{
char num;
again:
int usr;
float area;
cout << "\nEnter 1 to calculate area of rectangle\n";
cout << "Enter 2 to calculate area of trapezoid\n";
cout << "\nEnter your choice: ";
cin >> usr;
if (usr == 1)
{
double width, length;
cout << "Enter the width of rectangle: ";
cin >> width;
cout << "Enter the length of rectangle: ";
cin >> length;
area = length * width;
cout << "The area of rectangle is: " << area;
}
if (usr == 2) {
double base1, base2, height;
cout << "Enter the base 1 of trapezoid: ";
cin >> base1;
cout << "Enter the base 2 of trapezoid: ";
cin >> base2;
cout << "Enter the height of trapezoid: ";
cin >> height;
area = (((base1 + base2) / 2) * height);
cout << "The area of trapezoid is: " << area;
}
cout << "\n\ndo you want to do another calculation?";
cin >> num;
{
goto again;
}
if (num == 'y')
{
goto again;
}
if (num == 'n') {
exit(0);
}
}
Never ever use goto, unless you have a very good reason to (which you don't).
cin >> num;{
goto again;
}
I have no idea why you wrote that, but that's the problem. If you would have written it with proper formatting, it would have looked like this
cin >> num;
{
goto again;
}
The bracket {} just changes the scope (useful for variables and such), but doesn't do anything else. The goto again; still gets executed, so you run into an endless loop.
The 2 conditions after it are fine, so just removing { goto again; } will "fix" your issue.
Remove line no 44 in your code
your code
cin >> num;{
goto again;
}
Solution remove
goto again
cin >> num;{
}
This will solve your problem