Okay, basically I have a grades array which takes 6 grades from a file, like this:
for (int x = 0; x%26lt;6; x++)
{
fin %26gt;%26gt; grades[x];
if (grades[x] %26lt; 0)
{
cout %26lt;%26lt; "Error: Negative Grade detected. Terminating program.";
system("PAUSE%26gt;NUL");
return 0;
}
}
Simple enough, right? Anyway, I'm supposed to submit an error if I have only 5 grades in the input file. Problem is, it just makes my program loop indefinitely. What's a way that I can check to see that the sixth value is a proper grade or not?
Array question in C++?
Using the %26gt;%26gt; operator returns the input stream. If the input stream has reached the end, then it evaluates to false in a condition test.
Assuming there is nothing in your input file after the grades, you could do something like:
for (int x = 0; x %26lt; 6; x++)
{
if (!(fin %26gt;%26gt; grades[x]))
{
cout %26lt;%26lt; "Error: Less than 6 grades in input file";
system("PAUSE%26gt;NUL");
return 0;
}
if (grades[x] %26lt; 0)
{
cout %26lt;%26lt; "Error: Negative Grade detected. Terminating program.";
system("PAUSE%26gt;NUL");
return 0;
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment