What you do looks rather confuse, use the debugger to see what your code is doing.
if (gcount <= 100 && gcount >= 90) {
cout << "A:" << gcount << endl;
}
else if (gcount <= 89 && gcount >= 80) {
cout << "B:" << gcount << endl;
}
else if (gcount <= 79 && gcount >= 70) {
cout << "C:" << gcount << endl;
}
else if (gcount <= 69 && gcount >= 60) {
cout << "D:" << gcount << endl;
}
else if (gcount <= 59 && gcount >= 0) {
cout << "F:" << gcount << endl;
}
Independently from other bugs, your code fail when gcount is 89.5 or 69.2
The code can handle this case with simplifying because all the
if
and
else if
us a single structure that you need to learn.
assuming gcount is between 0 and 100:
if (gcount >= 90) {
cout << "A:" << gcount << endl;
}
else if (gcount >= 80) {
cout << "B:" << gcount << endl;
}
else if (gcount >= 70) {
cout << "C:" << gcount << endl;
}
else if (gcount >= 60) {
cout << "D:" << gcount << endl;
}
else if (gcount >= 0) {
cout << "F:" << gcount << endl;
}
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.
The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[
^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[
^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
Here is links to references books on C and C++ by the authors of the languages. Note than C is the ancestor of C++, so knowing C is always useful with C++.
The C Programming Language - Wikipedia, the free encyclopedia[
^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[
^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[
^]
C++ Programing Language[
^]