-
June 16th, 2011, 06:16 PM
#1
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 . . .
-
June 16th, 2011, 06:45 PM
#2
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";
-
June 16th, 2011, 07:19 PM
#3
Re: strcat(), searching for a name
Originally Posted by Philip Nicoletti
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;
}
-
June 16th, 2011, 07:32 PM
#4
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 ?
-
June 16th, 2011, 09:06 PM
#5
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.
-
June 17th, 2011, 02:59 AM
#6
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|