473,402 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

can anyone tell me why the letter grades aren't printing out on my program

the program reads input from a file and then outputs the averages and grade.
for some reason it is reading in the same line twice and it doesn't print out the grade. everything else is correct, if someone can help me thanks

this is my code....


Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. #include <fstream>
  6. using namespace std;
  7.  
  8. ifstream in_file;
  9. //structure that holds student record information.
  10. typedef struct records{
  11.         string name;
  12.         float quizes[7];
  13.         float projects[6];
  14.         float exams[2];
  15.         float labs[14];
  16. };
  17.  
  18. const int num_quizes = 7;
  19. const int num_exams = 2;
  20. const int num_projects = 6;
  21. const int num_labs = 14;
  22.  
  23. const float quizes_pts = 70;
  24. const float exams_pts = 200;
  25. const float projects_pts = 270;
  26. const float labs_pts = 1400;
  27.  
  28.  
  29. void get_scores(ifstream& infile, int num_scores, float scores[]);
  30. double get_average(float scores[], int num_scores, float tot_pts, float& average,int counter,float& current_score);
  31. void total_average(float& student_average,float ave1,float ave2,float ave3,float ave4);
  32. void grades_display(float ave1,float ave2,float ave3,float ave4,float total,char& grade,char& grade2);
  33.  
  34.  
  35.  
  36. int main()
  37. {
  38.  
  39.  
  40.     cout << "No.   Name      Quiz   Project   Exam   Lab   Total    Grade"
  41.          << endl;
  42.     cout << "--- ---------   ----- ---------- ------ ----- -------  -----"
  43.          << endl;
  44.     cout << endl;
  45.  
  46.  
  47.  
  48.         records grades;
  49.         ifstream in_file;
  50.         // Define a file input stream and open the file
  51.         in_file.open("record.txt"); //opening file
  52.  
  53.         if (in_file.fail())
  54.         {
  55.         // Failed to open the file (file doesn't exist or isn't readable)
  56.         cout << "Could not open file: "<< "\n";
  57.         exit(1);
  58.         }
  59.         float average = 0;
  60.         int counter = 1;
  61.         float current_score = 0;
  62.  
  63.         while(!in_file.eof())
  64.         {
  65.         //get string from the file
  66.         in_file >> grades.name;
  67.  
  68.         cout << counter << "     " << grades.name;
  69.  
  70.  
  71.         char letter_grade1;
  72.         char letter_grade2;
  73.         float student_average = 0;
  74.  
  75.  
  76.         // Repeatedly get characters from the file
  77.  
  78.         get_scores(in_file, num_quizes, grades.quizes);   //function for inputing info in struct.
  79.         get_average(grades.quizes, num_quizes, quizes_pts, average,counter,current_score); //function for average
  80.         float quiz_average = average;
  81.  
  82.  
  83.  
  84.         get_scores(in_file, num_projects, grades.projects); //input project scores
  85.         get_average(grades.projects, num_projects, projects_pts, average,counter,current_score);
  86.         float project_average = average;
  87.  
  88.  
  89.  
  90.         get_scores(in_file, num_exams, grades.exams); //input exam scores
  91.         get_average(grades.exams, num_exams, exams_pts, average,counter,current_score);
  92.         float exams_average = average;
  93.  
  94.  
  95.  
  96.         get_scores(in_file, num_labs, grades.labs); //input lab scores
  97.         get_average(grades.labs, num_labs, labs_pts, average,counter,current_score);
  98.         float labs_average = average;
  99.  
  100.  
  101.  
  102.         total_average(student_average,quiz_average,project_average,exams_average,labs_average);
  103.         grades_display(quiz_average,project_average,exams_average,labs_average,student_average,letter_grade1,letter_grade2);
  104.  
  105.         counter++;
  106. }
  107.  
  108.  
  109.         // Close the file
  110.     in_file.close();
  111.  
  112.  
  113.     return 0;
  114. }
  115.  
  116.  
  117.  
  118. //function that takes in parameters to input scores in array
  119. void get_scores(ifstream& in_file, int num_scores, float scores[])
  120. {
  121.  
  122.         for (int i = 0; i < num_scores; i++)
  123.         {
  124.          in_file >> scores[i];
  125.  
  126.         }
  127.  
  128. }
  129.  
  130. //funtion that takes the average of the inputed scores
  131. double get_average(float scores[], int num_scores,float tot_pts,float& average,int counter,float& current_score)
  132. {
  133.         float a = 0;
  134.  
  135.         average = 0;
  136.         for (int i = 0; i < num_scores; i++)
  137.         {
  138.                 a = scores[i] + a;
  139.         }
  140.  
  141.         current_score = a + current_score;
  142.         average = ((a)/(tot_pts));
  143.         average = (average * 100);
  144.  
  145.         while(counter > 2)
  146.         {
  147.         average = (average/100);
  148.         }
  149.         return average;
  150.  
  151.  
  152. }
  153.  
  154. //function to take total average for each member
  155. void total_average(float& student_average,float ave1,float ave2,float ave3,float ave4)
  156. {
  157.  
  158.         student_average  = ((ave1 + ave2 + ave3 + ave4)/4);
  159.  
  160. }
  161.  
  162. //function to output letter grade and output scores
  163. void grades_display(float ave1,float ave2,float ave3,float ave4,float total,char& grade,char& grade2)
  164. {
  165.  
  166.         if((100 >= total)&&(total >= 93))
  167.         {
  168.            grade = 'A';
  169.            grade2 = ' ';
  170.  
  171.         if((93 > total)&&(total >= 90))
  172.  
  173.            grade = 'A';
  174.            grade2 = '-';
  175.  
  176.         if((90 > total)&&(total <= 87))
  177.  
  178.            grade = 'B';
  179.            grade2 = '+';
  180.  
  181.         if((87 > total)&&(total <= 83))
  182.  
  183.            grade = 'B';
  184.            grade2 = ' ';
  185.  
  186.         if((83 > total)&&(total <= 80))
  187.  
  188.            grade = 'B';
  189.            grade2 = '-';
  190.  
  191.         if((80 > total)&&(total <= 77))
  192.  
  193.            grade = 'C';
  194.            grade2 = '+';
  195.  
  196.         if((77 > total)&&(total <= 73))
  197.  
  198.            grade = 'C';
  199.            grade2 = ' ';
  200.  
  201.         if((73 > total)&&(total <= 70))
  202.  
  203.            grade = 'C';
  204.            grade2 = '-';
  205.  
  206.         if((70 > total)&&(total <= 67))
  207.  
  208.            grade = 'D';
  209.            grade2 = '+';
  210.  
  211.         if((67 > total)&&(total <= 60))
  212.  
  213.            grade = 'D';
  214.            grade = ' ';
  215.  
  216.         if((60 > total)&&(total <= 0))
  217.  
  218.            grade = 'F';
  219.            grade2 = ' ';
  220.  
  221.          }
  222.         cout << "       " << ave1  << "       " << ave2  << "       " << ave3  << "       " << ave4 << "       " << total << "        " << grade << grade2 << endl;
  223.  
  224.  
  225.  
  226.  
  227. }
