Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

2-d char C strings...?

I've been using C++ <string> so long that I forgot how to handle C string. I want to store the names of 5 students in a 2-d array. I think that I need something like char name[5][60]; if the names have less than 60 chars but it's not working. Please show me how to declare the array and initialize the first name, ie, what's in the student[0] ...position. Thanks.

Update:

It's actually something I'm helping a newbie with! I want to only use cin/cout. I want to prompt for the names of 5 students. The assignment specifys a 2-d array to hold the names of 5 students. So, somehow I need a 2 d char array. I want to get each student's name with cin.

I almost never use C-strings once I learned about <string> but ...

Thanks for the help, hopefully you can help some more?

4 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    You can declare it like this:

    char name[5][60] ;

    What you did is correct. To initialize the value, you can do this:

    char name[5][60] = {"Tom", "Dick" , "and", "harry", "bla bla"};

    What you need to do for the input is not cin. For C its different. Here let me show you.

    #include<stdio.h>

    #include<string.h>

    void main()

    {

    int x;

    char name[5][60];

    for(x=0;x<=4;x++)

    {

    printf("\nEnter name of student %d", x+1);

    gets(name[x]);

    }

    for(x=0;x<=4;x++)

    {

    printf("\nName of student %d : %s", x + 1, name[x]);

    }

    getche();

    }

    You cant declare the int used for the For loop this way -> for(int i=0;i<5;i++).

    That method doesn't work in C programming.

    Instead of using scanf for saving the value into the variable, i use gets. Simply because scanf doesn't take in special characters like space. So if you enter Sangkaran Vilu. When you display, you will only see Sangkaran. If you use gets, then you will see Sangkaran Vilu.

    C programming is very interesting. I like both C and C++. Feel free to drop me an email if you need further assistance.

    Good luck, hope this helps.

  • 1 decade ago

    I like most of S.V.'s answer, but definitely don't use gets()...particularly with newbie.

    The C++ function to use is cin.getline(), as in:

    #include <iostream>

    using namespace std;

    int main(void)

    {

    char names[5][60];

    for (int i=0; i<5; ++i)

    {

    cout << "Enter name for student " << (i+1) << ": ";

    cin.getline(names[i], sizeof names[i]);

    }

    for (int i=0; i<5; ++i)

    {

    cout << "Student " << (i+1) << " is " << names[i] << endl;

    }

    }

    Notice the use of sizeof to specify the buffer size. This will not overrun the allocated area of the array. Don't worry about the lack of parens on sizeof...it's an operator, not a function. You only need the parens when there are precedence issues, or when the argument is a type, not a variable. (The argument isn't evaluated. The expression is only used to determine the compile-time size.) I think this is more self-documenting than sizeof(*names), which also works ... and brings us to:

    If by student[0], you meant names[0], that's a reference to the first row of the names[][] matrix. Used in an expression, an array reference is always treated as a pointer to to the first element of the array. It's important to note that the type of names[0] is not char, but char[60]. That's why sizeof names[0] will return a 60 and not a 1.

    Have fun!

    Edit: Oh, I didn't notice the details before. It looks like I wrote the assignment. I usually don't and didn't mean to this time. Be sure that your "tutee" gets the idea of using cin.getline() for C strings that can include spaces. When (s)he gets to C++ strings, emphasize that this is different from the standalone getline() function in <string>.

    Also, point out that cin >> setw(sizeof buffer) >> buffer is *the* way to read C string tokens. It's never too early to teach safe extraction. :^) (Note: setw() requires <iomanip> to be included.)

  • 1 decade ago

    It is hard to go back to char arrays after you've used <string>

    I assume you have the student name in a temporary buffer already? You just want to move it into name?

    Something like this?

    char buf[60];

    scanf("%s",buf);

    To get buf into the 2-D array, you would do:

    strcpy(name[i],buf);

    (I'm ignoring all error checking here, of course).

    The important thing is to make sure that the 'string' you copy into the array is NUL terminated (which strcpy will do for you). If you don't have that, you will get weird results.

    If that doesn't answer it, maybe post a snipped of code?

  • kanosh
    Lv 4
    5 years ago

        if (argc != 3) {         printf("Please supply 2 integer argumentsn");         return 0;     }     int a = atoi(argv[a million]);     int b = atoi(argv[2]);     if (a == 0) {         if (strcmp(argv[a million], "0")) {             printf("Please supply 2 integer argumentsn");             return 0;         }     }     if (b == 0) {         if (strcmp(argv[2], "0")) {             printf("Please supply 2 integer argumentsn");             return 0;         }     }     int sum = a + b; notice that the assessment with 0 is critical by way of fact atoi returns 0 on failure. So there is no thank you to tell from the return fee regardless of if it replace into an blunders, or the consumer unquestionably entered the quantity 0. So if the fee is 0, we manually learn it with strcmp.

Still have questions? Get your answers by asking now.