2015-12-21 76 views
-6

主: https://github.com/MJGHD/Stacks./blob/master/main.cpp布爾值不變? [C++]

//Imports needed libraries, headers and defines the size of the question and answer variables 

#include <iostream> 
#include <cstdlib> 
#include <string> 
#include "lib/script.h" 
#define STRING_SIZE 1000 

//Initalises the temporary dummy variable and the question + answer variables 
std::string questions[STRING_SIZE]; 
std::string answers[STRING_SIZE]; 

//Initalises showMainMenu function 
void showMainMenu(); 


int main() { 
    //Shows the main menu 
    showMainMenu(); 
    return EXIT_SUCCESS; 
} 

void showMainMenu() { 
    std::cout << "Welcome to Stack.! To get started, type in the number from 1-4 that you desire!\n\n"; 
    std::cout << "1. Create new stack\n"; 
    std::cout << "2. Open existing stack\n"; 
    std::cout << "3. Export existing stack\n"; 
    std::cout << "4. Options\n\n"; 
    std::cout << ""; 
    //Assigns the user input to the dummy variable initalised earlier 
    std::cin >> dummy.usrInput; 
    //Reads the usrInput and figures out what the user wanted 
    switch(dummy.usrInput){ 
     case 1: 
      createNewStack(); 
     case 2: 
      openExistingStack(); 
     case 3: 
      exportExistingStack(); 
     case 4: 
      showOptions(); 
     default: 
      std::cout << "That was not a valid input. Press enter to continue... "; 
      std::cin.get(); 
      clearScreen(); 
    } 
} 

腳本: https://github.com/MJGHD/Stacks./blob/master/lib/script.h

#include <iostream> 
#include <cstdlib> 
#include <string> 
#include <cstdio> 

struct usrOptions{ 
    bool autosave, randomisedCards; 
    std::string autosaveOn, randomOn; 
}option; 

struct dummies{ 
    int usrInput; 
}dummy; 

inline void clearScreen(){ 
    #ifdef _WIN32 
     std::system("cls"); 
    #else 
     std::system ("clear"); 
    #endif 
} 

inline void saveUserStack(){ 

} 

void createNewStack(){ 

} 

void openExistingStack(){ 

} 

void exportExistingStack(){ 

} 

void showOptions(){ 
    clearScreen(); 
    switch(option.autosave){ 
     case 0: 
      option.autosaveOn = "off"; 
     case 1: 
      option.autosaveOn = "on"; 
    } 
    switch(option.randomisedCards){ 
     case 0: 
      option.randomOn = "off"; 
     case 1: 
      option.randomOn = "on"; 
    } 

    std::cout << "1. Autosave is currently " << option.autosaveOn; 
    std::cout << "\n2. Randomised cards are currently " << option.randomOn; 
    std::cout << "\n\nWhich option do you wish to change (please enter in the form of an integer): "; 
    std::cin >> dummy.usrInput; 
    switch(dummy.usrInput){ 
     case 1: 
      std::cout << "Are you sure you wish to change the option for autosave? 0 = no 1 = yes: "; 
      std::cin >> dummy.usrInput; 
      switch(dummy.usrInput){ 
       case 0: 
        clearScreen(); 
        showOptions(); 
       default: 
        switch(option.autosave){ 
         case 0: 
          option.autosave = 1; 
         default: 
          option.autosave = 0; 
        } 
        showOptions(); 
       } 
     case 2: 
      std::cout << "Are you sure you wish to change the option for randomised cards? 0 = no 1 = yes: "; 
      std::cin >> dummy.usrInput; 
      switch(dummy.usrInput){ 
       case 0: 
        showOptions(); 
       default: 
        switch(option.randomisedCards){ 
         case 0: 
          option.randomisedCards = 1; 
         case 1: 
          option.randomisedCards = 0; 
        } 
        showOptions(); 
      } 
     default: 
      showOptions(); 
    }  
    std::cout << "Autosave is currently " << option.autosaveOn; 
} 

出於某種原因,當我確認修改的選項,該值不發生變化,或至少輸出保持不變。

+2

歡迎來到StackOverflow!請注意,問題應該是獨立的(即:它們可能包含外部資源,但它們應該在沒有它們的情況下進行回答)。你可以嘗試提供[mcve]。 –

+2

請提取一個最簡單的例子,然後在這裏發佈內聯。現在,您的問題是無關緊要的,請閱讀發佈指南瞭解更多信息。此外,標籤不應該在標題中,這是因爲它們有獨立的位置。 –

+1

「當我驗證選項的更改」是什麼意思?你在說什麼選擇?你怎麼改變它們?你如何驗證這些變化? –

回答

0

看着你的問題,看起來你忘記了爲每個交換機案例添加一箇中斷。

請看看下面的MSDN文章使用開關 https://msdn.microsoft.com/en-us/library/66k51h7a.aspx

如果您正在使用VB.NET,你可以使用case語句是自成一體。 C需要中斷,否則即使交換機不匹配,邏輯也會轉入下一個語句。