GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 19-Jun-2006, 10:12
UnWarierMage UnWarierMage is offline
New Member
 
Join Date: Jun 2006
Posts: 5
UnWarierMage is on a distinguished road

Grades Calculation Program


Hi guys,

I'm following an exercise out of a book: "Learn to Program with C++", author: John Smiley.

The problem is this: I've followed the code out of the book exactly, but the program doesn't work. What it's supposed to do is after you answer "Yes" to the initial question, the body of the program under the while loop should execute. However, what happens is after you answer "yes", the program just quits, althought the while loop says otherwise.

Here is the code (It's long - 280 lines! I think the problem may be within the first few lines of code, but I'm not sure):

Thanks for the help,
-'Mage.

Code below:

CPP / C++ / C Code:
/***************************
*Name: UnWarierMage
*Date: 6/18/06
*Project: Grades Calculation
*Version: 1.3
****************************/

//Preprocessing directives 

#include <iostream>
#include <string>
using namespace std; 

//Main()

int main() 
{ 
   const float ENGLISH_MIDTERM_PERCENTAGE = .25;
   const float ENGLISH_FINALEXAM_PERCENTAGE = .25;
   const float ENGLISH_RESEARCH_PERCENTAGE = .30;
   const float ENGLISH_PRESENTATION_PERCENTAGE = .20;

   const float MATH_MIDTERM_PERCENTAGE = .5F;
   const float MATH_FINALEXAM_PERCENTAGE = .50; 

   const float SCIENCE_MIDTERM_PERCENTAGE = .40;
   const float SCIENCE_FINALEXAM_PERCENTAGE = .40;
   const float SCIENCE_RESEARCH_PERCENTAGE = .20; 

   int midterm = 0; 
   int finalExamGrade = 0; 
   int research = 0; 
   int presentation = 0; 
   float finalNumericGrade = 0; 
   char finalLetterGrade;
   char response[256]; 
   string moreGradesToCalculate; 

   cout << "Do you want to calculate a grade? ";
   cin >> moreGradesToCalculate; 

   for (int i = 0; i < moreGradesToCalculate.length(); i++)
   { 
      moreGradesToCalculate[i] = toupper (moreGradesToCalculate[i]);      
   }//End for

   while (moreGradesToCalculate == "YES") 
   { 
      
      //Student type

      cout << "Enter Student Type" << "(1=English, 2=Math, 3=Science): ";
      cin.getline(response,256); 

      if (strlen(response) == 0)
      { 
         cout << "You must select a Student Type";
         return 1;      
      }//End if

      if (atoi(response) < 1 || (atoi(response) > 3))
      { 
         cout << response << " - is not a valid student type";
         return 1; 
      }//End if

      //ST Validated, grade calculation

      switch(atoi(response))
      {
         //Case 1: English student 
         case 1:
            cout << "Enter the Midterm Grade: ";
            cin.getline(response,256);
            midterm = atoi(response); 
           
            cout << "Enter the Final Examination Grade: ";
            cin.getline(response,256);
            finalExamGrade = atoi(response);

            cout << "Enter the Research Grade: "; 
            cin.getline(response,256);
            research = atoi(response);

            cout << "Enter the Presentation Grade: "; 
            cin.getline(response,256);
            presentation = atoi(response); 

            //finalNumericGrade calculation

            finalNumericGrade = (midterm * ENGLISH_MIDTERM_PERCENTAGE) + 
                                (finalExamGrade * ENGLISH_FINALEXAM_PERCENTAGE) + 
                                (research * ENGLISH_RESEARCH_PERCENTAGE) + 
                                (presentation * ENGLISH_PRESENTATION_PERCENTAGE); 

            //finalLetterGrade assignment 

            if (finalNumericGrade >= 93)
            {
               finalLetterGrade = 'A'; 
            }//End if
            
            else 
            if ((finalNumericGrade >= 85) && (finalNumericGrade < 93))
            { 
               finalLetterGrade = 'B';
            }//End if 

            else
            if ((finalNumericGrade >= 78) && (finalNumericGrade < 85))
            { 
               finalLetterGrade = 'C';
            }//End if

            else
            if ((finalNumericGrade >=70) && (finalNumericGrade < 78))
            { 
               finalLetterGrade = 'D';
            }//End if 

            else
            if (finalNumericGrade < 70)
            { 
               finalLetterGrade = 'F';
            }//End if 

            //Summary 

            cout << endl << "***ENGLISH STUDENT***" << endl << endl;
            cout << "Midterm Grade is: " << midterm << endl;
            cout << "Final Exam Grade is: " << finalExamGrade << endl;
            cout << "Research Grade is: " << research << endl;
            cout << "Presentation Grade is: " << presentation << endl;
            cout << "Final Numeric Grade is: " << finalNumericGrade << endl;
            cout << "Final Letter Grade is: " << finalLetterGrade; 

            break;
            //End case 1

         //Case 2: Math student 
         case 2: 
            cout << "Enter the Midterm Grade: "; 
            cin.getline(response,256); 
            midterm = atoi(response); 

            cout << "Enter the Final Examination Grade: ";
            cin.getline(response,256);
            finalExamGrade = atoi(response); 

            //finalNumericGrade calculation 

            finalNumericGrade = (midterm * MATH_MIDTERM_PERCENTAGE) + 
                                (finalExamGrade * MATH_FINALEXAM_PERCENTAGE); 

            //finalLetterGrade assignment 

            if (finalNumericGrade >= 90)
            { 
               finalLetterGrade = 'A';
            }//End if 

            else 
            if ((finalNumericGrade >= 83) && (finalNumericGrade < 90)) 
            { 
               finalLetterGrade = 'B';
            }//End if 

            else
            if ((finalNumericGrade >= 76) && (finalNumericGrade < 83))
            { 
               finalLetterGrade = 'C';
            }//End if 

            else 
            if ((finalNumericGrade >= 65) && (finalNumericGrade < 76))
            { 
               finalLetterGrade = 'D';
            }//End if 

            else 
            if (finalNumericGrade < 65)
            { 
               finalLetterGrade = 'F';
            }//End if 

            //Summary 

            cout << endl << "***MATH STUDENT***" << endl << endl; 
            cout << "Midterm Grade is: " << midterm << endl; 
            cout << "Final Exam Grade is: " << finalExamGrade << endl; 
            cout << "Final Numeric Grade is: " << finalNumericGrade << endl; 
            cout << "Final Letter Grade is: " << finalLetterGrade; 

            break;
            //End case 2 

         //Case 3: Science student 
         case 3:        
            cout << "Enter the Midterm Grade: ";
            cin.getline(response,256); 
            midterm = atoi(response); 

            cout << "Enter the Final Examination Grade: ";
            cin.getline(response,256); 
            finalExamGrade = atoi(response);
            
            cout << "Enter the Research Grade: "; 
            cin.getline(response,256); 
            research = atoi(response);
            
            //finalNumericGrade calculation 

            finalNumericGrade = (midterm * SCIENCE_MIDTERM_PERCENTAGE) + 
                                (finalExamGrade * SCIENCE_FINALEXAM_PERCENTAGE) + 
                                (research * SCIENCE_RESEARCH_PERCENTAGE); 

            //finalLetterGrade assignment

            if (finalNumericGrade >= 90) 
            { 
               finalLetterGrade = 'A';
            }//End if 

            else 
            if ((finalNumericGrade >= 80) && (finalNumericGrade < 90))
            { 
               finalLetterGrade = 'B'; 
            }//End if 

            else 
            if ((finalNumericGrade >= 70) && (finalNumericGrade < 80))
            { 
               finalLetterGrade = 'C'; 
            }//End if 

            else 
            if ((finalNumericGrade >= 60) && (finalNumericGrade < 70))
            { 
               finalLetterGrade = 'D'; 
            }//End if 

            else 
            if (finalNumericGrade < 60)
            { 
               finalLetterGrade = 'F'; 
            }//End if 

            //Summary 

            cout << endl << "***SCIENCE STUDENT***" << endl << endl;
            cout << "Midterm Grade is: " << midterm << endl; 
            cout << "Final Exam Grade is: " << finalExamGrade << endl; 
            cout << "Research Grade is: " << research << endl; 
            cout << "Final Numeric Grade is: " << finalNumericGrade << endl; 
            cout << "Final Letter Grade is: " << finalLetterGrade; 
         
            break;
            //End case 3

         default: 
            cout << response << " - is not a valid student type";
            return 1;
         //End default             
               
      }//End switch

      cout << endl << endl << "Do you have another grade to calculate? "; 
      cin >> moreGradesToCalculate; 
      for (int i = 0; i < moreGradesToCalculate.length(); i++)
      { 
         moreGradesToCalculate[i] = toupper (moreGradesToCalculate[i]);
      }//End for
   }//End while

   cout << "Thanks for using the Grades Calculation program!";
   cin.get();
   return 0; 

} //End main
  #2  
Old 19-Jun-2006, 10:28
UnWarierMage UnWarierMage is offline
New Member
 
Join Date: Jun 2006
Posts: 5
UnWarierMage is on a distinguished road

Re: Grades Calculation Program


hmm.. tried the suggestion that MichaelS-R gave...typing "YES" in all caps into the question field... the program ran, but quit (I saw a brief flash of another line of the program, and then it quit).

So, no joy.
  #3  
Old 19-Jun-2006, 12:21
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 376
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: Grades Calculation Program


try adding system("pause"); before the return statement in the main function.
  #4  
Old 19-Jun-2006, 13:20
UnWarierMage UnWarierMage is offline
New Member
 
Join Date: Jun 2006
Posts: 5
UnWarierMage is on a distinguished road

Re: Grades Calculation Program


I put "cin.get()" before every return statement in the program. Now what happens is when you say yes, it jumps to "Select student type" but then says immediately "you must select a student type".

-'Mage
  #5  
Old 19-Jun-2006, 13:34
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 376
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: Grades Calculation Program


The problem is line 53:

CPP / C++ / C Code:
cin.getline(response,256);

This works (I also changed a couple of ints to unsigned ints to keep the compiler happy.)

CPP / C++ / C Code:
/***************************
*Name: UnWarierMage
*Date: 6/18/06
*Project: Grades Calculation
*Version: 1.3
****************************/

