c ++ Structures导出特定名称的数据 [英] c++ Structures export data for specific name

查看:67
本文介绍了c ++ Structures导出特定名称的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我创建的结构:





< pre lang =C ++> #include < < span class =code-leadattribute> iostream >
#include < string >
#include < fstream >
使用 命名空间标准;


struct 学生
{
char first_name [ 10 ];
char last_name [ 10 ];
char country [ 20 ];


};
int main()
{
学生数组 [ 10 ];
int n,i;
cin>> N;

for (i = 0 ; i< n; i ++)
{
cout<< 名称:;
cin>> array [i] .first_name;
cout<< 姓氏:;
cin>> array [i] .last_name;
cout<< 国家/地区:;
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 );


}









我必须编写代码,一旦我进入John(例如)显示有关他的所有信息:姓氏,国家。当我输入国家出口:名字,姓氏。也许我没有正确解释。因为我的英语不好。也许这就是我无法找到具体信息或类似例子的原因。


输出示例:

 n = 2 
姓名:John
姓:Doe
国家:England

姓名:Pete
姓:Donaldson
国家:美国



这是我不能做的部分:

 /关于学生/ $的信息b $ b输入要检查的名称:
John



此处输出必须为:

姓名:John 
姓氏:Doe
国家:英格兰



另一张支票:

如果我输入Pete:

姓名:Pete 
姓:Donaldson
国家:美国



我我应该能够无限制地检查。



我试着这样做:



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



但是如何这样做我可以为name_for_check发出无限数量的请求。

我的意思是我可以多次检查名称。

解决方案

可能这个,我实际上并不了解问题所在描述:



  
{
char name_for_check [ 10 ] = < span class =code-string> \0;
cin>> name_for_check;
if (strcmp( quit,name_for_check)< 0
{
bool found = false ;
for (i = 0 ; i< n&& found == < span class =code-keyword> 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( < span class =code-string>退出,name_for_check));





以下代码请求用户无限输入,直到输入包含quit的字符串。我认为它应该解决以下问题


That is the structure that I created:


#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:

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.

解决方案

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

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


这篇关于c ++ Structures导出特定名称的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