Multiple variables in one equation Ok so I'm working with parrel arrays and I'm needing to calculate how many points are needed for each grade with the numberOfPoints array while doing it in one equation that uses variables that change each time through the loop. I'm not allowed to use 5 assignment operators so I'm kinda stuck on how to calculate and display that, any help would be appreciated!

 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 #include #include #include using namespace std; int main() { // DECLARE ARRAYS char letters = { 'A', 'B', 'C', 'D', 'F' }; int numberOfPoints = { 0 }; //DECLARE VARIABLES double totalPoints = 0.0; double points = 0.0; double grades = 0.0; bool found = false; string name = " "; //STATEMENTS cout << "Enter The Name Of The Student: " << endl; getline(cin, name); cout << "------------------------------------------------------------------------- " << endl; cout << "Please Enter The Total Possible Points: " << endl; cin >> totalPoints; cout << endl; cout << "Please Enter The Actual Points Recieved: " << endl; cin >> points; cout << endl; //LOOP while (totalPoints > points) { numberOfPoints = totalPoints * .9; numberOfPoints = totalPoints * .8; numberOfPoints = totalPoints * .7; numberOfPoints = totalPoints * .6; numberOfPoints = totalPoints * .5; } //MORE LOOPS for (int x = 0; x < 5 && found == false; x++) { if ( points >= numberOfPoints[x]) { cout << "Your Grade is: " << numberOfPoints[x] << ": Your Letter Grade Is: " << letters[x] << endl << endl; found = true; } } system("pause"); if (found == false) { system("cls"); cout << "Invalid Option, Please Try Again" << endl; } return 0; } I normally don't like to just do it for you but this is a little tricksy and once you have seen it, you will understand a lot. if you can't use a for loop.. keep reading.
for(int i = 1; i < 6; i++)
{
numberOfPoints[i-1] = totalPoints * (1.0 - (double)(i)/10.0);
}

a while loop:
int i{1}; //the for loop did this for us
while(i != 6) //while only has the condition part of the loop
{
as above, but you also need
i++; //the for loop did this for us
}

which an observant coder may note that a for loop, then, if missing the front and end parts, is a while loop:
for(; condition ;) ahh You're a lifesaver, thank you so much, I don't believe I've learned the (double)(I) does that make the I a double now?
Last edited on it is a 'cast' -- actually, its a lazy C style cast. It makes the i variable be treated as a double briefly. It probably isnt necessary there, sometimes I put them in without bothering to check if its 100% required.

https://en.cppreference.com/w/cpp/language/explicit_cast

C casts are just (type) in front of the variable as I showed. The link explains how that is unraveled as well. Oh, ill check this out then thanks for all your help! You don't in this case need to cast i to a double - as int / double gives a double as does double / int. Ony int / int gives int result. As 10.0 is used, this is a double as opposed to 10 which is in int. hmmm I tried (1 - (i) / 10) but it came out to nothing, I'm not sure why though cause isn't that the same as basically (1.0 - (double)(I) / 10)? no, as seeplus said, that is integer / integer.

5/10 is zero.
5/10.0 is 0.5

one of the arguments needs to force the compiler to do the math in double space instead of integer space. 10.0 will do that. (double)i/10 will do it. I doubled down above (hahah punz) and did it on both sides. This will bite you again if you don't remember it. Its very easy to forget and do int/int and lose your value.

its true on the decimal side too.
100/3 is 33
100/3.0 is 33.33333... if you need the precision, you have to work in floating point.
Last edited on Thank you for all your help on this! it had me kinda confused but I think it's making some more sense.
Topic archived. No new replies allowed.