Hello heart1210,
Let me cover some things that you may find helpful in the future.
On the main page of the forum the first post
http://www.cplusplus.com/forum/beginner/1/ is worth reading and more than once.
I am not sure where I first read it, but when you make your first post it helps everyone to mention whet IDE/compiler you are using.
If you are asking about something that comes from a school assignment it is best to post the instructions that you were given and not what you think they mean because someone else may see a question that you may need to have answered or that you may have to clarify.
If you do not know what is wrong with your code then you will not know what to fix or understand some of the suggestions that you are given.
I will refer to the last revised code that you posted.
I realize that these are your changes, but it is always best to post enough code that can be compiled and tested. It is easy enough for you to copy and paste, so do not make others fill in what you missed. It is also possible that something may be seen in what you left out that needs a mention.
Looking at the code:
Lines 1 and 2 were better in your first code. Defining these variables as "int"s is a step backwards. As you read the file the first number (85.56) is a floating point number, i.e., a double, that you are storing in an "int". This will work, but since an "int" only stores whole numbers the decimal part will be dropped. Not quite what you want.
The next problem is the variable names. In your first code not only are the variables defined with the correct type they have better names than "x" and "y". "score" is a good name, but "total" is misleading. It makes me thing that you are keeping a running total of something. For this variable "totalPoints" or pointsTotal" would work better.
For your first 3 variable it is always a good idea to initialize the variables. From C++11 on you have the uniform initializer, the {}s, empty the compiler will choose the best value to initialize the variable based on its type. The added advantage is that you can put a value between the {}s.
"strings", "vectors" and other containers are empty when defined and do not need to be initialized unless you want to give it a starting value.
Line 4 is a good start, but you forgot the variable name. If you tried to compile this I would expect an error.
The way you have used lines 6 and 12 is fine. It works. A suggestion or alternative that I normally use:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
Line 3 will create an object of "ifstream" and what is in the () will use the overloaded ctor to open the file at the same time. This sames you a line of code.
const std::string IN_FILE_NAME{ "" }; // <--- Put File name here.
std::ifstream inFile(IN_FILE_NAME);
if (!inFile)
{
std::cerr << "\n File " << std::quoted(IN_FILE_NAME) << " did not open" << std::endl;
//std::cerr << "\n File \"" << IN_FILE_NAME << "\" did not open." << std::endl;
return 1;
}
|
If you are not comfortable with the "std::quoted" the commented line will do the same thing. "std::quoted" comes from the "iomanip" header file and unless someone tells you about it or you see it in use you may never know about it. It is handy, but not always necessary.
At line 11 and at line 42 the {}s are not necessary and could be potential problem. If you were to define a variable inside those {}s it would local to that block and would be destroyed at the ending }. So if you intended to use it outside of that block you could not because it is no longer available.
Between lines 19 and 20 you need a blank line.
Line 20 is not working the way that you may be thinking. Given an if statement, a while condition or the middle part of a for loop what is between the () or the (; ;) is something that is evaluated to either (0) zero or something other than 0. This is converted to a bool variable with (0) meaning false or 1 meaning true. So in this case the (inFile) would evaluate to true and the use of (!) would make it false.
Even if you fix line 20 there is still no need for it because the is no loop back to use it a second time. Changing line 20 to a while loop as I showed you would make better use of the code block.
Line 24 works as is, but sometimes it is better to use () to show your intent or to make sure what is done first.
grade = (score / total) * 100.0;
.
In C++ there is no "else if" statement. Most times it is written
else if (grade >= 80)
and this works just fine. The 1 problem you do have is
else (grade < 59.9);
"else" is not used with anything in (), so this either needs to be an "else if" or loose the () and everything in them. Also ending this line with a (;) makes the next line not part of "if/else if/else" chain, so the last instruction is to set
description = "Fail"
. Therefor it will not matter what the score is it will always say "Fail".
Line 37 your "cout" statement.
Normally I would put this near the top of the program:
|
std::cout << std::fixed << std::showpoint << std::setprecision(2);; // <--- Only needs done once.
|
All 3 are done at once and will stay that way until changed.
In your code the "fixed" is used twice the first one could be useful, but it is in the wrong place. The second is not needed. Since "fixed" comes after "grade" it has no affect on "grade" which could be output in scientific notation, which is the default for displaying a "double" or "float". In a loop the 2nd time around that would be different.
The next part:
setprecision(5) << fixed << " " << description << endl;
. The "setprecision" and "fixed" have no affect on output of a "std::string", They only work on "double"s. In a loop the 2nd time around "grade" would now output with 5 decimal places and then "round(grade)" would also have 5 decimal places.
You had a better start with your original code. I eventually came to this:
|
cout << "Grade " << std::setprecision(2) << grade << " " << setprecision(0) << round(grade) << "%" << " " << description << "\n\n";
|
The "setprecision(0)" will not print any decimal places, but will print the decimal point. I would revise to this:
std::cout << std::fixed;
, what should be near the beginning of the code. The default value is "noshowpoint" and when you say "std::setprecision(2)" it will print ".00" because you told it to print 2 decimal places. Then when you say "setprecision(0)" before "round(grade)" it will print the whole number and no decimal point.
Line 43. Closing the file stream may be good form, but is not necessary as the dtor of the class will close the file and destroy the variable before the function looses scope. Or in this case before the program ends.
Last is the "return 0;". Again good form, but no longer a requirement sa the program will return (0) zero unless you tell it otherwise.
Andy