//Preprocessing directives

#include <iostream>
#include <string>
using namespace std;

//Main()

int main()
{
   const float ENGLISH_MIDTERM_PERCENTAGE = .25;
   const float ENGLISH_FINALEXAM_PERCENTAGE = .25;
   const float ENGLISH_RESEARCH_PERCENTAGE = .30;
   const float ENGLISH_PRESENTATION_PERCENTAGE = .20;

   const float MATH_MIDTERM_PERCENTAGE = .5F;
   const float MATH_FINALEXAM_PERCENTAGE = .50;

   const float SCIENCE_MIDTERM_PERCENTAGE = .40;
   const float SCIENCE_FINALEXAM_PERCENTAGE = .40;
   const float SCIENCE_RESEARCH_PERCENTAGE = .20;

   int midterm = 0;
   int finalExamGrade = 0;
   int research = 0;
   int presentation = 0;
   float finalNumericGrade = 0;
   char finalLetterGrade;
   char response[256];
   string moreGradesToCalculate;

   cout << "Do you want to calculate a grade? ";
   cin >> moreGradesToCalculate;

   for ( unsigned int i = 0; i < moreGradesToCalculate.length(); i++)
   {
      moreGradesToCalculate[i] = toupper (moreGradesToCalculate[i]);
   }//End for

   while (moreGradesToCalculate == "YES")
   {

      //Student type

      cout << "Enter Student Type" << "(1=English, 2=Math, 3=Science): ";
      cin >> response;

      if (strlen(response) == 0)
      {
         cout << "You must select a Student Type";
         return 1;
      }//End if

      if (atoi(response) < 1 || (atoi(response) > 3))
      {
         cout << response << " - is not a valid student type";
         return 1;
      }//End if

      //ST Validated, grade calculation

      switch(atoi(response))
      {
         //Case 1: English student
         case 1:
            cout << "Enter the Midterm Grade: ";
            cin.getline(response,256);
            midterm = atoi(response);

            cout << "Enter the Final Examination Grade: ";
            cin.getline(response,256);
            finalExamGrade = atoi(response);

            cout << "Enter the Research Grade: ";
            cin.getline(response,256);
            research = atoi(response);

            cout << "Enter the Presentation Grade: ";
            cin.getline(response,256);
            presentation = atoi(response);

            //finalNumericGrade calculation

            finalNumericGrade = (midterm * ENGLISH_MIDTERM_PERCENTAGE) +
                                (finalExamGrade * ENGLISH_FINALEXAM_PERCENTAGE) +
                                (research * ENGLISH_RESEARCH_PERCENTAGE) +
                                (presentation * ENGLISH_PRESENTATION_PERCENTAGE);

            //finalLetterGrade assignment

            if (finalNumericGrade >= 93)
            {
               finalLetterGrade = 'A';
            }//End if

            else
            if ((finalNumericGrade >= 85) && (finalNumericGrade < 93))
            {
               finalLetterGrade = 'B';
            }//End if

            else
            if ((finalNumericGrade >= 78) && (finalNumericGrade < 85))
            {
               finalLetterGrade = 'C';
            }//End if

            else
            if ((finalNumericGrade >=70) && (finalNumericGrade < 78))
            {
               finalLetterGrade = 'D';
            }//End if

            else
            if (finalNumericGrade < 70)
            {
               finalLetterGrade = 'F';
            }//End if

            //Summary

            cout << endl << "***ENGLISH STUDENT***" << endl << endl;
            cout << "Midterm Grade is: " << midterm << endl;
            cout << "Final Exam Grade is: " << finalExamGrade << endl;
            cout << "Research Grade is: " << research << endl;
            cout << "Presentation Grade is: " << presentation << endl;
            cout << "Final Numeric Grade is: " << finalNumericGrade << endl;
            cout << "Final Letter Grade is: " << finalLetterGrade;

            break;
            //End case 1

         //Case 2: Math student
         case 2:
            cout << "Enter the Midterm Grade: ";
            cin.getline(response,256);
            midterm = atoi(response);

            cout << "Enter the Final Examination Grade: ";
            cin.getline(response,256);
            finalExamGrade = atoi(response);

            //finalNumericGrade calculation

            finalNumericGrade = (midterm * MATH_MIDTERM_PERCENTAGE) +
                                (finalExamGrade * MATH_FINALEXAM_PERCENTAGE);

            //finalLetterGrade assignment

            if (finalNumericGrade >= 90)
            {
               finalLetterGrade = 'A';
            }//End if

            else
            if ((finalNumericGrade >= 83) && (finalNumericGrade < 90))
            {
               finalLetterGrade = 'B';
            }//End if

            else
            if ((finalNumericGrade >= 76) && (finalNumericGrade < 83))
            {
               finalLetterGrade = 'C';
            }//End if

            else
            if ((finalNumericGrade >= 65) && (finalNumericGrade < 76))
            {
               finalLetterGrade = 'D';
            }//End if

            else
            if (finalNumericGrade < 65)
            {
               finalLetterGrade = 'F';
            }//End if

            //Summary

            cout << endl << "***MATH STUDENT***" << endl << endl;
            cout << "Midterm Grade is: " << midterm << endl;
            cout << "Final Exam Grade is: " << finalExamGrade << endl;
            cout << "Final Numeric Grade is: " << finalNumericGrade << endl;
            cout << "Final Letter Grade is: " << finalLetterGrade;

            break;
            //End case 2

         //Case 3: Science student
         case 3:
            cout << "Enter the Midterm Grade: ";
            cin.getline(response,256);
            midterm = atoi(response);

            cout << "Enter the Final Examination Grade: ";
            cin.getline(response,256);
            finalExamGrade = atoi(response);

            cout << "Enter the Research Grade: ";
            cin.getline(response,256);
            research = atoi(response);

            //finalNumericGrade calculation

            finalNumericGrade = (midterm * SCIENCE_MIDTERM_PERCENTAGE) +
                                (finalExamGrade * SCIENCE_FINALEXAM_PERCENTAGE) +
                                (research * SCIENCE_RESEARCH_PERCENTAGE);

            //finalLetterGrade assignment

            if (finalNumericGrade >= 90)
            {
               finalLetterGrade = 'A';
            }//End if

            else
            if ((finalNumericGrade >= 80) && (finalNumericGrade < 90))
            {
               finalLetterGrade = 'B';
            }//End if

            else
            if ((finalNumericGrade >= 70) && (finalNumericGrade < 80))
            {
               finalLetterGrade = 'C';
            }//End if

            else
            if ((finalNumericGrade >= 60) && (finalNumericGrade < 70))
            {
               finalLetterGrade = 'D';
            }//End if

            else
            if (finalNumericGrade < 60)
            {
               finalLetterGrade = 'F';
            }//End if

            //Summary

            cout << endl << "***SCIENCE STUDENT***" << endl << endl;
            cout << "Midterm Grade is: " << midterm << endl;
            cout << "Final Exam Grade is: " << finalExamGrade << endl;
            cout << "Research Grade is: " << research << endl;
            cout << "Final Numeric Grade is: " << finalNumericGrade << endl;
            cout << "Final Letter Grade is: " << finalLetterGrade;

            break;
            //End case 3

         default:
            cout << response << " - is not a valid student type";
            return 1;
         //End default

      }//End switch

      cout << endl << endl << "Do you have another grade to calculate? ";
      cin >> moreGradesToCalculate;
      for (unsigned int i = 0; i < moreGradesToCalculate.length(); i++)
      {
         moreGradesToCalculate[i] = toupper (moreGradesToCalculate[i]);
      }//End for
   }//End while

   cout << "Thanks for using the Grades Calculation program!";
   cin.get();
   return 0;

} //End main

  #6  
