-
c++ Structures export data for specific name, country, last name
That is the structure that I created:
Code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct Students
{
char first_name[10];
char last_name[10];
char country[20];
};
int main()
{
Students array[10];
int n, i;
cin >> n;
for (i = 0; i < n; i++)
{
cout << "Name:";
cin >> array[i].first_name;
cout << "Last Name:";
cin >> array[i].last_name;
cout << "Country:";
cin >> array[i].country;
}
for (i = 0; i < n; i++)
{
cout << array[i].first_name << " ";
cout << array[i].last_name << " ";
cout << array[i].country << " ";
}
system("pause");
}
And I have to write code that once I enter John (for example) displays all information about him: last name, country. And when I enter country to export: first name, last name. Maybe I don't explain it properly. Because my english is bad. Maybe that's the reason that i can't find specific information or similar examples.
Code:
Ouput example:
n=2
Name:John
Last Name: Doe
Country: England
Name:Pete
Last Name: Donaldson
Country: USA
Name:John
Last Name: Peterson
Country: England
And that's the part that i can't do:
/Info about student/
Enter Name for check:
John
and here the output must be:
Name:John
Last Name: Doe
Country: England
Name:John
Last Name: Peterson
Country: England
another check:
if I enter Pete:
Name:Pete
Last Name: Donaldson
Country: USA
The same think for country, when if I enter England:
Name:John
Last Name: Doe
Country: England
Name:John
Last Name: Peterson
Country: England
-
Re: c++ Structures export data for specific name, country, last name
You need to iterate the array comparing the input data to the required data of the array element and if a match is found then display the contents of the array element. As you are using c-style strings, use strcmp() to compare. See https://msdn.microsoft.com/en-us/library/e0z9k731.aspx
-
Re: c++ Structures export data for specific name, country, last name
Quote:
Originally Posted by
2kaud
You need to iterate the array comparing the input data to the required data of the array element and if a match is found then display the contents of the array element. As you are using c-style strings, use strcmp() to compare. See
https://msdn.microsoft.com/en-us/library/e0z9k731.aspx
Thank you 2kaud. I try something. It looks silly but only this I managed to make.
Code:
for (i = 0; i < n; i++)
{
char name_for_check;
cin >> name_for_check;
if (strcmp(array[i].first_name, name_for_check) == 0)
{
cout << array[i].first_name << " ";
cout << array[i].last_name << " ";
cout << array[i].country << " ";
}
}
The code is wrong.
Quote:
Error C2664 'int strcmp(const char *,const char *)': cannot convert argument 2 from 'char' to 'const char *' 45
But I don't think that this is the only mistake.
-
Re: c++ Structures export data for specific name, country, last name
name_for_check is only a char, so it cannot store a string for the name to check.
Why are you not using std::string objects?
-
Re: c++ Structures export data for specific name, country, last name
Quote:
Originally Posted by
laserlight
name_for_check is only a char, so it cannot store a string for the name to check.
Why are you not using std::string objects?
Sorry. I didn't understand what you mean.
-
Re: c++ Structures export data for specific name, country, last name
Try this
Code:
char name_for_check[10];
cin >> name_for_check;
for (i = 0; i < n; i++)
{
if (strcmp(array[i].first_name, name_for_check) == 0)
{
cout << array[i].first_name << " ";
cout << array[i].last_name << " ";
cout << array[i].country << " ";
}
}
You need to ask for the input outside of the loop and make name_for_check a char array the same size as the struct element to check.
Quote:
Sorry. I didn't understand what you mean.
In c++, c-style strings are very rarely used as c++ has its own string type. One of the issues with using c-style strings is that you have specified the maximum size of the array as 10 for the first_name. This means that the maximum number of chars in the name can be only 9 (as the terminating NULL takes one position). If you enter more than 9 chars then this will be accepted but the code will overwrite what ever happens to be in memory - causing unwanted and undefined behaviour. This is called buffer overflow and is the siurce of many 'security type issues' within code.
Using the c++ string type this problem is avoided as you don't need to pre-allocate the size of the strings. See http://www.cplusplus.com/reference/string/string/ for more details.
NB You have the include for string in the code, so why not use it? :confused:
-
Re: c++ Structures export data for specific name, country, last name
I include string for case.
And I do not know how to do it with string objects...
Your suggestion works.
Do you have an idea how to do so that I can make an unlimited number of requests for name_for_check.
-
Re: c++ Structures export data for specific name, country, last name
Quote:
I include string for case.
No. You are including the c++ string type. For c-style strings you should include cctype if you want to use the c-style case functions.
Quote:
Do you have an idea how to do so that I can make an unlimited number of requests for name_for_check.
Yes - put the request for the name and the check within another loop. What will be the loop exit condition?
-
Re: c++ Structures export data for specific name, country, last name
-
Re: c++ Structures export data for specific name, country, last name
Quote:
And I do not know how to do it with string objects...
Is this a homework exercise?
-
Re: c++ Structures export data for specific name, country, last name
Quote:
Originally Posted by
fr4234
I think it should exit if there is no student with that name.
Then that's how you code it.
-
Re: c++ Structures export data for specific name, country, last name
Quote:
Originally Posted by
2kaud
Is this a homework exercise?
Not exactly. My homework is more complex, but this would help me somewhat.
-
Re: c++ Structures export data for specific name, country, last name
Quote:
Originally Posted by
2kaud
Then that's how you code it.
Is it possible to do it with do while.
-
Re: c++ Structures export data for specific name, country, last name
Quote:
And I do not know how to do it with string objects...
Then ask your teacher why you are being taught/asked to use c-style strings rather than the c++ string type? If you are learning c++ then you should be taught the c++ way - and that means c++ type string and not c-style strings. Black mark to teacher! :thumbd:
-
Re: c++ Structures export data for specific name, country, last name
Quote:
Originally Posted by
fr4234
Is it possible to do it with do while.
Yes.
-
Re: c++ Structures export data for specific name, country, last name
Well I do it like this:
Code:
char choose[1];
do {
cout << "New check? Y/N \n";
cin >> choose;
if (_stricmp("N", choose) == 0)
{
break;
}
char name_for_check[10];
cout << "\n Name for check:";
cin >> name_for_check;
for (i = 0; i < n; i++)
{
if (strcmp(array[i].first_name, name_for_check) == 0)
{
cout << array[i].first_name << " ";
cout << array[i].last_name << " ";
cout << array[i].country << endl;
}
}
}
while (_stricmp("Y", choose) == 0);
system("pause");
}
The code is working but after I run it it show this message:
Quote:
Run-Time Check Failure #2 - Stack around the variable 'choose' was corrupted.
-
Re: c++ Structures export data for specific name, country, last name
As choose is defined to be a c-style string and not a char, then
is going to treat choose as a c-style array and terminate it with a null. This makes the string at least 2 chars but you have only allocated space for 1!
This is why using c-style strings in a c++ program is a bad idea Get your teacher to explain using the c++ string type.