布尔值不变?[C++]
Boolean values not changing? [C++]
main:https://github.com/MJGHD/Stacks./blob/master/main.cpp
//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!nn";
std::cout << "1. Create new stackn";
std::cout << "2. Open existing stackn";
std::cout << "3. Export existing stackn";
std::cout << "4. Optionsnn";
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 << "nnWhich 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;
}
出于某种原因,当我验证选项的更改时,值不会更改,或者至少输出保持不变。
看看你的问题,你似乎忘记了在每个开关案例中添加一个中断。
请看下面MSDN上关于使用switch的文章https://msdn.microsoft.com/en-us/library/66k51h7a.aspx
如果你习惯了VB.NET,你可能会习惯于语句的大小写是自包含的。C需要中断,否则即使开关不匹配,逻辑也会进入下一个语句。
相关文章:
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 变量定义到C++布尔值转换
- 如何确保在使用基于布尔值的两个方法之一调用方法时避免分支预测错误
- 重载更少,则运算符返回相反的布尔值
- 将此布尔值传递给此函数的最有效方法是什么?
- 如何设置 c++ 类的布尔值?
- 使用 MAKEWORD / MAKEWPARAM 使用布尔值而不是布尔值
- 将 10 个线程与原子布尔值同步
- 创建类似于布尔值的变量类型
- 布尔值向量的基于范围 for 循环
- 零点和布尔值之间的比较
- 简化对两个布尔值的 4 个 if/else 检查
- 无法创建带有布尔值和矢量的地图
- 对于完成布尔值设置为 true 后未停止的循环
- fstream / ifstream / ofstream 对象如何转换为布尔值
- C++:将值赋值给原始数据类型(例如布尔值)是原子操作吗?
- 为什么布尔值不能比作最后一点?
- 如何使用返回布尔值的函数?
- 将uintptr_t转换为布尔值会使 SSO 基准速度减慢数倍
- 布尔值始终评估为真