Thread: data structure problem

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    10

    data structure problem

    Those some parts of my program. I write the problem at the buttom
    Code:
    void main()
    {
    	string option;
    
    	warehouse car[2];
    
    	car[0].inputCar("lamborgini",true);
    	car[0].showCar();
    	car[1].inputCar("ferrari",false);
    	car[1].showCar();
    
    		//display the choices of the user
    		cout<<"a) Show number of modified"<<endl;
    		cout<<"b) Show list of modified"<<endl;
    		
    		cin>> option;
    
    	switch (option[0])
    	{
    		case 'a':
    			countModified();
    			break;
    		case 'b':
    			listModified();
    			break;
    	}
    }//end main
    my function
    Code:
    void listModified()
    {
    	warehouse car[2];
    
    
    for (i=0;i<2;i++)
    	{
    		car[i].printModified();
    		//cout<<"COUT: "<<car[i].checkModified()<<endl;
    	}
    
    
    }
    i noticed that i must have warehouse car[2]; to every function i write elsewhere i get the following errors

    error C2065: 'car' : undeclared identifier
    and
    error C2228: left of '.printModified' must have class/struct/union

    the problem is that the values change.
    any way to fix this ?

    Thanks for your time!

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    You need to pass the car object into the function.
    So the function would look like:
    Code:
    void listModified(warehouse car)
    {
    //do stuff
    }
    You could then create your car object in main as you have and then do:
    listModified(car[2]), or whatever you call it.

    Read up on parameter passing, in particular the difference between pass by value and pass by reference.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    10
    Thanks for you time darren78.
    I tried to pass the parameters but again i got many more errors.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Show what changes you have made and the errors you are getting.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    I am going out now so won't be able to help any further this evening. Have a read up on Cprogramming.com Tutorial: Functions and also do a google search for functions and passing parameters. There is a plethora of information out there.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    10

    data structure problem

    Thats my changed sourcecode

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    using namespace std;
    
    int i=0;
    int total=0;
    int check=1;
    
    warehouse car[2];
    
    class warehouse
    {
    public:
        warehouse()
        {
        PRVmodified=true;
        }
    
        void printModified();
        void checkModified();
        void inputCar(char car[20],bool modified);
        void showCar();
    private:
        char PRVcar[20];
        bool PRVmodified;
    
    };
    void warehouse::inputCar(char car[20],bool modified)
    {
        strcpy(PRVcar,car);
        PRVmodified=modified;
    }
    
    void warehouse::showCar()
    {
        cout<<"car model is: "<<PRVcar<<endl;
        cout<<"car is modified: "<<PRVmodified<<endl<<endl;
    }
    void warehouse::checkModified()
    {
    if (PRVmodified == 1)
    {
                ++total;
                
    }
    
    }
    void countModified(warehouse car)
    {
        //warehouse car[2];
        
        for (i=0;i<2;i++)
        {
            car[i].checkModified();
        }
        cout<<"The total cars modified: "<<total<<endl;
    }
    
    void warehouse::printModified()
    {
    if (PRVmodified == 1)
    {
        cout<<"the car that is modified: "<<endl;
                
    }
    }
    void listModified(warehouse car)
    {
    for (i=0;i<2;i++)
        car[i].printModified();
    
    }
    void main()
    {
        string option;
    
        car[0].inputCar("lamborgini",true);
        car[0].showCar();
        car[1].inputCar("ferrari",false);
        car[1].showCar();
    
        //display the choices of the user
        cout<<"a) Show number of modified"<<endl;
        cout<<"b) Show list of modified"<<endl;
        
        cin>> option;
    
        switch (option[0])
        {
            case 'a':
                countModified(car[2]);
                break;
            case 'b':
                listModified(car[2]);
                break;
        }
    }//end main
    i got many errors like
    error C2228: left of '.showCar' must have class/struct/union
    error C2064: term does not evaluate to a function taking 1 arguments

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Okay, so you are using class methods, it's slightly different and there are a few things wrong here.

    I guess my using car[2] you are wanting to create and store multiple car objects. I would suggest just creating them singular and then storing them in an vector<warehouse>

    You can get away without having the input car method aswell and simply have a constructor that initialises the 2 data members when you create a new object.
    The constructor would be along the lines of:
    Code:
     warehouse(string car, bool modified)
        :PRVcar(car), PRVmodified(modified) {}
    When you create a new object you would then do as such:

    warehouse car("ford", true);

    For each object created you can then call showcar method as such:

    car.showcar();

    You don't need to pass an argument in this case, each single object will have a car type and a bool value of whether it's modified.

    Notice that I have also changed your char[20] to a string class. Strings should be used in the majority of cases rather than char arrays.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The problems in your code are as follows:
    - You try to create warehouses before that type is defined. At this point, the compiler doesn't know about it and thus complains.
    - The usage of global variables. Global variables are bad. Recommended you move the warehouses into main.
    - void main. Void main is non-standard; use int main.
    - car[2] doesn't exist! You have two cars in [0, 1]. The index 2 doesn't exist.

    Remember that if you want to use your car in other source files, then they will need to be able to see the definition of the warehouse. Suggest you read up on creating classes and using multiple source files.
    Also read up on references and pass by value, since there may be pitfalls here depending on what you want to do.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    10

    fixed!

    It works with global variable, but i wanted to make it work with passigf the arguements..
    I fix it and work with arguements now.
    Just have to change

    countModified(car);
    and
    void listModified(warehouse ArrayOfStruct[]) ;

    Elysia: I tried to change it to
    warehouse car[2];

    but i get runtime error
    Stack around the variable 'car' was corrupted....

    Thanks for the help!
    Last edited by fukki; 10-03-2010 at 05:03 AM.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by fukki View Post
    Elysia: I tried to change it to
    warehouse car[2];

    but i get runtime error
    Stack around the variable 'car' was corrupted....

    Thanks for the help!
    It means you tried to use a car which didn't exist, eg: car[2] in your case.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Passing Structure Reference To Function
    By soj0mq3 in forum C Programming
    Replies: 9
    Last Post: 04-24-2010, 10:27 AM
  2. problem with data in allocated memory
    By supi in forum C Programming
    Replies: 3
    Last Post: 06-09-2008, 02:06 AM
  3. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  4. can't insert data into my B-Tree class structure
    By daluu in forum C++ Programming
    Replies: 0
    Last Post: 12-05-2002, 06:03 PM
  5. Dynamic Data Structure -- Which one is better?
    By Yin in forum C++ Programming
    Replies: 0
    Last Post: 04-10-2002, 11:38 PM