Hello Arcad31a,
Since you have not marked this as being finished I will add this that might be helpful.
Looking at your code I have the feeling that you do not have a good understanding of scope. These are worth reading:
http://www.cplusplus.com/doc/tutorial/namespaces/
https://www.learncpp.com/cpp-tutorial/4-3a-scope-duration-and-linkage-summary/
and the main page of
https://www.learncpp.com/
When you write:
1 2 3 4
|
if (test1 >= 90)
{
grade1 = 'A';
}
|
The {}s define a new scope. So when you write:
1 2 3 4
|
if (test1>=90)
{
char grade1='A';
}
|
This is different from what you defined on line 13 and because it is defined as a local variable to the if statement using the same name is not a problem. The problem is that what you defined on line 13 is not being used because of the local variable and when you reach the closing } of the if statement the variable and it value are lost.
As
highwayman has pointed out by not defining "grade1" and the othere inside the scope of the if statement you will be using the variables defined on line 13.
Some things I have found over the time I have been here:
Be careful about naming your functions the same as functions that already exist, like "srand". I think the only reason it is working is that there is no function definition for "srand()" that you wrote and that the prototype is the same as the function from "cstdlib".
Speaking of "srand this is a better way to write the function call:
srand(static_cast<size_t>(time(nullptr)));
. As noted in your prototype "srand takes an "unsigned int", but "time" does not return an unsigned number. Somewhere I read this and now am not sure where I found it, but for the parameter for time zero(0) or (NULL) may work, but (nullptr) is the better choice.
If for no other reason than to find out the problems of using "rand" this video is helpful:
https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful
For actually using "rand" you may find it useful to call "rand" before your first actual use to generate a better random number. Something I once read mentioned doing this when using the "Windows" operating system.
1 2
|
rand();
float test1 = rand() % 100;
|
Two things about this bit of code:
"rand()" returns an "int" not a "float". Although not a problem there is no need for the float. Also "double" is the preferred floating point type. Now if you are looking for a floating point answer you will need to do some division on the rhs of the = to get that kind of an answer.
Second using "% 100" will give you a return value between zero(0) and (99). If you add one it will allow a result of (100), but you will loose (0). If you want (0) - (100) you will need to use "% 101" here.
Your if statements:
1 2 3 4
|
if (test1 >= 90)
{
grade1 = 'A';
}
|
Are repeated five different times, with a slight variation, and screams for a function. The code can very easily be changed to make it generic and the function can be called every time you need a letter grade.
If you are not up to writing separate functions yet that is OK as the code you have will work, but could be shortened. If you need some help writing a function let me know.
Near the end of the program you have
float average = (test1 + test2 + test3 + test4) / 4;
. First off it would be better to write this as
float average = (test1 + test2 + test3 + test4) / 4.0;
so the compiler does not have to promote the (4) to (4.0). Just a small thing, but it helps. And if by chance you change the (test) variables to "int"s to work with "rand()" you would basically be doing integer division which would leave you with only a whole number in "average".
But sometimes this line of code will return a number with a decimal value, so it does have its use on occasion.
Of the testing I did when there was a decimal part to the "average" it was only two numbers. I do not know if you have learned about this yet, but if you include the header file "<iomanip>" and this line of code:
std::cout << std::fixed << std::showpoint << std::setprecision(2);
your output will always be rounded to two decimal places and it will print ".00" if needed. It tends to look better when the output is consistent.
The above line of code only needs to be done once, so I would put it after the call to "srand". This will affect every "cout" statement until it is changed.
Hope that helps,
Andy