簡體   English   中英

C ++如何從字符串數組元素增加int變量

[英]C++ How to Increment Int Variable From String Array Element

我想知道在調用特定的字符串數組元素時是否有增加int變量的方法? 我正在做一個有趣的項目,遇到了障礙。 我項目的目標是擁有一個確定用戶喜歡哪種電子音樂類型的程序。 該程序將顯示兩個DJ,並詢問用戶他們更喜歡哪個DJ或讓他們都不選擇。 每個DJ最多將擁有三種他們主要擅長的流派,並且我為每種流派創建了int變量(所有變量都設置為0)。 一旦用戶選擇了DJ,我希望將點分配給與DJ關聯的每個體裁變量。 我不確定如何設置此規則,因為到目前為止我嘗試過的所有操作都失敗了(代碼中注釋了兩個示例嘗試)。 最終,我的計划是開發可以隨機選擇DJ的邏輯,但是我需要首先設置類型分配。 有什么辦法可以做到嗎? 任何幫助將不勝感激。 干杯!

###include "stdafx.h"
###include < iostream>
###include < iomanip>
###include < string>
###include < array>
###using namespace std;



int main()
{
    cout << "Hello! This program is designed to figure out what Electronic Music you like based on artists presented and the answers you choose...\n" << endl;
    cout << "When you are ready to begin press \"Enter\"..." << endl;
    getchar();


int bigRoom = 0;
int deepHouse = 0;
int drumBass = 0;
int dubstep = 0;
int electroHouse = 0;
int futureHouse = 0;
int hardDance = 0;
int house = 0;
int progressiveHouse = 0;
int techno = 0;
int trance = 0;
int trap = 0;

string textArray[5]{ "DeadMau5", "Armin Van Buuren", "Avicii", "Ferry Corsten", "Kaskade"};
string answer;
    cout << "Select the DJ you prefer by number. Otherwise select 3 if you don't know them. " << endl; //Haven't coded option 3 yet.
    cout << "1 - " << textArray[1] << endl;
    cout << "2 - " << textArray[2] << endl;

    cin >> answer;


    /*
    if (textArray[1]) {
        ++trance;
    }

    for (textArray[1]) {
        ++trance;
    }
    */

if (answer == "1") {
    cout << "You have selected: " << textArray[1] << endl;
}

else if (answer == "2") {
    cout << "You have selected: " << textArray[2] << endl;
}


//cout << trance << endl;


}

您可以在用戶選擇DJ之后增加每個提示音:

if (answer == "1") {
    cout << "You have selected: " << textArray[1] << endl;
    ++trance;
    // ++ ohter genres you want to increment
}

else if (answer == "2") {
    cout << "You have selected: " << textArray[2] << endl;
    ++trance;
    // ++ ohter genres you want to increment
}

暫無
暫無

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

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