簡體   English   中英

如何將用戶字符串輸入存儲在數組中,C ++

[英]How to store user string input in array, C++

我正在建立自己的冒險選擇。

因此,基本上,在用戶輸入名稱之后,我希望playerList開關語句存儲輸入到basePlayer []中的所有內容。

同樣,如果要運行此程序,則在輸入名稱后必須單擊向下箭頭才能進入playerList菜單,因為它存在多種故障。

幫助將不勝感激。

謝謝邁克

#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  description\n\n";

    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.\n\n*Enter* to become the Abbot\n*ArrowDown* to      return to TitleScreen";
        Sleep(1000);
    }
        break;
    case 1:{
        system("cls");
        cout << "SearFacts.\n\n*Enter* to become the Sear\n*ArrowDown* to  return to TitleScreen";
        Sleep(1000);
    }
        break;
    case 2:{
        system("cls");
        cout << "HellionFacts.\n\n*Enter* to become the Hellion\n*ArrowDown* to return to TitleScreen";
        Sleep(1000);
    }
        break;
    case 3:{
        system("cls");
        cout << "VagabondFacts.\n\n*Enter* to become the Vagabond\n*ArrowDown* to return to TitleScreen";
        Sleep(1000);
    }
        break;
    case 4:{
        system("cls");
        cout << "KnaveFacts.\n\n*Enter* to become the Knave\n*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()無效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM