strcat(), searching for a name
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: strcat(), searching for a name

  1. #1
    Join Date
    Mar 2011
    Posts
    153

    strcat(), searching for a name

    Hi

    Please help me to understand the working of the code below. CODE 1 is working fine. There is an issue with CODE 2 which is just a variation of CODE 1.

    1: How does strcat() works?
    [code]char temp_str[30] = {0};[\code]

    temp_str[] contains 29 zeroes and 30th element would be '\0'. Correct?

    Where does strcat() start appending to the destination string from?

    2: The variations in CODE 2 are in red. I get the following warning and the code doesn't produce correct result. Also have a look on the OUTPUT below.

    warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x|

    What's wrong?

    Please help me with the above queries.

    CODE 1
    Code:
    // read students' record and search for data of a particular student.cpp
    // use structure and display all data about the searched student
    
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    
    using namespace std;
    
    const int C = 2;
    
    ///////////////////////////////////////////////////////////////////////////////
    struct Student {int rollno; char firstname[20]; char lastname[20]; string sex; float gpa;};
    //////////////////////////////////////////////////////////////////////////////
    
    Student stud[C];
    
    int main()
    {
            int i; char searchname[30];
    
            for (i=0; i<C; i++)
            {
                    cout << "\n\nEnter student #" << (i+1) << "\'s details below\n\n";
                    cout << "enter roll no.: "; cin >> stud[i].rollno;
                    cin.ignore();
                    cout << "enter first name: "; cin.get(stud[i].firstname, 20);
                    cin.ignore();
                    cout << "enter last name: "; cin.get(stud[i].lastname, 20);
                    cout << "enter sex: "; cin >> stud[i].sex;
                    cout << "enter GPA: "; cin >> stud[i].gpa;
            }
    
            cout << endl;
    
            cin.ignore();
    
            cout << "enter the name to be searched for: "; cin.get(searchname, 30);
            // name to be searched for could be, say, Jack Dawson
    
    
            for (i=0; i<C; i++)
            {
                    char temp_str[30] = {0};
                    strcat(temp_str, stud[i].firstname);
                    strcat(temp_str, " ");
                    strcat(temp_str, stud[i].lastname);
    
    
                    if ( strcmp(temp_str, searchname ) == 0 )
                    {
                            cout << "Data for the searched student is shown below\n";
                            cout << "roll no.: " << stud[i].rollno << endl;
                            cout << "first name: " << stud[i].firstname << endl;
                            cout << "last name: " << stud[i].lastname << endl;
                            cout << "sex: " << stud[i].sex << endl;
                            cout << "GPA: " << stud[i].gpa << endl;
                            break;
                     }
    
                     else if ( i == (C-1) )
                     {
                             cout << "Sorry, no student exists with such name" << endl;
                             break;
                     }
            }
    
            system("pause");
            return 0;
    }
    CODE 2
    Code:
    // read students' record and search for data of a particular student.cpp
    // use structure and display all data about the searched student
    
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    
    using namespace std;
    
    const int C = 2;
    
    ///////////////////////////////////////////////////////////////////////////////
    struct Student {int rollno; char firstname[20]; char lastname[20]; string sex; float gpa;};
    //////////////////////////////////////////////////////////////////////////////
    
    Student stud[C];
    
    int main()
    {
            int i; char searchname[30]; char temp_str[30] = {0};
    
            for (i=0; i<C; i++)
            {
                    cout << "\n\nEnter student #" << (i+1) << "\'s details below\n\n";
                    cout << "enter roll no.: "; cin >> stud[i].rollno;
                    cin.ignore();
                    cout << "enter first name: "; cin.get(stud[i].firstname, 20);
                    cin.ignore();
                    cout << "enter last name: "; cin.get(stud[i].lastname, 20);
                    cout << "enter sex: "; cin >> stud[i].sex;
                    cout << "enter GPA: "; cin >> stud[i].gpa;
            }
    
            cout << endl;
    
            cin.ignore();
    
            cout << "enter the name to be searched for: "; cin.get(searchname, 30);
            // name to be searched for could be, say, Jack Dawson
    
    
            for (i=0; i<C; i++)
            {
                    temp_str[30] = {0};
                    strcat(temp_str, stud[i].firstname);
                    strcat(temp_str, " ");
                    strcat(temp_str, stud[i].lastname);
    
    
                    if ( strcmp(temp_str, searchname ) == 0 )
                    {
                            cout << "Data for the searched student is shown below\n";
                            cout << "roll no.: " << stud[i].rollno << endl;
                            cout << "first name: " << stud[i].firstname << endl;
                            cout << "last name: " << stud[i].lastname << endl;
                            cout << "sex: " << stud[i].sex << endl;
                            cout << "GPA: " << stud[i].gpa << endl;
                            break;
                     }
    
                     else if ( i == (C-1) )
                     {
                             cout << "Sorry, no student exists with such name" << endl;
                             break;
                     }
            }
    
            system("pause");
            return 0;
    }
    OUTPUT for CODE 2
    Code:
    
    Enter student #1's details below
    
    enter roll no.: 1
    enter first name: jackson
    enter last name: heights
    enter sex: male
    enter GPA: 6
    
    
    Enter student #2's details below
    
    enter roll no.: 2
    enter first name: jack
    enter last name: dawson
    enter sex: male
    enter GPA: 2
    
    enter the name to be searched for: jack dawson
    Sorry, no student exists with such name
    Press any key to continue . . .

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,639

    Re: strcat(), searching for a name

    Code # 2

    Code:
    temp_str[30] = {0};
    I'm not a language expert, but I don't think that is an initializer
    list. I would have thought that initializer lists could only by used
    when the variable is declared. That would be some type of assigment.

    Look at the value of each element in the string after that statement.
    Something like (just typed in ... might have typos):

    Code:
    for (int i=0; i<30; ++i)
    {
       cout << i << " : " << static_cast<int>(temp_str[i]) << "\n";
    }
    Also, write out the addresss of the first element:

    Code:
    cout << &temp_str[0] << "\n";

  3. #3
    Join Date
    Mar 2011
    Posts
    153

    Re: strcat(), searching for a name

    Quote Originally Posted by Philip Nicoletti View Post
    Code # 2

    Code:
    temp_str[30] = {0};
    I'm not a language expert, but I don't think that is an initializer
    list. I would have thought that initializer lists could only by used
    when the variable is declared. That would be some type of assigment.
    Hi Philip

    I just checked the following code and it works fine.

    Code:
    // learning initialization.cpp
    
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
            int v = 10;
    
            cout << v << endl;
    
            v = 20;
    
            cout << v << endl;
    
            system("pause");
            return 0;
    }

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,639

    Re: strcat(), searching for a name

    Well, yes. The first statement initializes the int "v", the
    second changes its value using the assignment operator.
    That is how "int" variables work.

    What does that have to do with using

    Code:
    temp_str[30] = {0};
    What is that suppose to do ?

    What was the result of adding the cout statements from
    my previous code ?

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

    Re: strcat(), searching for a name

    Not sure what you're trying to do, but temp_str[30] isn't a valid location. You can't safely do anything to it.

  6. #6
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,049

    Re: strcat(), searching for a name

    Code:
    char str[30] = {0};
    The code means, the first character is initialized to the value from initialization list which is zero in the case, and all the other array elements are initialized to zeros. This way all the elements get zeroed.

    Besides, there's no difference between 0 and '\0' while we're talking about char which merely is a signed byte.
    Best regards,
    Igor

  7. #7
    Join Date
    Mar 2011
    Posts
    153

    Re: strcat(), searching for a name

    Thank you everyone for all the help.

    Quote Originally Posted by Igor Vartanov View Post
    Code:
    char str[30] = {0};
    The code means, the first character is initialized to the value from initialization list which is zero in the case, and all the other array elements are initialized to zeros. This way all the elements get zeroed.

    Besides, there's no difference between 0 and '\0' while we're talking about char which merely is a signed byte.
    Thank you, Igor.

    That would mean the beginning of the string is in fact the ending of the string because as you say there is no difference between '\0' and '0'.

    I infer that any elements of the array that you don't specify a value for are initialized to 0, as long as at least one element is initialized.

    But what about structure? Suppose we have:
    Code:
    struct Student {int rollno, age; float marks; string name}
    .

    I initialize:
    Code:
    Student = {20,18};
    In this case I have only initialized "rollno" and "age". The rest of the members would contains garbage values and would not be initialized to "0" as is the case with array. Correct?

  8. #8
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,049

    Re: strcat(), searching for a name

    No. The rest of the structure is initialized with defaults which are zero or its equivalents for PODs.
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center