Thread: Dynamic memory. Output problems.

  1. #1
    Registered User
    Join Date
    Mar 2015
    Location
    Toronto, Ontario, Canada
    Posts
    9

    Dynamic memory. Output problems.

    Hello there. I'm working with dynamic memory right now and I wrote a code. I got a problem with my output and dont know how to fix it. I've 3 files w3.cpp weather.h weather.cpp. I assume that the problem is in the w3.cpp file. Could you help me to fix it? The problem : every time I run my code. The second/3rd/4th etc date is missing. The compiler doesnt let me to input something.

    Code:
    //My input/output
    
    Weather Data
    =====================
    Days of Weather: 3
    Enter date:
    May 10
    Enter high:
    15
    Enter low :
    10
    Enter date: // THIS IS MY PROBLEM
    Enter high:
    19
    Enter low :
    15
    Enter date:
    Enter high:
    20
    Enter low :
    19
    
    
    Weather report:
    ==================
    May 10 15  10  
    
    
     19  15  
    
    
     20  19
    Code:
    //w3.cpp
    
    //w3.cpp
    //Last changes : 30/5/2015
    #include <iostream>
    #include <string.h>
    #include "weather.h"
    using namespace std;
    
    
    int main(){
        
        int n;
        Weather* weather = NULL ;
        
        cout << "Weather Data\n";
        cout << "=====================" << endl;
        cout << "Days of Weather: ";
        cin >> n;
        cin.ignore();
        // allocate dynamic memory here
        
        weather = new (nothrow) Weather[n];
        
        
        
        for(int i = 0; i < n; i++){
            char date_description[31];
            double high=0.0, low=0.0;
      
            cout << "Enter date:\n";
            
            cin.getline(date_description,31);
            //cin >> date_description;
            cout << "Enter high:\n";
            cin >> high;
            cout <<"Enter low :\n";
            cin >> low;
            
            weather[i].set(date_description,high,low);
            strcpy( date_description, "" );
        }
        cout << endl;
        cout << "Weather report:\n";
        cout << "==================" << endl;
        
        for(int i = 0; i < n; i++){
            weather[i].display();
            cout << endl;
        }
        
        
        delete[] weather;
        weather = NULL;
    
    
        return 0;
        
    }
    // cout<<left<< setfill(" ")<<variables;
    Code:
    //weather.h
    //Last changes: 30/05/2015
    class Weather{
        char _weather_array[31]; 
        double _high;
        double _low;
    public:
        void set(char (&date_description)[31],double &high,double &low);
        void display() const;
    };
    Code:
    //weather.cpp
    //Last changes : 30/05/2015
    #include <iostream>
    #include <string.h>
    #include "weather.h"
    
    
    using namespace std;
    
    
    void Weather::set(char (&date_description)[31],double &high,double &low){
        strcpy (_weather_array,date_description);
        /*for (int i = 0; i<31;i++){
        _weather_array[i] = date_description[i];
        }*/
        _high = high;
        _low = low;
    }
    
    
    void Weather::display() const{
        cout << _weather_array<<" "<< _high<<"  "<<_low<<"  "<< endl;
    }
    Last edited by qwes3r; 06-02-2015 at 05:45 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Here's the problem:
    Code:
        for(int i = 0; i < n; i++){
            char date_description[31];
            double high=0.0, low=0.0;
       
            cout << "Enter date:\n";
             
            cin.getline(date_description,31);
            //cin >> date_description;
            cout << "Enter high:\n";
            cin >> high;
            cout <<"Enter low :\n";
            cin >> low;
             
            weather[i].set(date_description,high,low);
            strcpy( date_description, "" );
        }
    So the first time around, everything will work. The second time around, you have to consider that the newline was left in the stream after
    Code:
            cout << "Enter high:\n";
            cin >> high;
            cout <<"Enter low :\n";
            cin >> low;
    So when you finally get to cin.getline() the second time around, it consumes the \n in the stream and returns immediately.

    You seemed to know about this problem earlier in the code:
    Code:
        cout << "Days of Weather: ";
        cin >> n;
        cin.ignore();
    Another way to do it that might be simpler is to read in strings, and then convert to integers always.

    Code:
    strcpy( date_description, "" );
    You do not need to do this. The old date_description will fall out of scope and be destroyed at the end of the iteration, and then recreated at the beginning of the next. Even if that wasn't going to be the case, the string can just be overwrote by cin.getline().

  3. #3
    Registered User
    Join Date
    Mar 2015
    Location
    Toronto, Ontario, Canada
    Posts
    9
    Quote Originally Posted by whiteflags View Post
    Here's the problem:
    Code:
        for(int i = 0; i < n; i++){
            char date_description[31];
            double high=0.0, low=0.0;
       
            cout << "Enter date:\n";
             
            cin.getline(date_description,31);
            //cin >> date_description;
            cout << "Enter high:\n";
            cin >> high;
            cout <<"Enter low :\n";
            cin >> low;
             
            weather[i].set(date_description,high,low);
            strcpy( date_description, "" );
        }
    So the first time around, everything will work. The second time around, you have to consider that the newline was left in the stream after
    Code:
            cout << "Enter high:\n";
            cin >> high;
            cout <<"Enter low :\n";
            cin >> low;
    So when you finally get to cin.getline() the second time around, it consumes the \n in the stream and returns immediately.

    You seemed to know about this problem earlier in the code:
    Code:
        cout << "Days of Weather: ";
        cin >> n;
        cin.ignore();
    Another way to do it that might be simpler is to read in strings, and then convert to integers always.

    Code:
    strcpy( date_description, "" );
    You do not need to do this. The old date_description will fall out of scope and be destroyed at the end of the iteration, and then recreated at the beginning of the next. Even if that wasn't going to be the case, the string can just be overwrote by cin.getline().
    Thank you SO MUCH! I fixed the code and now it works! Thanks for the tips also

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By the way, I noticed:
    Code:
    weather = new (nothrow) Weather[n];
    I suggest that you do not use nothrow new. Rather, either catch std::bad_alloc or allow it to propagate. If you do want to use nothrow new, then you should check that weather is not a null pointer before proceeding to dereference it.

    Actually, it would be better if you just used a std::vector<Weather>. For practice with dynamic memory allocation without using standard containers, write a WeatherContainer class (or something like that) that does the RAII.

    Also, instead of an array of 31 char to stor a null terminated string, have you considered std::string?

    Note that your headers should have inclusion guards.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with dynamic memory and strings
    By ProgrammingGirl in forum C Programming
    Replies: 3
    Last Post: 10-08-2014, 02:16 PM
  2. Dynamic memory and realloc(), freeing memory
    By C_Sparky in forum C Programming
    Replies: 6
    Last Post: 10-06-2010, 07:55 PM
  3. dynamic output; changing without reprinting
    By frog in forum C++ Programming
    Replies: 5
    Last Post: 09-20-2010, 11:12 AM
  4. static memory and dynamic memory
    By nextus in forum C++ Programming
    Replies: 1
    Last Post: 03-01-2003, 08:46 PM
  5. Ascii conversion problems with dynamic memory
    By Butters in forum C++ Programming
    Replies: 2
    Last Post: 01-29-2002, 12:22 PM