CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Dec 2015
    Posts
    9

    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.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    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)

  3. #3
    Join Date
    Dec 2015
    Posts
    9

    Re: c++ Structures export data for specific name, country, last name

    Quote Originally Posted by 2kaud View Post
    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Dec 2015
    Posts
    9

    Re: c++ Structures export data for specific name, country, last name

    Quote Originally Posted by laserlight View Post
    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.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    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)

  7. #7
    Join Date
    Dec 2015
    Posts
    9

    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.

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    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)

  9. #9
    Join Date
    Dec 2015
    Posts
    9

    Re: c++ Structures export data for specific name, country, last name

    ...
    Last edited by fr4234; January 2nd, 2016 at 04:17 PM.

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    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)

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: c++ Structures export data for specific name, country, last name

    Quote Originally Posted by fr4234 View Post
    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)

  12. #12
    Join Date
    Dec 2015
    Posts
    9

    Re: c++ Structures export data for specific name, country, last name

    Quote Originally Posted by 2kaud View Post
    Is this a homework exercise?
    Not exactly. My homework is more complex, but this would help me somewhat.

  13. #13
    Join Date
    Dec 2015
    Posts
    9

    Re: c++ Structures export data for specific name, country, last name

    Quote Originally Posted by 2kaud View Post
    Then that's how you code it.
    Is it possible to do it with do while.

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    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)

  15. #15
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: c++ Structures export data for specific name, country, last name

    Quote Originally Posted by fr4234 View Post
    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)

Page 1 of 2 12 LastLast

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
  •  





Click Here to Expand Forum to Full Width

Featured