Not all, since numCities would be more appropriate as an unsigned integer. As the temperature values might be fractional, it makes sense to just declare those variables as double to begin with.Originally Posted by Phyxashun
Not all, since numCities would be more appropriate as an unsigned integer. As the temperature values might be fractional, it makes sense to just declare those variables as double to begin with.Originally Posted by Phyxashun
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
When I get to the do while loop it just asks for the temp one time and it looks like it's just taking the on entry and subtracting 1?
Code:#include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]) { //declare variables int cityTemp =0; double avgTemp =0; int numCities =0; double sumTemp =0; int counter =0; cout << "Enter the number of cities you want to average: "; cin >> numCities; for (int counter=0; counter<numCities; counter++) { cout <<"Enter the Temp: "; cin >> cityTemp; sumTemp += cityTemp; } //end for avgTemp = sumTemp / numCities; cout << "Average temp: " << avgTemp <<endl; sumTemp = 0.0; while ( counter < numCities ) { cout <<"Enter the Temp: "; cin >> cityTemp; sumTemp += cityTemp; counter++; } //end while avgTemp = sumTemp / numCities; cout << "Average temp: " << avgTemp <<endl; sumTemp = 0.0; do { cout <<"Enter the Temp: "; cin >> cityTemp; sumTemp += cityTemp; counter++; } while ( counter < numCities ); avgTemp = sumTemp / numCities; cout << "Average temp: " << avgTemp <<endl; system("pause"); return 0; }
You are having the same problem as before. After the while loop, the counter is equal to the number of cities, so the do..while loop just runs through once and then exits.
You didn't have to reset the counter before the while loop because you initialized a new variable with the name counter inside of the for loop.
Great thanks! I see what your saying and what I did wrong. Makes sense. What took me a while to figure out is I was trying to initialize counter = 0 by typing "int counter = 0;
guess I didn't need to declare it an intiger again. I wrote it just as counter = 0; Well it's working now. So thanks everyone for all the help!