0

I am trying to create a program that computes a students grades and gives you the result. I am doing this as part of a task from a book called "Accelerated C++".

The problem I am encountering at the moment is that I enter mid term and final exam scores as well as homework scores and it seems to calculate the final grade. However it closes before I can read it. I tried adding a pause using cin.get(); at the end but it didn't work.

#include <iomanip>
#include <ios>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using std::cin;             
using std::cout;
using std::endl;
using std::setprecision;
using std::string;
using std::streamsize;
using std::vector;
using std::sort; 


int main()
{
//ask for and read the students name
cout << "Please enter your first name: ";
string name;
cin >> name;
cout << "Hello, " << name << "!" << endl;

//ask for and read the midterm and final grades 

cout << "Please enter your midterm and final exam grades: ";
double midterm, final;
cin >> midterm >> final;

//Ask for their homework grades 

cout << "Enter all your homework grades, "
    "followed by end-of-file: ";

vector<double> homework;
double x;
// Invariant: Homework contains all the homework grades read so far
while (cin >> x)
    homework.push_back(x);

// Check that the student entered some homework grades
typedef vector<double>::size_type vec_sz;
vec_sz size = homework.size();
if (size == 0) {
    cout << endl << "You must enter your grades. "
        "Please try again." << endl;
    return 1;
}

// Sort the grades 
sort(homework.begin(), homework.end());

// Compute the median homework grade
vec_sz mid = size / 2;
double median;
median = size % 2 == 0 ? (homework[mid] + homework[mid - 1]) / 2
    : homework[mid];


// compute and write the final grade
streamsize prec = cout.precision();
cout << "Your final grade is " << setprecision(3)
    << 0.2 * midterm + 0.4 * final + 0.4 * median
    << setprecision(prec) << endl;

cin.get();

return 0;

}

Is there a way to add a pause at the end so that I can see the result? Any help would be greatly appreciated. The code it exactly the same as the book. I just don't understand why it isn't working. Can anyone help?

Regards

PapaSmurf
  • 161
  • 4
  • 15
  • Don't do it in your code. Tell your IDE to pause it or run it from the command line if not running from your IDE. – chris Aug 26 '14 at 22:51
  • Instead of running the program from the IDE, compile it in the IDE then run it from a command window. The command window will stay open after the program ends. – Mark Ransom Aug 26 '14 at 23:11
  • Search Stackoverflow for "C++ console pause" - numerous duplicates exist for this question. You do not need to run it separately from your IDE if you are running Visual Studio. See [this answer](http://stackoverflow.com/a/1152873/1227469). – JBentley Aug 26 '14 at 23:20
  • possible duplicate of [How to keep the console window open in visual c++?](http://stackoverflow.com/questions/454681/how-to-keep-the-console-window-open-in-visual-c) – JBentley Aug 26 '14 at 23:22
  • I did this and the program now works with ctrl + F5 however it does not work outside of the IDE. Any ideas why? – PapaSmurf Aug 26 '14 at 23:34

2 Answers2

1

You have to clear the stream state before using the stream again. The input operation that happened prior to cin.get() (i.e while (cin >> x)) continued to run until the stream state was no longer in a non-fail state. You need to clear() the stream state for it to be used again for I/O:

std::cin.clear();
std::cin.get();
David G
  • 94,763
  • 41
  • 167
  • 253
  • I have added this to the bottom of the code just before return 0; However it still doesn't seem to pause the application. Any ideas why? – PapaSmurf Aug 26 '14 at 23:04
  • Try discarding input: `cin.clear(); cin.ignore(numeric_limits::max(), '\n'); cin.get()` – David G Aug 26 '14 at 23:37
0

You are asking your user for an end of file to end the input. Once this end of file is provided, your cin stream does no longer accept input. May be you find another way to end your data entry loop !

cout << "Enter all your homework grades, "
    "followed by -1 : ";

...
while ((cin >> x) && x>=0)
...
         // the enter after your last number would let get() return, so:  
cin.ignore(std::numeric_limits<streamsize>::max(), '\n');  // clear input until '\n' 
cin.get();

Variant, with a string based input loop:

As you know alread strings, you can opt for a line by line entry. So you'd read a string per line. This allows you to check if there's an empty line and exit the loop. The drawback of this approach is that you must convert your string to a numer.

cin.ignore(std::numeric_limits<streamsize>::max(), '\n');  // clear previous '\n' 
cout << "Enter all your homework grades, "
    "followed by an empty line : ";
...
string str; 
while (getline(cin, str) && str!="") 
    homework.push_back(stod(str));  // convert string to double
...  // and end of programme as above
Christophe
  • 68,716
  • 7
  • 72
  • 138
  • That is the way that the book taught me to do it. Is there a better way to it? I'm just a beginner. :) – PapaSmurf Aug 26 '14 at 23:07
  • Oh Sorry ! Then the easier way is the solution proposed by 0x499602D2 which clears the error flags of the stream. I proposed a solution avoiding the entry of an end-of-file on cin, because this practice doesn't allow programmes to properly use file redirection in command line mode. Also, on some legacy systems, entering an end-of-file on console could have weird effects such as freezing the console (fotunately this is not a problem in windows!). – Christophe Aug 26 '14 at 23:21
  • I have tried ending the data loop this way and it still doesn't pause to let me see the result. – PapaSmurf Aug 26 '14 at 23:21
  • I've tested it and it works... It doesn't work only there is another input error (such as a blank between - and 1, or an end of file, or if there's non numeric input, like columns to separate the entered numbers). – Christophe Aug 26 '14 at 23:30
  • Oh yes, I have got it to work now, is there anyway to change it so that you don't have to enter -1 and have it show on the console? I am very thankful as my program now works, I am just curious. – PapaSmurf Aug 26 '14 at 23:43
  • Certainly. You could first ask how many marks and then stop asking when you've got sufficient input. Or if you you could opt for line by line input with `getline (cin, str)` - I'll do an edit to show. – Christophe Aug 27 '14 at 00:06
  • I know I'm not supposed to comment to say "thanks" but you have been so much help. I really appreciate it, as a beginner when the book that is teaching you doesn't work. It's a bit daunting. :) – PapaSmurf Aug 27 '14 at 00:53