如何从数组名称中显示名称? [英] How to display the name from the array name?

查看:89
本文介绍了如何从数组名称中显示名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习关于数组的c ++,我在显示数组名称中的名称时遇到问题



因为当我尝试显示名称时根据他在第一个字母所在位置的乘客成为他姓名的最后一个字母..嗯



这是我的代码



  #include    <   iostream.h  >  
#include < conio .h >

char * name [ 10 ] = { };
int * seat = 0 ;
char * pname = 乘客是:;
char ask;
void display();

int main()
{
clrscr();
cout<< 输入姓名和座位号......< < ENDL;
do
{
cout<< \ n输入你的名字:;
cin>> * name;
cout<< 输入您的座位号:;
cin>> * seat;
cout<< 你想再次输入吗?(是/否):;
cin>> ask;
}
while (ask == ' y' || ask == ' Y');
cout<< \\\
你想看到乘客的名字吗?(是/否): ;
cin>> ask;
if (ask == ' y' || ask == ' Y'
{
cout<< \ n该计划现在将引导您显示乘客的姓名......<< ENDL;
display();
}
else
{
cout<< \ n \ n本程序将很快结束....;
}
getch();
return 0 ;
}

void display()
{
执行
{
cout<< \ n输入要显示的座位号乘客姓名:;
cin>> * seat;
cout<< pname<< * name [* seat- 1 ]<< endl;
cout<< 你想再试一次吗?(是/否):;
cin>> ask;
}
while (ask == ' y' || ask == ' Y');
cout<< \ n该计划现在很快就会结束...... ;
getch();
}

解决方案

如果我理解你的要求,你需要一个字符串数组以及一系列座位号(或者更好的结构数组,乘客姓名座位号作为成员)。你的程序没有那些。它看起来你缺乏对C ++(和C)数组和字符串处理的基本理解(例如你真的不需要指针)。

你可以写:

< pre lang =C ++> #include < < span class =code-leadattribute> iostream >
#include < vector >

使用 namespace std;

struct 乘客
{
字符串名称;
int seat;
};

int main()
{
vector< Passenger>乘客;
char answer;
do
{

乘客p;
cout<< endl<< 输入乘客姓名;
cin>> p.name;
cout<< endl<< 输入乘客座位号;
cin>> p.seat;
passenger.push_back(p);
cout<< endl<< 你想继续吗?;
cin>>回答;
} while (answer == ' y );

int n,seat;

cout<< endl<< 输入座位号;
cin>>座位;

for (n = 0 ; n< passenger.size(); ++ n)
{
if (seat == passenger [n] .seat)
断裂;
}

if (n!= passenger.size())
cout<< endl<< 找到乘客<<乘客[N]。名称;
else
cout<< endl<< 抱歉,不匹配;
cout<< ENDL;
}


I'm trying to study c++ about array and I'm having a problem displaying the name from the array name

Because when I try to display the name of the passenger based on where he seat the first letter becomes the last letter of his name..ahmm

Here's my code

#include <iostream.h>
#include <conio.h>

char* name[10]={" "};
int* seat=0;
char* pname="The Passenger is: ";
char ask;
void display();

int main()
{
	clrscr();
	cout<<"The Entering of name and seat number..."<<endl;
	do
	{
		cout<<"\nEnter your name: ";
		cin>>*name;
		cout<<"Enter your seat number: ";
		cin>>*seat;
		cout<<"Do you want to input again?(Y/N): ";
		cin>>ask;
	}
	while(ask=='y'||ask=='Y');
	cout<<"\nDo you want to see the passenger's name?(Y/N): ";
	cin>>ask;
	if(ask=='y'||ask=='Y')
	{
		cout<<"\nThe Program will now direct you to the displaying of passenger's name...."<<endl;
		display();
	}
	else
	{
		cout<<"\n\nThe Program will end shortly....";
	}
	getch();
	return 0;
}

void display()
{
	do
	{
		cout<<"\nEnter seat number to display passenger's name: ";
		cin>>*seat;
		cout<<pname<<*name[*seat-1]<<endl;
		cout<<"Do you want to try again?(Y/N): ";
		cin>>ask;
	}
	while(ask=='y'||ask=='Y');
	cout<<"\nThe Program will now end shortly....";
	getch();
}

解决方案

If I understood correctly your requirements, you need an array of strings as well an array of seat numbers (or better an array of structs, with passenger name and seat number as members). Your program hasn't those. It looks you lack of a basic understanding on C++ (and C) array and string handling (e.g. you really don't need pointers).
You could write:

#include <iostream>
#include <vector>

using namespace std;

struct Passenger
{
  string name;
  int seat;
};

int main()
{
  vector <Passenger> passenger;
  char answer;
  do
  {

    Passenger p;
    cout << endl << "enter passenger name ";
    cin >> p.name;
    cout << endl << "enter passenger seat number ";
    cin >> p.seat;
    passenger.push_back(p);
    cout << endl << "do you wish to continue ? ";
    cin >> answer;
  } while ( answer == 'y');

  int n, seat;

  cout << endl << "enter the seat number ";
  cin >> seat;

  for (n=0; n<passenger.size(); ++n)
  {
    if ( seat == passenger[n].seat )
      break;
  }

  if ( n != passenger.size() )
    cout << endl << "found passenger " << passenger[n].name;
  else
    cout << endl << "sorry, no match";
  cout << endl;
}


这篇关于如何从数组名称中显示名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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