Noob in need of help.

This is a discussion on Noob in need of help. within the C++ Programming forums, part of the General Programming Boards category; Noob needs help. I have a situation where I'm asked to ask the user to enter in the number of ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    17

    Noob in need of help.

    Noob needs help.

    I have a situation where I'm asked to ask the user to enter in the number of cities to calculate an average temperature for and then use each of the 3 basic loop structures.

    Now where I am is this. I ask the suer how many cities? Then I have a basic for loop wich is working. I can't seem to figure out how to get the program to add up all the temps to do the average. I am also not sure bout the variables.

    I'm very new to this. Here is my code

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        //declare variables
        int Temp = 0;
        int averageTemp = 0;
        int numCities = 0;
        //double sum = 0; not sure?
        
        cout << "Enter the number of cities you would to average: ";
        cin >> numCities;
        
            for ( int i=0; i<=numCities; i++ )
            {
                 cout <<"Enter the Temp: ";
                 cin >> Temp;
            }   
           //for ( int Temp=0; //this is where I'm stuck
                //averageTemp = (temp + sum) /numCities;
                  cout << "Average temp: " << averageTemp <<endl;
          
        
        
        
        system("pause");
        return 0;

  2. #2
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Since you want more than one temperature
    Code:
     cin >> Temp;
    won't work, since you'll only replace the current inputted temperature with the newly inputted one, you should use a vector for this
    Currently research OpenGL

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    17

    Angry

    That makes sense. But I have to kinda stay on what we have learned so far. And this is only my 3rd lab in this class. We havn't covered vectors. Is there a more basic way to accomplish this? I am at work and do not have my book. Wanted to get a jump on this so I'm not up to late tonight.

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    If all you need to do is print the average, then a vector or array is not necessary. Instead you would do the following:

    Create a variable to store the total temperature and initialize it to 0.
    Read number of cities into another variable.
    Loop that many times asking for the temperature, adding the entered result to the total.
    Divide total by the number of cities to get average.

  5. #5
    Edo
    Edo is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    8
    Heya, I'm new too! The problem here is that every time the user enters the temperature, it gets written to Temp -- you are accidentally overwriting Temp every time.

    Code:
            for ( int i=0; i<=numCities; i++ )
            {
                 cout <<"Enter the Temp: ";
                 cin >> Temp; // <---- right here
            }
    You're also going to have an extra city because of this:
    Code:
     for ( int i=0; i<=numCities; i++ )
    If the user entered 1 city, the code would run once at i = 0, then again at i = 1 ...


    Something like this would work better:
    Code:
            int totalTemp = 0;
            for ( int i=0; i<numCities; i++ ) // i < numCities instead of <=, because it's starting at 0 instead of 1
            {
                 cout <<"Enter the Temp: ";
                 cin >> Temp;
                 totalTemp = totalTemp + Temp;  
            }
            averageTemp = totalTemp / numCities;
            cout << "Average temp: " << averageTemp <<endl;

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    17
    Perfect! Thank you.

    I just changed a couple variables to double to accomadate for decimal and were in buisness. I thought I tried somethig similar but I'm sure I had something wrong. I am only 3 classes in my first programming class ever so I'm still real green But I like it a lot and love learning!

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    17
    Now I have to figure out how to do the same loop in a while and do while loop.
    Here is the code I added for my while loop but it's not doing the average for the while loop. adding the temps together it seems and spitting that out as the averate. Here's the code.

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        //declare variables
        int Temp = 0;
        double averageTemp = 0;
        int numCities = 0;
        double totalTemp = 0;
        int i = 0;
        
        cout << "Enter the number of cities you want to average: ";
        cin >> numCities;
        
        for ( int i=0; i<numCities; i++ )
            {
            cout <<"Enter the Temp: ";
            cin >> Temp;
            totalTemp = totalTemp + Temp;
            }   
            
            averageTemp = totalTemp / numCities;
            cout << "Average temp: " << averageTemp <<endl;
    
        
           
        while ( i<numCities)
            {
            cout <<"Enter the Temp: ";
            cin >> Temp;
            totalTemp = totalTemp + Temp;
            i++;
            }      
              
              averageTemp = totalTemp / numCities;
              cout << "Average temp: " << averageTemp <<endl;
            
        
        
        
        system("pause");
        return 0;
        
        
    }

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And why do you believe that totalTemp is 0 when you start the while loop?

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    17
    Do you mean when I declared my variables? I'm a bit unclear when I should initialize them to a value and when I shouldn't.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by gec5741 View Post
    Do you mean when I declared my variables? I'm a bit unclear when I should initialize them to a value and when I shouldn't.
    All variables need to be assigned a value before you "use them" - that is, before you compare it with something else (including zero) or use it for calculations. Otherwise it will contain some random garbage [1], and most likely not do at all what you expect.

    [1] It is not random in the sense that a random number is, but rather in the sense that "you can't tell me what it will be without knowing A LOT about what goes on before you get to the usage of the variable", and also, it may well change if you add or remove some other variables around the variable we talk about. Some compilers intentionally fill undefined variable memory with some recognizable pattern - in which case the value is just some "strange number" every time.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Dec 2008
    Posts
    65
    What you want is something like this. Notice the reinitialization of sumTemp to 0.

    Code:
    #include <iostream>
    
    int main()
    {
        int    cityTemp    = 0;
        double avgTemp     = 0;
        int    numCities   = 0;
        double sumTemp     = 0;
    
        std::cout << "Enter the number of cities you want to average: ";
        std::cin >> numCities;
    
        for (int i = 0; i < numCities; i++)
        {
            std::cout << "Enter the Temp: ";
            std::cin >> cityTemp;
            sumTemp += cityTemp;
        }
        avgTemp = sumTemp / numCities;
        std::cout << "Average temp: " << avgTemp << std::endl;
    
        int counter = 0;
        sumTemp   = 0;
        while (counter <= numCities)
        {
            std::cout << "Enter the Temp: ";
            std::cin >> cityTemp;
            sumTemp += cityTemp;
            counter++;
        }
        avgTemp = sumTemp / numCities;
        std::cout << "Average temp: " << avgTemp << std::endl;
    
        return 0;
    }

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by gec5741 View Post
    Do you mean when I declared my variables? I'm a bit unclear when I should initialize them to a value and when I shouldn't.
    I did not mean when you declared your variables, as Phyxashun pointed out -- before the while loop is not where you declared your variables.

    To answer your question, you need to initialize variables to a value if you intend to use them. For instance, you don't have to initialize numCities, because you're not using it before your read into the variable. Same with temp. But you do have to initialize totalTemp, because the first thing you do with it is use it (totalTemp + temp).

  13. #13
    Registered User
    Join Date
    Jan 2009
    Posts
    17
    Thank you,

    I think i'm starting to see. Weird thing is the while loop dosent' seem to want to put the avreage in a decimal if needed. it's averageing a temp of 2 and 3 as average = 3. Where the for loop will display it as 2.5.

  14. #14
    Registered User
    Join Date
    Jan 2009
    Posts
    17

    Talking

    I'm getting there. I have the while loop working now. But my do while loop won't bump the appriate amount of times. I'll get it.

  15. #15
    Registered User
    Join Date
    Dec 2008
    Posts
    65
    The easy solution is to use a cast:

    Code:
    avgTemp = (double)sumTemp / (double)numCities;
    The better solution (to some) would be to make all the variable doubles.

    Both would give you the fraction you are looking for.

Page 1 of 2 12 LastLast
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, 07: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21