Home char* element of class is empty
Reply: 0

char* element of class is empty

user5286
1#
user5286 Published in May 25, 2018, 8:58 am

I am relatively new to C++ and classes. When I pass a char* through to the class constructor and copy it into the m_make and m_model char*'s, they are still empty. They are not pointing to random bits of memory when I print them out, no data is outputted, just blank space. Constructor:

RentalCar::RentalCar(int year, char * make, char * model, float price, bool available)
{
       m_year = year;
       myStringCopy(m_make, make);
       myStringCopy(m_model, model);
       m_price = price;
       m_available = available;
}

EDIT:

void readFile(RentalAgencyADT * list, ifstream & file)
{
char letter;
char word[7];
int i = 0; //position in word
int j = 0; //position in RentalCar data
int k = 0; //position of line in RentalAgencyADT
int l = 0; //position of RentalAgencyADT
while((letter = file.get()) != EOF)
{
    if(letter == ' ' || letter == '\n')
    {
        int year;
        char make[7], model[7];
        float price;
        bool available;
        i = 0;
        if(k != 0)
        {
            switch(j)
            {
                case 0:
                year = atoi(word);
                j++;

                break;
                case 1:
                myStringCopy(make,word);
                j++;
                break;
                case 2:
                myStringCopy(model,word);
                j++;
                break;
                case 3:
                price = atof(word);
                j++;
                break;
                case 4:

                available = (bool) atoi(word);
                list[l].inventory[k - 1] = RentalCar(year,make,model,price,available);
                j = 0;
                k++;
                break;
            }
            clearWord(word);
            i = 0;
            if(k == 7)
            {

            }
        }
        else if(k == 0)
        {

            switch(j)
            {
                case 0:
                myStringCopy(list[l].name, word);
                j++;
                break;
                case 1:
                //intCopy((list[l].zipcode),word);
                //cout << list[l].zipcode << endl;
                for(int i = 0; i < 5; i++)
                {
                    list[l].zipcode[i] = word[i] - '0';
                    cout << list[l].zipcode[i];
                }
                j = 0;
                k++;
                break;
            }
            clearWord(word);
        }
        if(j == 4)
        {
            clearWord(make);
            clearWord(model);
        }
    }
    else
    {
        word[i] = letter;
        i++;
    }

}

}

Here is the RentalCar.cpp file:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "RentalCar.h"
using namespace std;
   char* myStringCopy(char * destination, char * source);
   RentalCar::RentalCar(int year, char * make, char * model, float price, bool available)
   {
       m_year = year;
       myStringCopy(m_make, make);
       myStringCopy(m_model, model);
       m_price = price;
       m_available = available;
   }
   char* myStringCopy(char * destination, char * source) 
   {
       int i = 0;
       while(source[i] != '\0')
       {
           destination[i] = source[i];
           i++;
       }
       destination[i] = '\0';


       return destination;
   }
   int RentalCar:: getYear()
   {
       return m_year;
   }
   void RentalCar:: setYear(int year)
   {
       m_year = year;
   }
   char* RentalCar:: getMake()
   {
       return m_make;
   }
   void RentalCar:: setMake(char * make)
   {
       myStringCopy(m_make, make);
   }
   char* RentalCar:: getModel()
   {
       return m_model;   
   }
   void RentalCar:: setMode(char * model)
   {
       myStringCopy(m_model, model);
   }
   float RentalCar:: getPrice()
   {
       return m_price;
   }
   void RentalCar:: setPrice(int price)
   {
       m_price = price;
   }

   bool RentalCar:: getAvailable()
   {
       return m_available;
   }
   void RentalCar::setAvailable(bool available)
   {
       m_available = available;
   }
   void RentalCar::print()
   {
       cout << m_year << " " << m_make << " " << m_model << ", $" << m_price << ", Available: " << boolalpha << m_available << endl; 
   }
   float RentalCar::estimateCost(int days)
   {
       return m_price * days;
   }

I was using the strcpy() function earlier just to test but it didnt work either.

Headerfile:

#ifndef RENTALCAR_H_
#define RENTALCAR_H_
class RentalCar
{
public:
   RentalCar(const int year, char * make, char * model, const float price, bool available);

   int getYear();
   void setYear(int year);

   char* getMake();
   void setMake(char make[]);

   char* getModel();
   void setMode(char model[]);

   float getPrice();
   void setPrice(int price);

   bool getAvailable();
   void setAvailable(bool available);

   void print();

   float estimateCost(int days);

protected:

private:
    int m_year;
    char * m_make;
    char * m_model;
    float m_price;
    bool m_available;

};
#endif
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.388704 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO