Beginner's Assignment

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • itoyrei
    New Member
    • Oct 2007
    • 3

    Beginner's Assignment

    can anyone help me in making char array?

    for example if i declared:

    char names[20];

    and for example i want to enter 3 names (example joe, sarah, and arthur)
    for that array and then display those names...how will i write it as a syntax or any declarations that i missed?...sorry for being a newbie,but answering my question will be a great help for my school assignment..

    im expecting for the output to look like this:

    joe
    sarah
    arthur

    thank you.
  • scruggsy
    New Member
    • Mar 2007
    • 147

    #2
    Originally posted by itoyrei
    can anyone help me in making char array?

    for example if i declared:

    char names[20];

    and for example i want to enter 3 names (example joe, sarah, and arthur)
    for that array and then display those names...how will i write it as a syntax or any declarations that i missed?...sorry for being a newbie,but answering my question will be a great help for my school assignment..

    im expecting for the output to look like this:

    joe
    sarah
    arthur

    thank you.
    char names[20] declares an array which can hold 20 characters.
    So, if you want to store 3 names, you will need 3 char arrays, one to hold each name.
    You could make 3 separate arrays like:
    Code:
    char name1[20];
    char name2[20];
    char name3[20];
    ...but I suspect you'd rather create an array of 3 names, each of which would be stored in an array of 20 chars.
    In other words, an array of 3 arrays of 20 characters.
    Hope this helps. Check your textbook or google if you're not sure how to declare that array.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      The array you have defined is an array of 20 characters, not 20 names.

      What do you understand a name to be?? If you can define that, then you should be able to define an array of those things.

      Comment

      • itoyrei
        New Member
        • Oct 2007
        • 3

        #4
        well,i guess i have no choice but to manually assign them one by one...
        but,is there also another way of writing the code like this...

        Code:
        int main()
        {
             int num[3],i;
             cout<<"enter 3 numbers:\n";
        
             for (i=0;i<3;i++)
             {
                  cout<<"num["<<i<<"]=";
                  cin>>num[i];
             }
        
             cout<<"the numbers you entered are:\n";
             
             for (i=0;i<3;i++)
              {
                  cout<<"num["<<i<<"]=";
                  cout<<num[i];
             }
        
             system("PAUSE");
             return ;
        }
        ...i mean like the method used in assigning the the numbers that the user have
        entered to the different arrays...

        thanks scruggsy and weaknessforcats for the early reply...

        Comment

        • Laharl
          Recognized Expert Contributor
          • Sep 2007
          • 849

          #5
          You need to return a value at the end of your main, your compiler may add in 0 for you, but it's good to get in the habit of adding it yourself in case you need to return something else because of an error.

          [CODE=cpp]
          return 0;
          [/CODE]

          Your logic works for integers, but you need to be aware that a char[] stores one character at each index, not a string of them. So, "hello" would be stored in the array as 'h' 'e' 'l' 'l' 'o'. Thus, you have the choice of multiple arrays, or a 2D array (matrix).

          Comment

          • oler1s
            Recognized Expert Contributor
            • Aug 2007
            • 671

            #6
            You should look into C++ strings. If you are going to use C++, you might as well take advantage of what it provides.

            With simple character arrays, you can pass in a char array as a parameter for cin and cout.

            [CODE=cpp]
            char name[SOME_SIZE];
            std::cin >> name;
            [/CODE]

            Comment

            • itoyrei
              New Member
              • Oct 2007
              • 3

              #7
              sorry,my bad Laharl for forgetting to write "return 0;"

              well,I better get my self to learn about strings.

              thanks Laharl and oler1s as well as to those who read and and gave their reply for your time reading and giving some suggestions to my post.thanks guys...

              Comment

              Working...