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 . . .
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:
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;
}
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.
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.
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?
Bookmarks