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

    Exclamation Please help asap!

    Hi folks,
    I need help, I'm a beginner. I have an assignment due in 5 hours and I don't even know how to start, I'm stuck! I would appreciate if you could give me suggestion how to do this:

    A user enters 15 different words. The program creates an array of 15 objects and then display the contend of the array, using c++ class.
    Last edited by Scubo; April 8th, 2009 at 03:00 AM. Reason: confusing

  2. #2
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Please help asap!

    Sure, start by reading this.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  3. #3
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: Please help asap!

    would you please try to do something yourself first!
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  4. #4
    Join Date
    Apr 2009
    Posts
    4

    Unhappy Re: Please help asap!

    I would if I knew how, I don't ask for the whole solution, I just wanted to hear your suggestions as I don't know how to solve this, I simply wanted a hint, but I see nobody is willing to help. I do not expect to get free solutions from this forum. Thank you for your understanding.

  5. #5
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: Please help asap!

    Ok, here is a general program skeleton:

    Code:
    int main()
    {
    
     return 0;
    };
    Since you want to print out the result, you will need to include iostream and since your words are to be held in objects, you will want to include string since string is the obvious container for holding words. You also want an array of 15 objects, so that could be an array of 15 strings:

    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
      std::string words[15];
    
      //rest of the code here
    
      return 0;
    };
    The rest of your code should happen within the main function.

    You might want to let the user know that they need to enter 15 words. e.g.

    Code:
      std::cout << "Please enter 15 words (pressing return after each word)" << std::endl;
    You will then need to create a loop that runs 15 times and fills out your string array. If your loop has a counter i, then you can input data into each word element using:
    Code:
      std::cin >> word[i];
    You will then need to make another loop that runs 15 times to print the result. You can do this with std::cout.

    If your code is required to be encapsulated into a class, then you will need to figure out how you want to do that. I've now given you more help than I possibly should, but given your deadline is very soon, you might need it. In future, don't leave things to the last minute!

    Put the bits together that I have suggested, and if you get stuck after that, then let us know, and we might help you.
    Last edited by PredicateNormative; April 8th, 2009 at 08:08 AM. Reason: Removed some code - gave too much help.

  6. #6
    Join Date
    Apr 2009
    Posts
    6

    Re: Please help!

    Can somebody tell me what is wrong with my code, I'm not getting the right output:

    #include <iostream>
    using namespace std;
    #include <string>

    int main(){

    char s[5];

    cout<<"Enter 5 words followed each by <enter>"<<endl;

    for(int i=0; i<5; i++)
    {
    cin.getline(s[i],20);
    }

    for(int j=0; j<5; j++)
    {

    cout << s[j]<<endl;
    }


    return 0;
    }


    error: cannot convert parameter 1 from 'char' to 'char *'

  7. #7
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Please help!

    Quote Originally Posted by leonida View Post
    Can somebody tell me what is wrong with my code, I'm not getting the right output:

    #include <iostream>
    using namespace std;
    #include <string>

    int main(){

    char s[5];

    cout<<"Enter 5 words followed each by <enter>"<<endl;

    for(int i=0; i<5; i++)
    {
    cin.getline(s[i],20);
    }

    for(int j=0; j<5; j++)
    {

    cout << s[j]<<endl;
    }


    return 0;
    }


    error: cannot convert parameter 1 from 'char' to 'char *'
    Please use code tags.

    Code:
    char s[5];
    I thought you were required to store 15 words?

    Code:
    cin.getline(s[i],20);
    This will limit the size of the word to 20 characters (including the null-terminator). If it doesn't specify that the maximum size for each word is 20 characters, then you might want to change this.

    I think you should take notice of PredicateNormative's advice and make an array of strings as it sounds like it matches the description of the assignment.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  8. #8
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: Please help!

    Quote Originally Posted by leonida View Post
    Can somebody tell me what is wrong with my code, I'm not getting the right output:

    #include <iostream>
    using namespace std;
    #include <string>

    int main(){

    char s[5];

    cout<<"Enter 5 words followed each by <enter>"<<endl;

    for(int i=0; i<5; i++)
    {
    cin.getline(s[i],20);
    }

    for(int j=0; j<5; j++)
    {

    cout << s[j]<<endl;
    }


    return 0;
    }


    error: cannot convert parameter 1 from 'char' to 'char *'
    Yes, a minor error is that your using declaration should be placed after your string include... not that you are using string at the moment anyway. But the real problem is as follows:
    Code:
    char s[5];
    is a char array that holds five characters, you are then passing the ith element (which is of type char) to cin.getline which is expecting a char array (type char*). Even if you changed your declaration of char s[5] to char* s[5] it would still be wrong, because you will have only allocated 5 char pointers. You have two options here, you either declare s as follows:

    Code:
    char s[5][20];
    The above declares an array of 5 char arrays where each char array has a maximum of 20 characters (including the null terminator), or you could change your code to use string and a different form of getline (a better option):

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(){
    
      string s[5];
    
      cout<<"Enter 5 words followed each by <enter>"<<endl;
    
      for(int i=0; i<5; i++)
      {
        getline(cin,s[i]);
      }
    
      for(int j=0; j<5; j++)
      {
    
        cout << s[j]<<endl;
      }
    
      system("PAUSE");
      return 0;
    }

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