Old 19-Jun-2006, 13:36
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,153
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Grades Calculation Program


Quote:
Originally Posted by UnWarierMage
Hi guys,

I'm following an exercise out of a book: "Learn to Program with C++", author: John Smiley.

I'm not familiar with the book or the author, however this program is a really fine example of why it's not a Good Thing to mix cin>> and getline in a program.

The program that you posted compiles and executes exactly as I think it is supposed to when I compiled it with the Borland bcc32 compiler. On the other hand, it exhibited precisely the behavior that you reported when I ran it with Microsoft visual C++ (both Visual Studio Version 6.0 and Visual C++ 2005 Express). Same bad behavior with GNU gcc.

Stop! Don't think that Borland is always Good and Microsoft and GNU are always bad. Don't throw away whatever it is that you are using on the basis of behavior with this program. I hate to repeat myself, but I think this program is a pretty good example of what not to do about input data in C++.

To me, the mystery is not so much: "Why didn't the other stuff work?" The mystery is: "Why did the Borland stuff work?" Another mystery might be: "Why would an author of a beginning textbook use a program that uses cin>> for reading a C++ std::string and then use the form of istream::getline( ) for reading into an array of char (treated as a C-style "string")?" How many confusing items could I put into a simple program? That's a rhetorical question. See footnote.

