Halp with my program?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Halp with my program?

Hybrid View

  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
    Posts
    12,057

    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
    Posts
    12,057

    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?

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center