This is what I have so far. any and all help is greatly appreciated. My program has a class of 20 students and D or better is passing at the end i have to tell how many students passed or failed.
#include<iostream>
using namespace std;
int main()
{
int grade;
int passing=0;
int failing=0;
cout<<"enter a grade: ";
cin>>grade;
for (int student=20; (grade>0 && grade<100); student--){
while (student>0){
if (grade== "a")
passing++;
if (grade== "b")
passing++;
if (grade=="c")
passing++;
if (grade=="d")
passing++;
if (grade=="e");
failing++;
if (grade=="f");
failing++;
}
}
cout<<passing<<" students passed"<<endl;
cout<<failing<<" students failed"<<endl;
system("pause");
return(0);
}
It just won't add the grades for passing and failing up.
#include<iostream>
using namespace std;
int main()
{
int grade;
int passing=0;
int failing=0;
int student=20;
while (student>0) {
cout<<"enter a grade: ";
cin>>grade;
student--;
if (grade== 'a')
passing++;
else if (grade== 'b')
passing++;
else if (grade=='c')
passing++;
else if (grade=='d')
passing++;
else if (grade=='e')
failing++;
else (grade=='f');
failing++;
}
cout<<passing<<" students passed"<<endl;
cout<<failing<<" students failed"<<endl;
system("pause");
return(0);
}
Take out the system("pause");
most people here consider it poor programming, even though it does work, there are better ways. Run it from a command line is better.
Change int grade; to
string grade;
For each grade change
if (grade== 'a')
to
if (grade== "a")
Test your program, try using invalid letters and numbers such as 1 2 3 4 A B C D. It needs more work but your getting there.
Results from my test
C:\Temp>teststudentgrade2
enter a grade: a
enter a grade: b
enter a grade: c
enter a grade: d
enter a grade: e
enter a grade: f
enter a grade: a
enter a grade: b
enter a grade: c
enter a grade: d
enter a grade: e
enter a grade: f
enter a grade: a
enter a grade: b
enter a grade: c
enter a grade: d
enter a grade: e
enter a grade: f
enter a grade: a
enter a grade: b
14 students passed
23 students failed
C:\Temp>teststudentgrade2
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
20 students passed
20 students failed
I bet if you read the next 2 lines you can figure the above problem out.
else (grade=='f');
failing++;
There will still be a logic problem you need to figure out.
C:\Temp>teststudentgrade2
enter a grade: aa
enter a grade: bb
enter a grade: cc
enter a grade: dd
enter a grade: ee
enter a grade: ff
enter a grade: dd
enter a grade: f
enter a grade: f
enter a grade: f
enter a grade: f
enter a grade: f
enter a grade: f
enter a grade: f
enter a grade: f
enter a grade: f
enter a grade: f
enter a grade: f
enter a grade: f
enter a grade: f
0 students passed
13 students failed
I'm not quite sure how to apply the string, I do see the issue with my else statement but i'm not sure how to fix it I tried substituting the ifs for while loops but it didnt go well it only executes the first number.
I can't get my program to repeat the loop it's in please help me.
#include<iostream>
using namespace std;
int main()
{
char ch1='a', ch2='b', ch3='c', ch4='d', ch5='e', ch6='f';
char ch;
int passing=0;
int failing=0;
int student=20;
for (student > 0; student--;) {
cout<<"enter a grade: ";
cin>>ch;
It only gives me 10 students passed 10 students failed. How best to fix that? Any and all help is appreciated.
#include<iostream>
using namespace std;
int main()
{
char ch1='a', ch2='b', ch3='c', ch4='d', ch5='e', ch6='f';
char ch=0;
int passing=0;
int failing=0;
int student=20;
while (student > 0){
if (ch == ch1 || ch2 || ch3 || ch4);{
passing++;
cout<<"enter a grade: ";
cin>>ch;
student--;
}
if (ch == ch5 || ch6){
failing++;
cout<<"enter a grade: ";
cin>>ch;
student--;
}
}
cout<<passing<<" students passed"<<endl;
cout<<failing<<" students failed"<<endl;
system ("pause");
return(0);
}
#include<iostream>
usingnamespace std;
int main()
{
int grade;
int passing=0;
int failing=0;
//this next line declares the top of the program so it knows where to go
//back to at the bottom
Top1:
cout << "Enter a grade: ";
cin >> grade;
//for these you needed ' instead of "
if (grade== 'a')
{
passing++;
}
elseif (grade== 'b')
{
passing++;
}
elseif (grade== 'c')
{
passing++;
}
elseif (grade== 'd')
{
passing++;
}
elseif (grade== 'e');
{
failing++;
}
elseif (grade== 'f');
{
failing++;
}
else
{
cout << "Please enter a valid choice" << endl;
//this command tells it to go back to the top where you declared Top1
goto Top1;
}
cout << passing << " students passed" << endl;
cout << failing << " students failed" << endl;
system("pause");
return(0);
}
That should work if you are using microsoft visual c++ 2010 express, but if you use another compiler
then you might need to change the syntax a bit.