So, I'll show a way that I think will give the results that you might want. I'm not claiming it's a better way of doing it, but it works with compilers for which I have access, and it uses consistent I/O.

CPP / C++ / C Code:
#include <iostream>
//#include <string> // not needed since we don't use strings any more
using namespace std;

//Main()

int main()
{
    const float ENGLISH_MIDTERM_PERCENTAGE      = .25;
    const float ENGLISH_FINALEXAM_PERCENTAGE    = .25;
    const float ENGLISH_RESEARCH_PERCENTAGE     = .30;
    const float ENGLISH_PRESENTATION_PERCENTAGE = .20;

    const float MATH_MIDTERM_PERCENTAGE         = .5F;
    const float MATH_FINALEXAM_PERCENTAGE       = .50;

    const float SCIENCE_MIDTERM_PERCENTAGE      = .40;
    const float SCIENCE_FINALEXAM_PERCENTAGE    = .40;
    const float SCIENCE_RESEARCH_PERCENTAGE     = .20;

    unsigned i;

    int midterm = 0;
    int finalExamGrade = 0;
    int research = 0;
    int presentation = 0;
    float finalNumericGrade = 0;
    char finalLetterGrade;
    char response[256];
    string moreGradesToCalculate;

    cout << "Do you want to calculate a grade? ";
    cin.getline(response, 256);


    for (i = 0; i < strlen(response); i++) {
        response[i] = toupper(response[i]);
    }                           //End for

    while (strcmp(response, "YES") == 0) {

        //Student type

        cout << "Enter Student Type" << "(1=English, 2=Math, 3=Science): ";
        cin.getline(response, 256);

        if (strlen(response) == 0) {
            cout << "You must select a Student Type";
            return 1;
        }                       //End if

        if (atoi(response) < 1 || (atoi(response) > 3)) {
            cout << response << " - is not a valid student type";
            return 1;
        }                       //End if

        //ST Validated, grade calculation

        switch (atoi(response)) {
            //Case 1: English student 
        case 1:
            cout << "Enter the Midterm Grade           : ";
            cin.getline(response, 256);
            midterm = atoi(response);

            cout << "Enter the Final Examination Grade : ";
            cin.getline(response, 256);
            finalExamGrade = atoi(response);

            cout << "Enter the Research Grade          : ";
            cin.getline(response, 256);
            research = atoi(response);

            cout << "Enter the Presentation Grade      : ";
            cin.getline(response, 256);
            presentation = atoi(response);

            //finalNumericGrade calculation

            finalNumericGrade = (midterm * ENGLISH_MIDTERM_PERCENTAGE) +
                (finalExamGrade * ENGLISH_FINALEXAM_PERCENTAGE) +
                (research * ENGLISH_RESEARCH_PERCENTAGE) +
                (presentation * ENGLISH_PRESENTATION_PERCENTAGE);

            //finalLetterGrade assignment 

            if (finalNumericGrade >= 93) {
                finalLetterGrade = 'A';
            }                   //End if

            else if ((finalNumericGrade >= 85) && (finalNumericGrade < 93)) {
                finalLetterGrade = 'B';
            }                   //End if 

            else if ((finalNumericGrade >= 78) && (finalNumericGrade < 85)) {
                finalLetterGrade = 'C';
            }                   //End if

            else if ((finalNumericGrade >= 70) && (finalNumericGrade < 78)) {
                finalLetterGrade = 'D';
            }                   //End if 

            else if (finalNumericGrade < 70) {
                finalLetterGrade = 'F';
            }                   //End if 

            //Summary 

            cout << endl << "***ENGLISH STUDENT***" << endl << endl;
            cout << "Midterm Grade is       : " << midterm << endl;
            cout << "Final Exam Grade is    : " << finalExamGrade << endl;
            cout << "Research Grade is      : " << research << endl;
            cout << "Presentation Grade is  : " << presentation << endl;
            cout << "Final Numeric Grade is : " << finalNumericGrade << endl;
            cout << "Final Letter Grade is  : " << finalLetterGrade;

            break;
            //End case 1

            //Case 2: Math student 
        case 2:
            cout << "Enter the Midterm Grade           : ";
            cin.getline(response, 256);
            midterm = atoi(response);

            cout << "Enter the Final Examination Grade : ";
            cin.getline(response, 256);
            finalExamGrade = atoi(response);

            //finalNumericGrade calculation 

            finalNumericGrade = (midterm * MATH_MIDTERM_PERCENTAGE) +
                (finalExamGrade * MATH_FINALEXAM_PERCENTAGE);

            //finalLetterGrade assignment 

            if (finalNumericGrade >= 90) {
                finalLetterGrade = 'A';
            }                   //End if 

            else if ((finalNumericGrade >= 83) && (finalNumericGrade < 90)) {
                finalLetterGrade = 'B';
            }                   //End if 

            else if ((finalNumericGrade >= 76) && (finalNumericGrade < 83)) {
                finalLetterGrade = 'C';
            }                   //End if 

            else if ((finalNumericGrade >= 65) && (finalNumericGrade < 76)) {
                finalLetterGrade = 'D';
            }                   //End if 

            else if (finalNumericGrade < 65) {
                finalLetterGrade = 'F';
            }                   //End if 

            //Summary 

            cout << endl << "***MATH STUDENT***" << endl << endl;
            cout << "Midterm Grade is       : " << midterm << endl;
            cout << "Final Exam Grade is    : " << finalExamGrade << endl;
            cout << "Final Numeric Grade is : " << finalNumericGrade << endl;
            cout << "Final Letter Grade is  : " << finalLetterGrade;

            break;
            //End case 2 

            //Case 3: Science student 
        case 3:
            cout << "Enter the Midterm Grade           : ";
            cin.getline(response, 256);
            midterm = atoi(response);

            cout << "Enter the Final Examination Grade : ";
            cin.getline(response, 256);
            finalExamGrade = atoi(response);

            cout << "Enter the Research Grade          : ";
            cin.getline(response, 256);
            research = atoi(response);

            //finalNumericGrade calculation 

            finalNumericGrade = (midterm * SCIENCE_MIDTERM_PERCENTAGE) +
                (finalExamGrade * SCIENCE_FINALEXAM_PERCENTAGE) +
                (research * SCIENCE_RESEARCH_PERCENTAGE);

            //finalLetterGrade assignment

            if (finalNumericGrade >= 90) {
                finalLetterGrade = 'A';
            }                   //End if 

            else if ((finalNumericGrade >= 80) && (finalNumericGrade < 90)) {
                finalLetterGrade = 'B';
            }                   //End if 

            else if ((finalNumericGrade >= 70) && (finalNumericGrade < 80)) {
                finalLetterGrade = 'C';
            }                   //End if 

            else if ((finalNumericGrade >= 60) && (finalNumericGrade < 70)) {
                finalLetterGrade = 'D';
            }                   //End if 

            else if (finalNumericGrade < 60) {
                finalLetterGrade = 'F';
            }                   //End if 

            //Summary 

            cout << endl << "***SCIENCE STUDENT***" << endl << endl;
            cout << "Midterm Grade is: " << midterm << endl;
            cout << "Final Exam Grade is    : " << finalExamGrade << endl;
            cout << "Research Grade is      : " << research << endl;
            cout << "Final Numeric Grade is : " << finalNumericGrade << endl;
            cout << "Final Letter Grade is  : " << finalLetterGrade;

            break;
            //End case 3

        default:
            cout << response << " - is not a valid student type";
            return 1;
            //End default             

        }                       //End switch

        cout << endl << endl << "Do you have another grade to calculate? ";
        cin.getline(response, 256);
        for (i = 0; i < strlen(response); i++) {
            response[i] = toupper(response[i]);
        }                       //End for
    }                           //End while

    cout << "Thanks for using the Grades Calculation program!" << endl;
    cout << "Press 'Enter' to exit the program: ";

    cin.getline(response, 256);
    return 0;

}                               //End main