this is the input i used.....
smith 9 9.33 8 10 5.5 8 10 20 47.5 47 45 47.5 48 83 87 100 98 96 100 98 92 88
96 92 86 92 94 100 96

this is what is output......

No. Name Quiz Project Exam Lab Total Grade
--- --------- ----- ---------- ------ ----- ------- -----

1 smith 85.4714 94.4444 85 94.8571 89.9433
2 smith 85.4714 94.4444 85 94.8571 89.9433
May 13 '07 #1
2 2179
weaknessforcats
9,208 Expert Mod 8TB
You have a null line at the end of your data file.

I ran your code and it more or less works. There's a little garbage around the name display.

What compiler are you using??? I have to change some your variable names:

exams_ average is an example. It has an embedded space that I had to take out. There woere some others also.
May 13 '07 #2
yeah I just got it I changed some of my loops and it work... my question now is I'm tring to change the exactly the same code into a struct....any pointers I already added a public and private part to my struct and also added a void function in the public part to input my scores but can I still use constants with classes or do I have to rewrite that part of my code. Im really new with classes so any help would be helpful.
May 14 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Mr.Clean | last post by:
I have a string. I'd like to take this string and make each char change until it gets to another char at the same position of the string. Example: Original String: " NEW YORK " Final...
16
by: Mansi | last post by:
Given the following declaration: char letter = 'A'; Is there a way that I can increment letter so that 'B' is returned? Thanks. Mansi
2
by: millind | last post by:
I have a small issue. I have a html page which i need to print it legal size. My default printer setting is letter. How can i set a script which will print just this page in legal without change...
13
by: QQ | last post by:
for instance, I read a char from the input and I need to decide whether it is a letter or a number What I am doing is char a; ...... // read a int true = false; if( (a >='0') && (a <='z') true...
4
by: vadrama | last post by:
takes a list of grades and counts the amount A's, B's, C's etc... I am using a while statement to count the total number of grades which works great, but my if and else statements to count how...
2
by: Richard Hollenbeck | last post by:
I originally wrote my grades program in MS-Access to help community college teachers in the California community colleges keep track of their students' grades and produce reports for the students...
33
by: joebenjamin | last post by:
I am trying to write a program that will generate 100 random numbers between 1 and 50. Using these numbers, I want to generate a list that will tell the number of random numbers that fell between...
3
by: Energizer100 | last post by:
Hey. I'm new to Java and I'm trying to make a grades program for a class assignment. This is what I have so far. import java.util.Scanner; import static java.lang.System.out; public class...
3
by: RLN | last post by:
I have an app that will send out an email periodically. My table has one row and three columns: a primary key, an "EmailFrom" Address and an Email template. The template is the text of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.