如何显示数组名称中的名称
How to display the name from the array name?
我正在尝试研究有关数组的c ++。并且我在显示数组名称中的名称时遇到问题
因为当我尝试根据乘客的座位显示乘客的姓名时第一个字母成为他名字的最后一个字母。啊嗯
这是我的代码
#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<<"nnThe 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();
}
显示数组名称中的名称,首先 必须正确定义数组:)
如果您还不知道标准类std::string
那么数组应该定义为例如
char name[10][20];
也就是说,它可以存储长度不超过 10 个字符的名称 20 个字符。
或者,如果您了解类std:string
那么您可以将其定义为
#include <string>
std::string name[10];
定义会更好
#include <string>
#include <array>
std::array<std::string, 10> name;
对于此程序,不需要定义任何指针。
考虑到当您要求输入座位号时,您应该检查该号码是否在 1 - 10 范围内(在这种情况下,当使用它访问阵列时,您需要将其减少 1)或 0 - 9。
您似乎在指针方面遇到了问题。即
int* seat=0;
不会为整数分配空间,而是为指针分配空间,然后设置此指针并将其设置为地址 0。当您执行时
cin>>*seat;
程序尝试取消引用不指向任何内容的指针。此外,当您执行时
cin >>*name
您不是写入整个字符串,而是写入取消引用位置,即字符串的第一个字符。
对于开始更改
int* seat=0;
自
int seat = 0
和
cin>>*name;
自
cin>>name;
祝你好运!
编辑:
您还想更改:
cin >> *seat;
自
cin >> seat
尽管如此,您可能需要在基本语言功能(如指针)方面进行更多练习,您可以使用一些现有的数据结构。由于您使用的是 cout 和 cin,因此我假设您的编译器支持 C++ 标准库。我会用地图来拯救乘客。
#include <iostream>
#include <map>
struct passenger
{
char name[10];
};
std::map<int, passenger> passengers;
int seat;
char ask;
void display();
int main()
{
std::cout<< "The Entering of name and seat number..." << std::endl;
do
{
passenger p;
std::cout << "nEnter your name: ";
std::cin >> p.name;
std::cout << "Enter your seat number: ";
std::cin >> seat;
// add passenger to the map
passengers.insert(std::pair<int, passenger>(seat, p));
std::cout << "Do you want to input again?(Y/N): ";
std::cin >> ask;
}
while(ask=='y'||ask=='Y');
std::cout << "nDo you want to see the passenger's name?(Y/N): ";
std::cin >> ask;
if(ask=='y' || ask=='Y')
{
std::cout << "nThe Program will now direct you to the displaying of passenger's name...." << std::endl;
display();
}
return 0;
}
void display()
{
do
{
std::cout << "nEnter seat number to display passenger's name: ";
std::cin >> seat;
// check if passenger data is available for this seat
if(passengers.find(seat) != passengers.end()) {
std::cout << "The Passenger is: " << passengers[seat].name << std::endl;
} else {
std::cout << "Seat still available ..." << std::endl;
}
std::cout << "Do you want to try again?(Y/N): ";
std::cin >> ask;
}
while(ask=='y' || ask=='Y');
}
使用的是指针*seat
则不必将其访问为 cin>>*seat
.你只需要写cin>>seat
.在学习 cpp 时,也请查看指针是什么。
这里也有一些信息,尽管这并不完全是主题,
int *S; // pointer of type int
int x; // variable of type int
S = &x; // pointer S is now pointing to memory location of x
S = 4;
或
x = 4; 更改 S 指向的内存位置的内容
<<*S; 显示 S 指向的内存地址,即x
- 在数组中显示第一个元素用户输入
- 如何在结构数组中显示学生姓名以及高于平均 GPA 的 ID
- 使用 memset 填充数组会显示垃圾
- 我将如何将数组的显示从数字更改为字符串C++
- 在二维数组中显示两侧的总和
- 随机化字符数组以显示随机密码
- 如何在来自不同类的字符串或字符数组中显示特定字符
- 将名称分配给座位并在数组中显示
- 将字符串添加到2D数组并显示COUT消息(没有错误消息?)的错误
- 循环数组时显示'stack smashing detected abc2 terminated Aborted'
- 字符* 数组出现"multiple definitions"错误,但无符号字符数组未显示错误
- 二维数组未显示正确的输出
- C++返回数组并显示它
- 如何在数组中显示二进制搜索树结果
- 在二维数组中显示字符
- c++字符数组不显示地址
- 我的字符数组只显示最后几个字符.c++
- 在一个RECT数组中显示所有向量
- 在数组中显示最小值
- 读取2d数组并显示它(c++)