Need Help

Hello, I am a student taking C++ , and I am asking for help for my homework, not that I am asking to help me do my homework, but I have a problem with the code (Which I wrote)

But before anyone calls me lazy, I would like to apologize , as I have no one to turn to for help (I am in my term break) .

This is the code in question :
#include <ctime>
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
srand(time(0));
int studentScore = rand() % 100 + 1;
int studentAssignment = rand() % 100 + 1;
int studentAttendance = rand() % 100 + 1;

cout << "Student term test score :" << studentScore << endl;
cout << "student Assignment score :" << studentAssignment << endl;
cout << "Student attendance :" << studentAttendance << endl;

if(studentAttendance >85)
{
if (studentAssignment>79 && studentScore>79)
{
cout << "A" << endl;
}
else
{
if (studentAssignment <=65 && studentAssignment >= 60 && studentScore <= 70 && studentScore >= 60)
{
cout << "C" << endl;
}
else
{
if (studentAssignment = 0 && studentScore <41 )
{
cout << "F" << endl;
}
else
if (studentAssignment <30 && studentScore <20)
{
cout << "F" << endl;
}
else
{
cout << "Grade cannot be determined" << endl;
}

}
}
}
}

The problem with my code is that It will not display the grades "A" "C" "F" and "grade cannot be determined" , I can build and run this solution without problem, but I am unable to get the fourth output , So I would like an expert to tell me my problem, I would really want to learn this , so I would like to request hints, and not the entire solution .....well at least until I am clueless...Thank you,

Sebastian
Hi!

First off, I'd like to ask you to use code tags. You can get them with the little button to the right of the window that looks like <>. I've reformatted your code for you, and also condensed your if-elses.

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
#include <ctime>
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
	srand(time(0));
	int studentScore = rand() % 100 + 1;
	int studentAssignment = rand() % 100 + 1;
	int studentAttendance = rand() % 100 + 1;
	
	cout << "Student term test score :" << studentScore << endl;
	cout << "student Assignment score :" << studentAssignment << endl;
	cout << "Student attendance :" << studentAttendance << endl;
	
	if(studentAttendance >85)
	{
		if (studentAssignment>79 && studentScore>79)
			cout << "A" << endl;
		else if (studentAssignment <=65 && studentAssignment >= 60 && studentScore <= 70 && studentScore >= 60)
			cout << "C" << endl;
		else if (studentAssignment = 0 && studentScore <41 )
			cout << "F" << endl;
		else if (studentAssignment <30 && studentScore <20)
			cout << "F" << endl;
		else
			cout << "Grade cannot be determined" << endl;
	}
}


The next thing to note is that I'm still waking up, so I might be missing something obvious.

The final thing to note is that all your grade-giving if-elses will only work if the student attendance is above 85. Otherwise, nothing will be printed. Assuming that the values from your random number generation are evenly-distributed, that whole code has a roughly 0.15 probability of running.

-Albatross
The line:

int studentAttendance = rand() % 100 + 1;

might not always be greater than 85 as it is randomly generated, when the application generates a value of less than 85 everything inside of: if(studentAttendance >85) will not execute.

For debugging purposes, you might want to add: studentAttendance = 90; to override your randomly generated value for this, so you can see the rest of the code execute.

Although not essential you may want to add some boolean (bool) variables to mask the contents of the if statements to make it more understandable. For example:

bool fAttendanceOK = studentAttendance >85; and have if( fAttendanceOK ).
bool fIsA = studentAssignment>79 && studentScore>79; and have if( fIsA ).

You also need to put return 0; at the end of your main function (as in the prototype it says you will return an int).

I hope this helps!

EDIT: Too late :(
Last edited on
closed account (z05DSL3A)
IamSebastian,

You have some very tight constraights on getting anything other than 'Grade cannot be determined' or nothing.

also: if (studentAssignment = 0 && studentScore <41 ) == not =
closed account (yUq2Nwbp)
IamSebastian first of all your if (studentAssignment = 0 && studentScore <41 ) condition doesn't do what you want.......change studentAssignment = 0 to studentAssignment == 0 because you compare studentAssignment with 0......

and then it could be problem connecting with prioritets......i'm not sure for 100% but

if (studentAssignment <=65 && studentAssignment >= 60 && studentScore <= 70 && studentScore >= 60)

condition might work not correct............

try this

if ((studentAssignment <=65 && studentAssignment >= 60) && (studentScore <= 70 && studentScore >= 60))

Topic archived. No new replies allowed.