I haven't changed anything significant other than the input routine for getting the "Ok to go" message from the user, and I won't comment on general style or anything else.

I perceive from your question about the program ending with a brief flash that

1. You are running from some Integrated Development Environment (Maybe Microsoft, maybe dev-c++, maybe something else. Sometimes when you are asking for help, it helps us to help you if you tell us exactly what compiler you are using (and what operating system). You have no way of knowing whether the behavior is compiler-dependent or environment-dependent. Sometimes we can guess; sometimes --- not so much.

2. You haven't done much programming with this environment, since you (undoubtdedly) would have encountered the old "window closes when the program exits and I can't see what the heck it printed" behavior before.

My suggestion is that you write a few simple programs (start with elementary book examples; then make up your own simple program. See what is required to keep the window open after the program exits. Write a few simple programs that input strings (C++ std::strings and/or C-style "strings"). Write a few simple programs that input numerical values (the world-famous Fahrenheit-do-Celsius conversion, for example, and I'll even give you bonus points for spelling "Fahrenheit " correctly.)

I know it's disappointing to have a program in a book that doesn't work the way it is supposed to, and ones like this are particularly difficult to debug (I say this, based on the number of posts that I have seen with problems like this). Don't give up on a book just because there is one program that needs tweaking a little. I can tell you from experience that it's really easy to concoct a program that tests perfectly well on one system but fails on another system, sometimes due to things like mixing cin>> and getline. I have seen programs that apparently work OK on all systems, but I have seen others (like this one) that do not.

By the same token, make sure you understand what's happening with book examples before going to the next. Sometimes it's easy to overlook important stuff in little programs in one's eagerness to get to the "good stuff".

Regards,

Dave

Footnote:
I sometimes get annoyed by authors who ask too many rhetorical questions; don't you? But wait! Wasn't that a rhetorical question? On the other hand, what if this is not a rhetorical question?
  #7  
Old 19-Jun-2006, 13:45
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,153
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Grades Calculation Program


Quote:
Originally Posted by Blake
The problem is line 53:

CPP / C++ / C Code:
cin.getline(response,256);

The problem is mixing cin and getline and not taking everything into account. With your program and Microsoft or GNU compilers here's a program run:

Code:
Do you want to calculate a grade? yes Enter Student Type(1=English, 2=Math, 3=Science): 2 Enter the Midterm Grade: Enter the Final Examination Grade: 95 ***MATH STUDENT*** Midterm Grade is: 0 Final Exam Grade is: 95 Final Numeric Grade is: 47.5 Final Letter Grade is: F Do you have another grade to calculate? no Thanks for using the Grades Calculation program!

(It did pause after the "Thanks..." message, and that's good --- at least I can see that it didn't do what it was meant to do.)

Rather than seeing what incremental changes might be fortunate to make it appear to work, I suggest that a programmer use either cin>> or getline(), but not both.

I usually favor getline() where it makes a difference, but I use cin a lot also (just not in the same program as getline()). That's just my opinion of course, and Your Mileage May Vary.

Regards,

Dave

P.S.
When people try to give advice like this, and other people test it and find it doean't really seem necessary, it's easy to understand why the others ignore the advice. But some day, some way it will make a difference. Please: Don't take just my advice, or any other individual's. But you might want to keep this in mind when you go to a new computer system and a new compiler: Some of the stuff you learned to do is actually not OK.

That's why I like this forum: I get to see things from lots of other people's point of view. I always learn something when I visit this place (always).

P.S.S.
Note that the modifications to the program that I showed in my post were designed to illustrate a way of working around the problem of mixing cin and getline() --- just say, "No!" There are lots of other things to do that could make the program more robust, regardless of which function is used for getting the user data, and I don't say that what I posted is a perfect example of a program of this type. But it did illustrate a point, I hope.

D
Last edited by davekw7x : 19-Jun-2006 at 14:19.
  #8  
Old 19-Jun-2006, 14:17
UnWarierMage UnWarierMage is offline
New Member
 
Join Date: Jun 2006
Posts: 5
UnWarierMage is on a distinguished road

Re: Grades Calculation Program


Davekw7x:

I'm using the dev-C++ compiler ("Bloodshed") and I used it throughout my last-term C++ programming course, so I know that you have to put "system("PAUSE")" or "cin.get()" before your last return statement in the program to get everything to stay on-screen long enough for you to see.

I tried your program, and it still gives me the error I previously described - jumps to the "You must select a student type" error message without the user entering any data into the program... I type "Yes" and it jumps to that.

... I've just been following all the other exercises in the book, and they've all worked without any problems. And what this exercise is is a modification of a previous exercise, the major mod being adding the while loop.

Still completely stymied,

-'Mage
  #9  
Old 19-Jun-2006, 14:58
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 376
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: Grades Calculation Program


Did you try my version?
  #10  
Old 19-Jun-2006, 15:05
UnWarierMage UnWarierMage is offline
New Member
 
Join Date: Jun 2006
Posts: 5
UnWarierMage is on a distinguished road

Re: Grades Calculation Program


Blake:

Tried it, still got the same error.

-'Mage
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to read particular memory location ? realnapster C Programming Language 10 10-May-2006 09:11
Student ID/Grades program help... kakamuti C Programming Language 13 13-Jan-2006 18:26
creating a file in [c] i hate c C Programming Language 15 21-Nov-2005 13:52
Type casts ? kai85 C++ Forum 12 23-Jun-2005 12:04
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 08:10

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 10:43.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.