CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Apr 2010
    Posts
    26

    Halp with my program?

    My teacher asked me to simply create a scenario in C++ that uses an array of objects.
    Well, i followed everything by book yet weird problem appears. What is wrong? My take is that the dynamic addressing part has problem. What is the problem? Thanks in advance.
    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    class kill
    {
    
    string name;
    public:
    void inName(string name);
    string outName();
    };
    
    void kill::inName(string name)
    {
        this->name=name;
    }
    
    string kill::outName()
    {
        return name;
    }
    
    int main()
    {
    int size;
    kill* peopleyouhate;
    cout<<"Insert the amount of people you wanna terminate"<<endl;
    cin>>size;
    peopleyouhate= new kill[size];
    for(int i=0; i<size; i++)
    {
    string name;
    cout<<"Insert the name of the person you wanna terminate"<<endl;
    cin>>name;
    peopleyouhate[i].inName(name);
    }
    
    cout<<"These are the people you wanna terminate, there is no more turning back..."<<endl;
    for(int j=0; j<size; j++)
    {
    cout<<peopleyouhate[j].outName<<endl;
    }
    
    return 0;
    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Halp with my program?

    cout << peopleyouhate[j].outName() << endl;

    Get in the habit of using white space. It'll make your program more readable.

  3. #3
    Join Date
    Apr 2010
    Posts
    26

    Re: Halp with my program?

    WOW! THAT'S IT? Wow....just wow!!! I am seriously sorry for wasting your time. By the way, is my way of creating a dynamic memory correct because when it comes to pointer and all i am not very confident. Also, i modified the program so that it can take in the name "John Connor" without error by putting in a getline code but there seems to be a looping problem which i do not understand. Mind checking it out? Thank you for your time spent on answering my menial question. I understand some of you guys are programming gurus and have better and more important things to do. I am truly sorry if i wasted any of your time.

    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    class kill
    {
    
    string name;
    public:
    void inName(string name);
    string outName();
    };
    
    void kill::inName(string name)
    {
        this->name=name;
    }
    
    string kill::outName()
    {
        return name;
    }
    
    int main()
    {
        int size;
        kill* peopleyouhate;
        cout<< "Insert the amount of people you wanna terminate "<<endl;
        cin>>size;
        peopleyouhate= new kill[size];
    for(int i=0; i<size; i++)
    {
        string name;
        cout<<"Insert the name of the person you wanna terminate"<<endl;
        getline(cin,name);
        peopleyouhate[i].inName(name);
    }
    
    cout<<"These are the people you wanna terminate, there is no more turning back..."<<endl;
    
    for(int j=0; j<size; j++)
    {
        cout<< peopleyouhate[j].outName() <<endl;
    }
    
    return 0;
    }

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Halp with my program?

    Wasn't as waste of time. You put the effort in and just made a trivial mistake. Believe it or not, we all do something similar from time to time.

    Your allocation is fine, just don't forget for every new, there should be a delete.

  5. #5
    Join Date
    Apr 2010
    Posts
    26

    Re: Halp with my program?

    Thanks but is still have problem with the getline function. The getline function did not seem to prompt me to insert a value....

  6. #6
    Join Date
    Aug 2007
    Posts
    858

    Re: Halp with my program?

    Quote Originally Posted by hayloiuy View Post
    Thanks but is still have problem with the getline function. The getline function did not seem to prompt me to insert a value....
    All getline does is read input from the console. If you want to give the user any kind of prompt you have to do it with cout.

  7. #7
    Join Date
    Apr 2010
    Posts
    26

    Re: Halp with my program?

    Woops sorry, i seemed to have caused some misunderstanding. What i meant was that the getline function causes the first loop not to accept any value. Try compiling it and you will understand.

  8. #8
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: Halp with my program?

    This is a very commom error for beginners.
    The reason is that you're mixing input methods.

    Code:
        cin>>size;
    The stream extractors stop reading when they find a whitespace -> leaving the final '\n' in the buffer

    Code:
        string name;
        cout<<"Insert the name of the person you wanna terminate"<<endl;
        getline(cin,name);
    getline stops reading when it finds the terminating character ( by default '\n' ) that's why an empty string is returned.
    solution: put
    Code:
        cin.ignore();
    after the use of the stream extractor.
    Kurt

  9. #9
    Join Date
    Apr 2010
    Posts
    26

    Re: Halp with my program?

    Wow !That's something new! Ok it works but if that is your explanation then this should not work right?
    cin>>a;
    cin>>b;
    Because the second cin will execute the /n from the first cin?

  10. #10
    Join Date
    Apr 2010
    Posts
    26

    Re: Halp with my program?

    Oh yeah, thanks by the way.

Tags for this Thread

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

Featured