如何从数组名称中显示名称? 我试图研究C++的数组 我在显示数组名中的名称时遇到问题
因为当我试着根据乘客的座位显示他的名字时 第一个字母成为他名字的最后一个字母 这是我的密码如何从数组名称中显示名称? 我试图研究C++的数组 我在显示数组名中的名称时遇到问题,c++,arrays,C++,Arrays,因为当我试着根据乘客的座位显示他的名字时 第一个字母成为他名字的最后一个字母 这是我的密码 #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 Enteri
#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();
}
#包括
#包括
char*name[10]={”“};
int*seat=0;
char*pname=“乘客是:”;
查问;
void display();
int main()
{
clrsc();
您似乎对指针有问题。即
int* seat=0;
不会为整数分配空间,而是为指针分配空间,然后设置此指针并将其设置为地址0。执行
cin>>*seat;
您的程序尝试取消引用不指向任何内容的指针。此外,当您执行
cin >>*name
您不会写入整个字符串,而是写入取消引用位置,即字符串的第一个字符
换换口味
int* seat=0;
到
及
到
祝你好运
编辑:
您还想更改以下选项:
cin >> *seat;
到
你似乎对指针有问题,即
int* seat=0;
不会为整数分配空间,而是为指针分配空间,然后设置此指针并将其设置为地址0。执行
cin>>*seat;
您的程序尝试取消引用不指向任何内容的指针。此外,当您执行
cin >>*name
您不会写入整个字符串,而是写入取消引用位置,即字符串的第一个字符
换换口味
int* seat=0;
到
及
到
祝你好运
编辑:
您还想更改以下选项:
cin >> *seat;
到
你似乎对指针有问题,即
int* seat=0;
不会为整数分配空间,而是为指针分配空间,然后设置此指针并将其设置为地址0。执行
cin>>*seat;
您的程序尝试取消引用不指向任何内容的指针。此外,当您执行
cin >>*name
您不会写入整个字符串,而是写入取消引用位置,即字符串的第一个字符
换换口味
int* seat=0;
到
及
到
祝你好运
编辑:
您还想更改以下选项:
cin >> *seat;
到
你似乎对指针有问题,即
int* seat=0;
不会为整数分配空间,而是为指针分配空间,然后设置此指针并将其设置为地址0。执行
cin>>*seat;
您的程序尝试取消引用不指向任何内容的指针。此外,当您执行
cin >>*name
您不会写入整个字符串,而是写入取消引用位置,即字符串的第一个字符
换换口味
int* seat=0;
到
及
到
祝你好运
编辑:
您还想更改以下选项:
cin >> *seat;
到
要显示数组名称中的名称,首先必须正确定义数组。)
如果您还不知道标准类std::string
,那么数组应该定义为
char name[10][20];
也就是说,它可以存储10个长度不超过20个字符的名称
或者,如果您知道classstd:string
,则可以将其定义为
#include <string>
std::string name[10];
#包括
std::字符串名称[10];
而且最好是定义
#include <string>
#include <array>
std::array<std::string, 10> name;
#包括
#包括
std::数组名;
对于该程序,不需要定义任何指针
考虑到当您要求输入座位号时,您应该检查该号码是否在1-10范围内(在这种情况下,当您使用它访问阵列时,您需要将其减少1)或0-9。要显示阵列名称中的名称,首先要正确定义阵列。)
如果您还不知道标准类std::string
,那么数组应该定义为
char name[10][20];
也就是说,它可以存储10个长度不超过20个字符的名称
或者,如果您知道classstd:string
,则可以将其定义为
#include <string>
std::string name[10];
#包括
std::字符串名称[10];
而且最好是定义
#include <string>
#include <array>
std::array<std::string, 10> name;
#包括
#包括
std::数组名;
对于该程序,不需要定义任何指针
考虑到当您要求输入座位号时,您应该检查该号码是否在1-10范围内(在这种情况下,当您使用它访问阵列时,您需要将其减少1)或0-9。要显示阵列名称中的名称,首先要正确定义阵列。)
如果您还不知道标准类std::string
,那么数组应该定义为
char name[10][20];
也就是说,它可以存储10个长度不超过20个字符的名称
或者,如果您知道classstd:string
,则可以将其定义为
#include <string>
std::string name[10];
#包括
std::字符串名称[10];
而且最好是定义
#include <string>
#include <array>
std::array<std::string, 10> name;
#包括
#包括
std::数组名;
对于该程序,不需要定义任何指针
考虑到当您要求输入座位号时,您应该检查该号码是否在1-10范围内(在这种情况下,当您使用它访问阵列时,您需要将其减少1)或0-9。要显示阵列名称中的名称,首先要正确定义阵列。)
如果您还不知道标准类std::string
,那么数组应该定义为
char name[10][20];
也就是说,它可以存储10个长度不超过20个字符的名称
或者,如果您知道classstd:string
,则可以将其定义为
#include <string>
std::string name[10];
#包括
std::字符串名称[10];
而且最好是定义
#include <string>
#include <array>
std::array<std::string, 10> name;
#包括
#包括
std::数组名;
为了这个节目