Thread: Noob in need of help.

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Phyxashun
    The better solution (to some) would be to make all the variable doubles.
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    Jan 2009
    Posts
    17
    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;
        
        
    }

  3. #18
    Registered User
    Join Date
    Dec 2008
    Posts
    2
    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.

  4. #19
    Registered User
    Join Date
    Jan 2009
    Posts
    17
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob Q: How to read a value of a byte in binary?
    By Hitsuin in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 02:46 PM
  2. Noob printf question
    By lolguy in forum C Programming
    Replies: 3
    Last Post: 12-14-2008, 08:08 PM
  3. I'm a noob and I need help.
    By nifear4 in forum C Programming
    Replies: 17
    Last Post: 10-14-2008, 01:20 PM
  4. noob needs help!!!!:(
    By tykenfitz in forum C Programming
    Replies: 1
    Last Post: 07-10-2005, 08:49 AM
  5. noob: compiling error... plz help!
    By chosii in forum C Programming
    Replies: 2
    Last Post: 05-10-2002, 05:53 AM