Solved

How to write a C++ program for student and grades?

Posted on 2014-10-07
8
5,011 Views
Last Modified: 2014-10-13
I am trying to write a C++ program to only input 10 student last names, number of correct answers, and total number of questions.  The program will use a loop for 10 students only.  The program will print out like this.
Greater than 90 should say Excellent, 80-90 should say Well Done, 70-80 should say Good, 60-70 Needs Improvement, and under 50 Failed.

smith 83% .83333 Well Done
This is a format of the printout. Should be 10 lines of different name and so on.
0
Comment
Question by:David Weeks
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
8 Comments
 

Author Comment

by:David Weeks
ID: 40367323
I cannot figure out why my program will not work correctly.

// The program will read ten names from the user's input and compute  
// the grade for each name and prints the grade and tells whether the 
// grade is excellent, well done, good, or needs improvement. 

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>

using namespace std;

int main()

{
	string studentname;
	int score;
	int studentscore;
	int totalpoints;
	int percentage;
	int count;
	float finalscore;
	count = 1;
	    while (count <= 10)
	{
		cout << "Please enter the student's name" << endl;
		cin >> studentscore;
		cout << "Please enter the student's score" << endl;
		cin >> score;
		cout << "Please enter the total points" << endl;
		cin >> totalpoints;
		count = count++
		finalscore = score / totalpoints;
		percentage = finalscore * 100;
		if (percentage >= 90)
			cout << studentname << percentage << "%" << finalscore << "Excellent" << endl;
		if (percentage >= 80 && percentage < 90)
			cout << studentname << percentage << "%" << finalscore << "Well Done" << endl;
		if (percentage >= 70 && percentage < 80)
			cout << studentname << percentage << "%" << finalscore << "Good" << endl;
		if (percentage >= 60 && percentage < 70)
			cout << studentname << percentage << "%" << finalscore << "Needs Improvement" << endl;
		if (percentage < 50 && percentage < 60)
			cout << studentname << percentage << "%" << finalscore << "Failed" << endl;
	}
	return 0;
}

Open in new window

0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 40367341
The problem basically is this line:

finalscore = score / totalpoints;

Open in new window


The result of' 'score / totalpoints' (aka an integer division) is an integer value and will in your case most likely be zero. Try casting the values to 'float' first to avoid that, e.g.

finalscore = (float) score / (float) totalpoints;

Open in new window

0
 
LVL 86

Expert Comment

by:jkr
ID: 40372114
Do you have any further issues?
0
 
LVL 34

Expert Comment

by:sarabande
ID: 40376890
to add to jkr's solution:

there are more issues in your code:

1. the student name was asked for but you have no code to get the input from user cause you
    mixed up studentname and studentscore.

2.  you better use a for loop instead of a while loop like 'for (int count = 0; count < 10; ++count)'.
     that makes sure that the count properly was incremented after each loop cycle.
     usually in c and c++ you were using a zero-based loop counter what avoids mistakes
     when using it as index for arrays which are zero-based as well.

3. you may use 'else if' instead of 'if' statements when checking the percentage for ranges less than 90.
    that spares the check for the upper boundary (if (perc > 90) ... else if (perc > 80) ... else if (perc > 70) ...)

4. use a statement like 'getline(cin, strinput);' at end of main before return. then the program
    stops at end waiting for input and you can check the output in the console window (shell)
    if you run the program from the ide. alternatively, if running the program with debugger,
    you may set a breakpoint to the return statement.
 
note, for input you should use function call of getline(cin, strinput) instead of 'cin >> ...' . that allows to properly handle the case when a user wants to quit (for example by a statement like 'if (strinput == "quit") break;' ) .it also allows spaces for text input. the disadvantage is that you need to check and convert the string input, if a number was required. you may use a temporary stringstream for that purpose or - maybe easier - use a function like strtol or atoi.

Sara
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question