尝试了一个基本的C ++挑战,(C ++初学者),我制作了这段代码。我知道在数组中调用一个值从零开始,但我希望用户输入1-5而不是0-4,因为我不想走简单的路线,想看看我是否可以这样做。 / p>
这是我的问题,我做了一个基本功能,从int选项中减去1,允许用户输入1-5,但数组看到的值为1-4。但是,如此图像所示,它似乎忽略了我的功能并跳到代码的下一部分。我在下面提供了我的代码。
#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include <string>
using namespace std;
string drink[5] = { "Coke", "Water", "Sprite", "Monster", "Diet Coke" };
int choice;
int correct = 0;
void AdjArray()
{
choice--;
};
int main()
{
while (correct != 1)
{
cout << "Enter the number of the beverage you would like." << endl;
cout
<< " Coke = 1\n Water = 2\n Sprite = 3\n Monster = 4\n Diet Coke = 5"
<< endl;
cin >> choice;
AdjArray;
if (choice >= 0 && choice <= 4)
{
cout << "You have chosen " << drink[choice] << "." << endl;
correct = 1;
}
else
{
system("cls");
cout << "Error, you entered: " << choice
<< ". Please enter a number between 1 and 5.\n" << endl;
}
}
return 0;
}
答案 0 :(得分:2)
你没有打电话给你的功能。将AdjArray;
更改为AdjArray();