如何显示数组名称中的名称

How to display the name from the array name?

本文关键字:数组 何显示 显示      更新时间:2023-10-16

我正在尝试研究有关数组的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

尽管如此,您可能需要在基本语言功能(如指针)方面进行更多练习,您可以使用一些现有的数据结构。由于您使用的是 coutcin,因此我假设您的编译器支持 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