C++ 在开关内部的函数之后不执行代码
下面的代码在“Y”或“Y”的情况下调用函数randStats(),在函数返回后,我需要它在开关情况下执行函数调用下面的代码,但它没有这样做。这可能是一个编译器问题,因为我以前遇到过代码块问题,新代码似乎被完全忽略,而其他代码则没有 下面是由下面的开关调用的函数本身的粘贴: 注意:据我所知,这不是编码问题,而是完全忽略代码的问题C++ 在开关内部的函数之后不执行代码,c++,switch-statement,codeblocks,C++,Switch Statement,Codeblocks,下面的代码在“Y”或“Y”的情况下调用函数randStats(),在函数返回后,我需要它在开关情况下执行函数调用下面的代码,但它没有这样做。这可能是一个编译器问题,因为我以前遇到过代码块问题,新代码似乎被完全忽略,而其他代码则没有 下面是由下面的开关调用的函数本身的粘贴: 注意:据我所知,这不是编码问题,而是完全忽略代码的问题 // newchar.cpp #include <iostream> #include "player.h" #include "randstats.h"
// newchar.cpp
#include <iostream>
#include "player.h"
#include "randstats.h"
#include "newchar.h"
int new_character()
{
std::cout << "\n\nCreating new character...";
std::cout << "\n\nClaim an alias for your character: ";
std::cin >> player.alias;
char x;
std::cout << "You have chosen " << player.alias << "\n";
std::cout << "Is this correct? [y/n]: ";
std::cin >> x;
switch(x)
{
case 'y':
case 'Y':
std::cout << "\nInserting " << player.alias << " into this hapless world of strife...";
std::cout << "\n\nRandomizing stats...\n" << std::endl;
randStats(); // randomize stats
//print new character information
std::cout << "Alias: " << player.alias << "Level: " << player.current_level << std::endl;
std::cout << "Stats: " << player.str << "STR " << player.dex << "DEX " << player.con << "CON " << player.intel << "INT " << player.wis << "WIS " << player.cha << "CHA " << std::endl;
break;
case 'n':
case 'N':
std::cout << "Aborting...\n";
break;
default:
break;
}
//update the database
//do it
return 0;
}
//newchar.cpp
#包括
#包括“player.h”
#包括“randstats.h”
#包括“newchar.h”
int新字符()
{
std::cout player.alias;
字符x;
randstats.cpp中的第49行是
// remaining_points - x;
应取消注释并将其固定为:
remaining_points -= x;
用等号
否则,它将永远循环。您确定randStats()吗
正在返回?这似乎是唯一的解释。在我从ideone链接的randstats.cpp文件中,请参见函数std::toupper
和std::tolower
,这样您可以转换为一种情况,并进行两次比较,而不是4次。您是否尝试过使用调试器单步执行代码,以查看randstats()之后的情况
?如果您对调试器感到不舒服,请在开关后放置一个输出命令,确保以它从未“永远循环”结束对我来说。啊哈。这似乎是因为我从开关调用了函数,然后从被调用函数内部的开关调用了另一个函数,它永远不会返回到原始开关并在它之后执行任何代码……我似乎被某个人误导了。我从被调用函数内部的开关中删除了函数调用现在它只执行所有函数调用下面的代码。如果这有意义的话。请注意,它不会返回到开关并执行函数调用下面的任何操作。