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 2Code:// 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; }
OUTPUT for CODE 2Code:// 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; }
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 . . .




Reply With Quote