Thread: what is wrong with this program?

  1. #1
    learner
    Guest

    Question what is wrong with this program?

    #include<iostream>

    class student
    {

    private:
    char name[100];
    char address[100];
    char city[100];
    char state[2];
    char zip[5];
    public:
    student(){};
    student( char name,char address,char city,char state,char zip);
    void getdata()
    {
    cout<<"Enter name:"; cin>>name;
    cout<<"Enter address:"; cin>>address;
    cout<<"Enter city:"; cin>>city;
    cout<<"Enter state:"; cin>>state;
    cout<<:Enter zip:"; cin>>zip;

    }
    void showdata()
    {
    cout<<"NAME IS:"<<name<<endl;
    cout<<"ADDRESS IS:"<<address<<endl;
    cout<<"CITY IS:" <<city<<endl;
    cout<<"STATE IS :"<<state<<endl;
    cout<<"ZIP IS :"<<zip<<endl;
    }
    };

    int main()
    {
    {
    student();

    student .getdata();
    student .showdata();
    }


    return 0;
    };

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    for starters, you have an extra pair of brackets in int main()

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    317
    This works, if you don't know why just ask:
    Code:
    #include<iostream> 
    using namespace std;//otherwise need to use std::cout
    class student 
    { 
    
     private: 
       char name[100]; 
       char address[100]; 
       char city[100]; 
       char state[2]; 
       char zip[5]; 
    public: 
      student(){}; 
      student( char name,char address,char city,char state,char zip); 
    void getdata() 
    { 
       cout<<"Enter name:"; cin>>name; 
       cout<<"Enter address:"; cin>>address; 
       cout<<"Enter city:"; cin>>city; 
       cout<<"Enter state:"; cin>>state; 
       cout<<"Enter zip:"; cin>>zip; //you had a typo
    
    } 
    void showdata() 
    { 
        cout<<"NAME IS:"<<name<<endl; 
        cout<<"ADDRESS IS:"<<address<<endl; 
        cout<<"CITY IS:" <<city<<endl; 
        cout<<"STATE IS :"<<state<<endl; 
        cout<<"ZIP IS :"<<zip<<endl; 
    } 
    }; 
    
    int main() 
    { 
    	student Student; //you need to declare an instance and then use that
    
    	Student.getdata(); 
    	Student.showdata();  
    
    
    return 0; 
    }
    Last edited by Traveller; 05-09-2002 at 08:06 PM.

  4. #4
    learner
    Guest

    Question undeclared identifier

    Thank you very much. but I get 2 errors of undeclared identifiers for getdata and showdata in the main!
    {};
    student( char name,char address,char city,char state,char zip);
    void getdata()
    {
    cout<<"Enter name:"; cin>>name;
    cout<<"Enter address:"; cin>>address;
    cout<<"Enter city:"; cin>>city;
    cout<<"Enter state:"; cin>>state;
    cout<<:Enter zip:"; cin>>zip;

    }
    void showdata()
    {
    cout<<"NAME IS:"<<name<<endl;
    cout<<"ADDRESS IS:"<<address<<endl;
    cout<<"CITY IS:" <<city<<endl;
    cout<<"STATE IS :"<<state<<endl;
    cout<<"ZIP IS :"<<zip<<endl;
    -------------------Configuration: stu - Win32 Debug--------------------
    Compiling...
    stu.cpp
    C:\My Documents\stu.cpp(38) : error C2065: 'getdata' : undeclared identifier
    C:\My Documents\stu.cpp(39) : error C2065: 'showdata' : undeclared identifier
    Error executing cl.exe.

    stu.obj - 2 error(s), 0 warning(s)

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    317
    Are you sure you have everything exactly as shown above because I compiled it just fine.

  6. #6
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Do the functions come before int main() in your code? If not you need to prototype them above int main().

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    156
    No, thank you
    Compiler: MingW(IDE: Bloodshed Dev-C++ 4.01)
    Web Site: Zoo Crew
    Forums: Zoo Boards
    E-mail: [email protected]

    "Do you wanna go to jail or do you wanna go home?!?!" - Alonzo(Training Day)

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    317
    They are defined in the class definition which he did not show on the last post.

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    156
    I agree with Travelle(whatever), I am the best.
    Compiler: MingW(IDE: Bloodshed Dev-C++ 4.01)
    Web Site: Zoo Crew
    Forums: Zoo Boards
    E-mail: [email protected]

    "Do you wanna go to jail or do you wanna go home?!?!" - Alonzo(Training Day)

  10. #10
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Originally posted by Golden Bunny
    I agree with Travelle(whatever), I am the best.
    What are you talking about?! Traveller was simply filling me in on learner's last post. There was no disagreement between Traveller and I. I was asking learner if he had prototyped the functions since the code he posted was not complete.

    You are not the best? You can't pay attention well enough to properly spell Traveller's name.

  11. #11
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    Student.getdata();
    Student.showdata();

    When declaring the class you use a small 's' but when calling the functions you use a big 'S'.
    Maybe your compiler allows that and his doesnt...

    EDIT:: maybe i should actually read the posts aswell =D
    Last edited by ErionD; 05-10-2002 at 08:17 AM.

  12. #12
    Registered User GreenCherry's Avatar
    Join Date
    Mar 2002
    Posts
    65
    It works fine but whats the purpose? Why are you using arrays to input one item for each set? also, the zipcode array should be an integer.

  13. #13
    Registered User GreenCherry's Avatar
    Join Date
    Mar 2002
    Posts
    65
    Code:
    void getdata() 
    { 
       cout<<"Enter name:"; cin>>name[0]; 
       cout<<"Enter address:"; cin>>address[0]; 
       cout<<"Enter city:"; cin>>city[0]; 
       cout<<"Enter state:"; cin>>state[0]; 
       cout<<"Enter zip:"; cin>>zip[0];
    } 
    
    void showdata() 
    { 
        cout<<"NAME IS:"<<name[0]<<endl; 
        cout<<"ADDRESS IS:"<<address[0]<<endl; 
        cout<<"CITY IS:" <<city[0]<<endl; 
        cout<<"STATE IS :"<<state[0]<<endl; 
        cout<<"ZIP IS :"<<zip[0]<<endl; 
    }
    Those would be a bit more correct, ut the purpose of this program is still mysterious.

  14. #14
    Unregistered
    Guest
    the non-default constructor isn't used, which is good, because it should be changed to havie char arrays as arguments, not single characters.

    Green Cherry's last post using single char as variables doesn't make much sense based on what type of data the program is intended to use.

    Storing zip as a string is just as valid as storing it as a numerical value such as an int or a long.

  15. #15
    learner
    Guest

    Angry thnks everyone but it still not working

    Thank you evryone !
    I still get the same 2 error messages! greencherry can you please post the program the way you coded?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Maze Program - What am I doing wrong?
    By Gipionocheiyort in forum C++ Programming
    Replies: 20
    Last Post: 08-02-2007, 01:31 PM
  2. Replies: 5
    Last Post: 01-13-2007, 02:14 AM
  3. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM
  4. Whats wrong with this program - Output of a series
    By duffmckagan in forum C Programming
    Replies: 2
    Last Post: 07-26-2006, 09:57 AM
  5. Whats wrong with my program?
    By Ruflano in forum C++ Programming
    Replies: 5
    Last Post: 02-21-2002, 05:09 PM