Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;:从数组中挑选某物_C++_Arrays - Fatal编程技术网

C++ C++;:从数组中挑选某物

C++ C++;:从数组中挑选某物,c++,arrays,C++,Arrays,我对编程很陌生,如果我说的任何话都没有意义,那么很抱歉! 我目前正在尝试制作一个程序,发现自己陷入了一个小问题。我制作了一个数组,名为choice,choice[0]=one,choice[1]=two, 当我问用户一些问题时,例如 我希望能够让用户从阵列中选择他们想要的内容 cout << "Which would you like? Enter '0' for one, enter '1' for two " << endl; cin >> choice

我对编程很陌生,如果我说的任何话都没有意义,那么很抱歉! 我目前正在尝试制作一个程序,发现自己陷入了一个小问题。我制作了一个数组,名为
choice
choice[0]=one
choice[1]=two

当我问用户一些问题时,例如

我希望能够让用户从阵列中选择他们想要的内容

cout << "Which would you like? Enter '0' for one, enter '1' for two " << endl;
cin >> choice

我希望它能打印出“You select red”或“You select blue”,只需输入“0”或“1”

您希望输入成为数组的索引,因此类似于:

size_t index;
cout << "Which would you like? Enter '0' for one, enter '1' for two " << endl;
cin >> index;
cout << "\nYou chose: " << choice[index] << endl;
size\t索引;
库特指数;
不能包含
#包括
#包括
使用名称空间std;
字符串arr[5]={“一”、“二”、“三”、“四”、“五”};
字符串返回arraypos(int-pos)
{
如果(pos>=0&&pos<(sizeof(arr)/sizeof(*arr)))
返回arr[pos];
其他的
返回“错误的索引!”;
}
int main(){
int pos=0;
cin>>pos;
不能包含
#包括
使用名称空间std;
int main()
{
字符串选择[2];
int userschoice;
选项[0]=“红色”;
选项[1]=“蓝色”;
学校;
如果(userschoice==0)
{
不能包含
#包括
使用名称空间std;
int main()
{
字符串选择[2];
int用户选择;
选项[0]=“红色”;
选项[1]=“蓝色”;
不能选择;

如果(userchoice>1)可以请添加一些代码(或正确格式化),否则至少我)帮不了你所以,当用户输入0时,你希望它打印“一”,当用户输入1时,你希望它打印“二”?您应该学习更多关于基本类型的知识。看来choice是一个数组,您可以将其转换为double或int,但肯定不能转换为数组。字符串choice[2]的末尾缺少分号
,并且你不需要在任何地方定义一个
用户选择变量。也许在这一点上,最好先学习一些基础知识。试着编写使用硬编码的数字和值的程序,然后试着用用户输入来替换它们!我只是快速编写了代码,而不是从程序本身复制过来的,我确实有一个se字符串选择[2]结尾的micolon,我也在我的程序中定义了userchoice-alsoThanks为了帮助,我尝试了你所说的,它说错误:没有运算符“[]”匹配这些操作数操作数类型是std::string[2][std::string]@HelpIsNeeded:Then
choice
并不是您的代码让我们相信的那样。例如,此代码将适用于
std::string choice[2]
。在主要帖子中添加了一些示例代码,以尝试更清楚地说明我希望我的程序执行的操作:)虽然这个答案可能是正确和有用的,但最好是随附一些解释来解释它如何帮助解决问题。如果有变化,这在将来会特别有用(可能不相关)导致它停止工作,用户需要了解它曾经是如何工作的。
size_t index;
cout << "Which would you like? Enter '0' for one, enter '1' for two " << endl;
cin >> index;
cout << "\nYou chose: " << choice[index] << endl;
#include <iostream>
#include <string.h>
#include <cstdlib>
using namespace std;

string arr[5] = { "one" , "two" , "three" , "four" , "five" };

string returnArrayPos( int pos )
{
    if( pos >= 0 && pos < ( sizeof( arr ) / sizeof( *arr ) ) )
        return arr[pos];
    else
        return "Wrong Index!";
}

int main() {
    int pos = 0;

    cin >> pos;
    cout << returnArrayPos( pos ) << endl;

    return 0;
}
#include <iostream>
#inclue <string>
using namespace std;

int main()
{
string choice[2];
int userschoice;

choice[0] = "Red";
choice[1] = "Blue";

cout << "Type '0' to choose red, type '1' to choose blue" << endl;
cin >> userschoice;
if(userschoice == 0)
{
cout << "You chose " << choice[0] << endl;
}
else if(userschoice == 1)
{
cout << "You chose " << choice[1] << endl;
}
}
#include <iostream>
#include <string>
using namespace std;

int main()
{
string choice[2];
int userchoice;

choice[0] = "Red";
choice[1] = "Blue";

cout << "Type '0' to choose red, type '1' to choose blue" << endl;
cin >> userchoice;
if(userchoice>1) cout<<"Sorry my vocabulary isn't large enough..";
else
cout << "You chose " << choice[userchoice] << endl;
return 0;}