if statement doesn't work

I compiled and ran the following program without success. I finally gave up and tried running the one that came with the textbook (Accelerated C++) and still no luck. Is the program wrong or am I doing something incorrectly?? I added system ("pause") to "freeze" the console (windows)


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

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

int main()
{
// ask for and read the student's 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 and read the 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;
system("pause");
return 0;
}

How is the program behaving? Does it compile? Do you get an error message?

Please use [ code ] [ /code ] tags (without the spaces) next time.
What's the error message? The code works fine for me.
The program runs fine... What's your problem? (note that end-of-file means control+Z)
ran the following program without success.

What does that mean?

no luck

No luck with what?
The if statement doesn't run, when size==0.......console dissapears when data entered is incorrect or empty.

please help
Last edited on
How are you expecting the user to indicate that he has finished typing in all his grades?
end-of-file
You are doing nothing to pause the console if the list is empty.

Try something like this:

1
2
3
4
5
6
7
8
	if(size == 0)
	{
		cout << endl << "You must enter your grades. "
			"Please try again." << endl;
		cin.clear(); // clear end of file flag
		cin.ignore(); // wait for key press
		return 1;
	}
I tried this with and without system("pause") I still do not get the string lateral to apear when size==0.....Could it be the compiler itself (Dev-C++)??

Where you able to run the program while size==0 with the following message on the console?:

you must enter your grades.
Please try again
Yes, it works fine for me. Prints the message in full.

You must enter your grades. Please try again.
Do you think it could be my compiler?
You could try it on a different compiler or a different version of the same compiler.
Could you recommend an alternative? I am currently using Dev C++
Last edited on
Try CodeBlocks
Thanks
1
2
3
4
5
if (size == 0) {
cout << endl << "You must enter your grades. "
"Please try again." << endl;
return 1;
}



Whether the console dissapears or not depends on your IDE. - some of them keep the console
open during a debugging run after the program terminates, others closes the console.

The OP's closes the console - he can use cin.get(), or even system("pause") - yes, I know, Iknow - to keep the console open.

Topic archived. No new replies allowed.