Results 1 to 7 of 7

Thread: the newbie returneth

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    India
    Posts
    203

    the newbie returneth

    #include<iostream.h>
    #include<conio.h>

    const int MAX=4;
    class person
    {
    char name[10];
    public:
    void display()
    {
    cout<<name;
    }
    void accept()
    {
    cout<<"ENter Name";
    cin>>name;
    }
    };

    void main()
    {
    person *parr[MAX];

    for(int i=0;i<MAX;i++)
    {
    parr[i]=new person;
    parr[i]->accept();
    }

    for(int j=0;j<MAX;j++)
    {
    parr[i]->display();
    }

    getch();

    }


    1)..#include<Iostream> does not work..it doesnt recognise cin/cout/<</>> etc...I have 2 add the ".h" at the end.

    2) getch()..the function I think is supposed 2 b used to halt the program so tt the user can read the screen output before the Window vanishes...it apparently stops the program right before any output is done on screen...

    3)..the program here is giving me garbage output..cant figure out y.Probably the error would be painfully obvious the next time I look at the code..but still...

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    1. You need to have a using namespace std; in there.

    2. Before the getch, use cout << flush;

    Code:
    #include<iostream>
    #include <string>
    #include<conio.h> 
    
    using namespace std;
    
    const int MAX = 4; 
    class person { 
        string name; // Changed this so you're not limited on names
    public: 
        void display() { 
            cout << name << endl; 
        }
    
        void accept() { 
            cout << "Enter Name: "; 
            cin >> name; 
        } 
    }; 
    
    int main() { 
        person parr[MAX]; // You don't need a pointer since it's fixed
    
        for(int i = 0; i < MAX; ++i) {
            parr[i]->accept(); 
        } 
    
        for(int j = 0; j < MAX; ++j) { 
            parr[j]->display(); // Changed this to j from i
        }
    
        cout << flush; // endl actually does this anyway, but its good practice
    
        getch();
    
        return 0;
    }
    Hopefully the string change should fix 3.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    << and >> aren't overloaded for string. Use getline to input it and cout << name.c_str() for output.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    They are, they act in exactly the same way you'd expect (get up to the first whitespace character).

    getline gets up until a newline.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Strange, I usually get an error when doing
    string str("Hello!");
    cout << str << endl;
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You have to be using the templated iostreams (#include <iostream>) or it won't work.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I do. But I haven't tried in a while. I'll do it now.

    Ok, it works. Never mind.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width