Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
That is the structure that I created:


C++
#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.

Ouput example:
n=2
Name:John
Last Name: Doe
Country: England

Name:Pete
Last Name: Donaldson
Country: USA

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

another check:
if I enter Pete:
Name:Pete
Last Name: Donaldson
Country: USA

I should be able to make unlimited checks.

I tryed to make it like this:

C++
for (i = 0; i < n; i++)
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 << " ";
	}
}
}

But how to do so that I can make an unlimited number of requests for name_for_check.
I mean that i can check name more than one time.
Posted
Updated 2-Jan-16 10:06am
v7
Comments
Sergey Alexandrovich Kryukov 2-Jan-16 14:41pm    
It's not clear where your problem is. What have you tried so far?
—SA
Member 12204396 2-Jan-16 14:49pm    
I tryed to make it like that but it doesnt work:

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 << " ";
}
}

Sergey Alexandrovich Kryukov 2-Jan-16 15:19pm    
You are trying to input name_for_check n times.
—SA
Member 12204396 2-Jan-16 15:23pm    
Yes, actually I'm not really aware of what I do.
Sergey Alexandrovich Kryukov 2-Jan-16 16:55pm    
You need to understand each line of what you do.
—SA

1 solution

Probably this one, I don't actually understand the condition of the problem being described:

C++
do
{
     char name_for_check[10] = "\0";
     cin >> name_for_check;
     if (strcmp("quit", name_for_check) < 0)
     {
         bool found = false;
         for (i = 0; i < n && found == false; i++)
    	      if (!strcmp(array[i].first_name, name_for_check))
              { 	
		  cout << array[i].first_name << " ";
		  cout << array[i].last_name << " ";
		  cout << array[i].country << " ";
                  found = true;
  	      }
     }
}while(!strcmp("quit", name_for_check));


The following code requsts for a user input infinitely until the string containing "quit" is typed in. I think that it should solve the following problem
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900