Can´t find the error
Hello everyone...
I have a problem on the last part of my codes.
This programm supposed to accept several kind of courses and its corresponding grades.
So far so good... but when it comes to display the courses and the grades and its remarks of passed or failed, the programm gives me an error message. Actually it gives me 45 errors beginning with the first if else statement.
Here are my codes so far:
#include <iostream>
#include <string>
using namespace std;
main()
{
string name, course[10], grade[10];
int x, y, n;
char response;
cout<< "Please enter your name: ";
cin>> name;
cout<< "Please enter number of subjects: ";
cin>> y;
cout<< "Hello " << name << " you have " << y << " subjects." << endl;
n=1;
for(int a=1; a<=y; a++)
{
cout<< "Please enter your course " << n << ":";
cin>> course[x];
cout<< endl;
cout<< "Please enter your Grade: ";
cin>> grade[x];
cout<< endl;
n++;
}
cout<< "Course Grade Remarks " << endl;
for(int ctr=1;ctr<=y;ctr++)
{
if (grade[ctr] < 75) // HERE THE FIRST ERROR STARTS
cout<< course[x]<< "5.0\t FAILED";
else if (grade[ctr] ==75)
cout<< course[x]<< "\t3.0\t passed";
else if (grade[ctr] <=78)
cout<< course[x]<< "\t2.75\t passed";
else if (grade[ctr] =81)
cout<< course[x]<< "\t2.5\t passed";
else if (grade[ctr] <=84)
cout<< course[x]<< "\t2.25\t passed";
else if (grade[ctr] <=87)
cout<< course[x]<< "\t2.0\t passed";
else if (grade[ctr] <=90)
cout<< course[x]<< "\t1.75\t passed";
else if (grade[ctr] <=93)
cout<< course[x]<< "\t1.5\t passed";
else if (grade[ctr] <=96)
cout<< course[x]<< "\t1.25 \t passed";
else if (grade[ctr] ==100)
cout<< course[x]<< "\t1.0\t passed";
else
cout<<"invalid input of grade";
}
cin>> response;
return 0;
}
If i set the if else statements in a comment block the programm runs ecxept the aoutput of course grade and remarks (pass/Failed).
Would be grate if somebody can help me out.
HelloMe
Junior Poster in Training
87 posts since Sep 2009
Reputation Points: 10
Solved Threads: 1
ups some spelling misstakes... never mind them :)
HelloMe
Junior Poster in Training
87 posts since Sep 2009
Reputation Points: 10
Solved Threads: 1
grade[] is an array of strings, it should be ints or doubles
jonsca
Quantitative Phrenologist
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
hmmm... when i set grades as an integer then the compiler itself gives me this error mesage:
unhendlled exception in schule.exe: 0xC000005 Access violation.
so it will not run at all :(
HelloMe
Junior Poster in Training
87 posts since Sep 2009
Reputation Points: 10
Solved Threads: 1
Well your cins and couts are using a variable X which is never changed in your program. You should use the loop variable as your index.
jonsca
Quantitative Phrenologist
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
for(int a=1; a<=y; a++)
{
cout<< "Please enter your course " << n << ":";
cin>> course[a];
cout<< endl;
cout<< "Please enter your Grade: ";
cin>> grade[a];
cout<< endl;
n++;
}
Also note that arrays start at zero.
jonsca
Quantitative Phrenologist
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
#include <iostream>
#include <string>
using namespace std;
main()
{
string name, course[10], grade[10];
int x, y, n, g;
char response;
cout<< "Please enter your name: ";
cin>> name;
cout<< "Please enter number of subjects: ";
cin>> y;
cout<< "Hello " << name << " you have " << y << " subjects." << endl;
n=1;
for(int a=1; a<=y; a++)
{
cout<< "Please enter your course " << n << ":";
cin>> course[x];
cout<< endl;
cout<< "Please enter your Grade: ";
cin>> g;
cout<< endl;
n++;
}
cout<< "Course Grade Remarks " << endl;
for(int ctr=1;ctr<=y;ctr++)
{
if (grade[ctr] < 75)
cout<< course[ctr]<< "5.0\t FAILED";
else if (grade[ctr] ==75)
cout<< course[ctr]<< "\t3.0\t passed";
else if (grade[ctr] <=78)
cout<< course[ctr]<< "\t2.75\t passed";
else if (grade[ctr] =81)
cout<< course[ctr]<< "\t2.5\t passed";
else if (grade[ctr] <=84)
cout<< course[ctr]<< "\t2.25\t passed";
else if (grade[ctr] <=87)
cout<< course[ctr]<< "\t2.0\t passed";
else if (grade[ctr] <=90)
cout<< course[ctr]<< "\t1.75\t passed";
else if (grade[ctr] <=93)
cout<< course[ctr]<< "\t1.5\t passed";
else if (grade[ctr] <=96)
cout<< course[ctr]<< "\t1.25 \t passed";
else if (grade[ctr] ==100)
cout<< course[ctr]<< "\t1.0\t passed";
else
cout<<"invalid input of grade";
}
cin>> response;
return 0;
}
i changed it to this now it changes right? But my compiler will hang itself up when i try to run this.
Sry im still new in c++
HelloMe
Junior Poster in Training
87 posts since Sep 2009
Reputation Points: 10
Solved Threads: 1
i changed the 1 to 0 but still a problem
HelloMe
Junior Poster in Training
87 posts since Sep 2009
Reputation Points: 10
Solved Threads: 1
It needs to be int grade[10]; it can't be string if you are going to do calculations with it. If you only have one g you're writing over it each time. And that course[x] should be course[a]
jonsca
Quantitative Phrenologist
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
#include <iostream>
#include <string>
using namespace std;
main()
{
string name, course[10], grade[10];
int x, y, n;
char response;
cout<< "Please enter your name: ";
cin>> name;
cout<< "Please enter number of subjects: ";
cin>> y;
cout<< "Hello " << name << " you have " << y << " subjects." << endl;
n=1;
for(int a=0 a<=y; a++)
{
cout<< "Please enter your course " << n << ":";
cin>> course[x];
cout<< endl;
cout<< "Please enter your Grade: ";
cin>> grade[x];
cout<< endl;
n++;
}
cout<< "Course Grade Remarks " << endl;
for(int ctr=1;ctr<=y;ctr++)
{
if (grade[ctr] < 75)
cout<< course[ctr]<< "5.0\t FAILED";
else if (grade[ctr] ==75)
cout<< course[ctr]<< "\t3.0\t passed";
else if (grade[ctr] <=78)
cout<< course[ctr]<< "\t2.75\t passed";
else if (grade[ctr] =81)
cout<< course[ctr]<< "\t2.5\t passed";
else if (grade[ctr] <=84)
cout<< course[ctr]<< "\t2.25\t passed";
else if (grade[ctr] <=87)
cout<< course[ctr]<< "\t2.0\t passed";
else if (grade[ctr] <=90)
cout<< course[ctr]<< "\t1.75\t passed";
else if (grade[ctr] <=93)
cout<< course[ctr]<< "\t1.5\t passed";
else if (grade[ctr] <=96)
cout<< course[ctr]<< "\t1.25 \t passed";
else if (grade[ctr] ==100)
cout<< course[ctr]<< "\t1.0\t passed";
else
cout<<"invalid input of grade";
}
cin>> response;
return 0;
}
these are my changes now... but compiler will hang itself up
HelloMe
Junior Poster in Training
87 posts since Sep 2009
Reputation Points: 10
Solved Threads: 1
it has to be for(int variable = 0;variable
jonsca
Quantitative Phrenologist
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
#include <iostream>
#include <string>
using namespace std;
main()
{
string name, course[10];
int x, y, n, grade[10];
char response;
cout<< "Please enter your name: ";
cin>> name;
cout<< "Please enter number of subjects: ";
cin>> y;
cout<< "Hello " << name << " you have " << y << " subjects." << endl;
n=1;
for(int a=0; a<=y; a++)
{
cout<< "Please enter your course " << n << ":";
cin>> course[a];
cout<< endl;
cout<< "Please enter your Grade: ";
cin>> grade[x];
cout<< endl;
n++;
}
cout<< "Course Grade Remarks " << endl;
for(int ctr=1;ctr<=y;ctr++)
{
if (grade[ctr] < 75)
cout<< course[ctr]<< "5.0\t FAILED";
else if (grade[ctr] ==75)
cout<< course[ctr]<< "\t3.0\t passed";
else if (grade[ctr] <=78)
cout<< course[ctr]<< "\t2.75\t passed";
else if (grade[ctr] =81)
cout<< course[ctr]<< "\t2.5\t passed";
else if (grade[ctr] <=84)
cout<< course[ctr]<< "\t2.25\t passed";
else if (grade[ctr] <=87)
cout<< course[ctr]<< "\t2.0\t passed";
else if (grade[ctr] <=90)
cout<< course[ctr]<< "\t1.75\t passed";
else if (grade[ctr] <=93)
cout<< course[ctr]<< "\t1.5\t passed";
else if (grade[ctr] <=96)
cout<< course[ctr]<< "\t1.25 \t passed";
else if (grade[ctr] ==100)
cout<< course[ctr]<< "\t1.0\t passed";
else
cout<<"invalid input of grade";
}
cin>> response;
return 0;
}
ok made all the changes but here again the error is again:
unhendlled exception in schule.exe: 0xC000005 Access violation.
But thanks for the help... yes there were some stupid errors :)
HelloMe
Junior Poster in Training
87 posts since Sep 2009
Reputation Points: 10
Solved Threads: 1
actually it runs but half way the during my inputs for course and grade i will receive this error:
unhendlled exception in schule.exe: 0xC000005 Access violation.
I think its maybe from the compiler itself and has nothing to do with the code?
HelloMe
Junior Poster in Training
87 posts since Sep 2009
Reputation Points: 10
Solved Threads: 1
#include <iostream>
#include <string>
using namespace std;
main()
{
string name, course[10];
int x, y, n, grade[10];
char response;
cout<< "Please enter your name: ";
cin>> name;
cout<< "Please enter number of subjects: ";
cin>> y;
cout<< "Hello " << name << " you have " << y << " subjects." << endl;
n=1;
for(int a=0; a<=y; a++)
{
cout<< "Please enter your course " << n << ":";
cin>> course[a];
cout<< endl;
cout<< "Please enter your Grade: ";
cin>> grade[x];
cout<< endl;
n++;
}
cout<< "Course Grade Remarks " << endl;
for(int ctr=1;ctr<=y;ctr++)
{
if (grade[ctr] < 75)
cout<< course[ctr]<< "5.0\t FAILED";
else if (grade[ctr] ==75)
cout<< course[ctr]<< "\t3.0\t passed";
else if (grade[ctr] <=78)
cout<< course[ctr]<< "\t2.75\t passed";
else if (grade[ctr] =81)
cout<< course[ctr]<< "\t2.5\t passed";
else if (grade[ctr] <=84)
cout<< course[ctr]<< "\t2.25\t passed";
else if (grade[ctr] <=87)
cout<< course[ctr]<< "\t2.0\t passed";
else if (grade[ctr] <=90)
cout<< course[ctr]<< "\t1.75\t passed";
else if (grade[ctr] <=93)
cout<< course[ctr]<< "\t1.5\t passed";
else if (grade[ctr] <=96)
cout<< course[ctr]<< "\t1.25 \t passed";
else if (grade[ctr] ==100)
cout<< course[ctr]<< "\t1.0\t passed";
else
cout<<"invalid input of grade";
}
cin>> response;
return 0;
}
HelloMe
Junior Poster in Training
87 posts since Sep 2009
Reputation Points: 10
Solved Threads: 1
i changed the ctr to 0. But still the strange error message is coming.
HelloMe
Junior Poster in Training
87 posts since Sep 2009
Reputation Points: 10
Solved Threads: 1
Still have the grade[x] in there... take the x out of your variable declarations so you can't use it by mistake.
jonsca
Quantitative Phrenologist
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
if the index is zero it has to be a
jonsca
Quantitative Phrenologist
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
Guys thank you so much i got it now
HelloMe
Junior Poster in Training
87 posts since Sep 2009
Reputation Points: 10
Solved Threads: 1
Thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you
HelloMe
Junior Poster in Training
87 posts since Sep 2009
Reputation Points: 10
Solved Threads: 1
jonsca
Quantitative Phrenologist
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581