如何将用户字符串输入存储在数组中,C++
How to store user string input in array, C++
我正在构建一个选择你自己的冒险。
因此,基本上,在用户输入他们的名称后,我希望playerList开关语句将输入的内容存储到basePlayer[]中。
此外,如果你要运行这个程序,你必须在输入你的名字后单击向下箭头才能进入playerList菜单,因为它有点bug。
我们将不胜感激。
谢谢,Mike
#include <iostream>
#include <string>
#include <windows.h>
#include <cmath>
#include <cstring>
#include <cstdlib>
using namespace std;
int main(){
Beep(251.63,100);
Beep(329.63,100);
Beep(392,100);
Beep(251.63,100);
Beep(329.63,100);
Beep(392,100);
Beep(251.63,100);
Beep(329.63,100);
Beep(392,100);
string playerList[6] = {"Abbot", "Sear", "Hellion", "Vagabond", "Knave","##QUIT##"};
string cityList[4] = {"city1","city2","city3","city4"};
string spiritList[4] = {"spirit1","spirit2","spirit3","spirit4",};
string yesNo[2] = {"yes","no"};
string name;
string player;
string city;
string spirit;
string basePlayer[4];
basePlayer[0] = name;
basePlayer[1] = player;
basePlayer[2] = city;
basePlayer[3] = spirit;
int pointer = 0;
while(true){
cout << "What is your name?" << endl;
cin >> name;
cout << "your name is " << name << "?" << endl;
basePlayer[0] = name;
cout << basePlayer[0];
int pointer = 0;
while(true){
system("cls");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 5);
cout << basePlayer[0]<<" please choose your Adventurer:n*tab* for descriptionnn";
for (int row = 0; row < 6; ++row){
if(row == pointer){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
cout << playerList[row] << endl;}
else{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 5);
cout << playerList[row] << endl;}}
while(true){
if (GetAsyncKeyState(VK_UP) != 0){
Beep(800,50);
pointer -= 1;
if (pointer == -1){
pointer = 0;}
break;}
else if (GetAsyncKeyState(VK_DOWN) != 0){
Beep(800,50);
pointer += 1;
if (pointer == 6){
pointer = 0;}
break;}
else if (GetAsyncKeyState(VK_TAB) != 0){
Beep(1200,50);
Beep(1000,50);
switch(pointer){
case 0:{
system("cls");
cout << "AbbotFacts.nn*Enter* to become the Abbotn*ArrowDown* to return to TitleScreen";
Sleep(1000);
}
break;
case 1:{
system("cls");
cout << "SearFacts.nn*Enter* to become the Searn*ArrowDown* to return to TitleScreen";
Sleep(1000);
}
break;
case 2:{
system("cls");
cout << "HellionFacts.nn*Enter* to become the Hellionn*ArrowDown* to return to TitleScreen";
Sleep(1000);
}
break;
case 3:{
system("cls");
cout << "VagabondFacts.nn*Enter* to become the Vagabondn*ArrowDown* to return to TitleScreen";
Sleep(1000);
}
break;
case 4:{
system("cls");
cout << "KnaveFacts.nn*Enter* to become the Knaven*ArrowDown* to return to TitleScreen";
Sleep(1000);
}
break;
case 5:{return 0;}
break;
break;}}
else if (GetAsyncKeyState(VK_RETURN) != 0){
Beep(1000,50);
Beep(1200,50);
switch(pointer){
case 0:{
system("cls");
cout << "You have chosen the Abbot"<< endl;
Sleep(1000);
}
break;
case 1:{
system("cls");
cout << "You have chosen the Sear"<< endl;
Sleep(1000);
}
break;
case 2:{
system("cls");
cout << "You have chosen the Hellion"<< endl;
Sleep(1000);
}
break;
case 3:{
system("cls");
cout << "You have chosen the Vagabond" << endl;
Sleep(1000);
}
break;
case 4:{
system("cls");
cout << "You have chosen the knave"<< endl;
Sleep(1000);
}
break;
case 5:{return 0;
break;}
break;}}
}
Sleep(150);
}
}
}
尝试从程序中删除语句cin.get()
,因为cin >> name
输入name
,因此不使用cin.get()
。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 使用strcpy将char数组的元素复制到另一个数组
- 使用指针从C++中的数组中获取最大值
- C++使用整数的压缩数组初始化对象
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '