-
January 2nd, 2016, 12:23 PM
#1
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
Last edited by fr4234; January 2nd, 2016 at 01:44 PM.
-
January 2nd, 2016, 02:02 PM
#2
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
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 2nd, 2016, 02:32 PM
#3
Re: c++ Structures export data for specific name, country, last name
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.
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.
-
January 2nd, 2016, 02:46 PM
#4
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?
-
January 2nd, 2016, 03:05 PM
#5
Re: c++ Structures export data for specific name, country, last name
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.
-
January 2nd, 2016, 03:42 PM
#6
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.
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?
Last edited by 2kaud; January 2nd, 2016 at 03:46 PM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 2nd, 2016, 03:59 PM
#7
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.
-
January 2nd, 2016, 04:09 PM
#8
Re: c++ Structures export data for specific name, country, last name
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.
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?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 2nd, 2016, 04:13 PM
#9
Re: c++ Structures export data for specific name, country, last name
Last edited by fr4234; January 2nd, 2016 at 04:17 PM.
-
January 2nd, 2016, 04:15 PM
#10
Re: c++ Structures export data for specific name, country, last name
And I do not know how to do it with string objects...
Is this a homework exercise?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 2nd, 2016, 04:17 PM
#11
Re: c++ Structures export data for specific name, country, last name
Originally Posted by fr4234
I think it should exit if there is no student with that name.
Then that's how you code it.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 2nd, 2016, 04:20 PM
#12
Re: c++ Structures export data for specific name, country, last name
Originally Posted by 2kaud
Is this a homework exercise?
Not exactly. My homework is more complex, but this would help me somewhat.
-
January 2nd, 2016, 04:26 PM
#13
Re: c++ Structures export data for specific name, country, last name
Originally Posted by 2kaud
Then that's how you code it.
Is it possible to do it with do while.
-
January 2nd, 2016, 04:28 PM
#14
Re: c++ Structures export data for specific name, country, last name
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!
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 2nd, 2016, 04:28 PM
#15
Re: c++ Structures export data for specific name, country, last name
Originally Posted by fr4234
Is it possible to do it with do while.
Yes.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Tags for this Thread
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
|