if/ Else的Else组件不执行(c++)
Else component of if/else not executing (C++)
我有一个相对较大的程序,并不是所有的程序都与我的问题有关,但是这个特殊的部分难倒了我。下面是我的int main:
int main ()
{
int caseNumber ;
string clientName, clientEmail, subject, path, fileName, firstTime ;
//creates string for path of files created and where the Python project is stored (with the .exe)
path = _pgmptr ;
fileName = "EmailGenerator.exe" ;
fileName.length() ;
path.erase(path.length() - fileName.length()) ;
//checks first time use for customizing
cout << "Welcome to the Ticket Email Generator.nn"
<< "If this is your first time using this program,nplease enter Y to customize some personal details.n"
<< "If not, enter any other character.n" ;
cin >> firstTime ;
cin.ignore() ;
if (firstTime == "Y" || firstTime == "y")
{
//save sender email (defaults to morgan_wallace@cable.comcast.com - creator)
setSender (path) ;
//Verifies signature file is as desired (to be saved for the future)
char ready = 'n' ;
while (!(ready == 'y' || ready == 'Y'))
{
std::cout << "nPlease modify the signature.txt file located in " + path << endl
<< "Enter Y when done.n" ;
cin >> ready ;
cin.ignore() ;
}
}
//Email "To" field:
setTo (path) ;
//Email "Subject" field:
setSubject (path) ;
//Email "Body" field:
std::cout << "nPlease provide the following information for the body of the email:" << endl ;
//create standard time-based greeting at top of message
setToName (path) ;
//select message type & compose body of message
ofstream scriptfout (path + "script.txt") ;
std::cout << "nPlease select from case type menu:" << endl << endl ;
caseTypeMenu(scriptfout) ;
scriptfout.close() ;
//compose full body and add signature
setBody (path) ;
std::cout << "Would you like to review your email before sending? Y/N " ;
char reviewChar ;
cin >> reviewChar ;
cin.ignore() ;
if (reviewChar == 'y' || reviewChar == 'Y')
{
review (path) ;
}
else if (reviewChar == 'N' || reviewChar == 'n')
{
//run email sender (python program)
string pythonPathString = path + "EmailProj/EmailProj/EmailProj.py" ;
string pythonString = "C:/Python27/python.exe " + pythonPathString + " " + path ;
system(pythonString.c_str()) ;
//Exit program
string anything ;
std::cout << "Enter anything to exitn" ;
cin >> anything ;
cin.ignore() ;
}
else
{
cout << "Invalid entry review: " + reviewChar << endl ;
//Exit program
string anything ;
std::cout << "Enter anything to exitn" ;
cin >> anything ;
cin.ignore() ;
}
return 0 ;
}
我的特定问题我的最后一个if/else(其他所有内容都按预期执行):
std::cout << "Would you like to review your email before sending? Y/N " ;
char reviewChar ;
cin >> reviewChar ;
cin.ignore() ;
if (reviewChar == 'y' || reviewChar == 'Y')
{
review (path) ;
}
else if (reviewChar == 'N' || reviewChar == 'n')
{
//run email sender (python program)
string pythonPathString = path + "EmailProj/EmailProj/EmailProj.py" ;
string pythonString = "C:/Python27/python.exe " + pythonPathString + " " + path ;
system(pythonString.c_str()) ;
//Exit program
string anything ;
std::cout << "Enter anything to exitn" ;
cin >> anything ;
cin.ignore() ;
}
else
{
cout << "Invalid entry review: " + reviewChar << endl ;
//Exit program
string anything ;
std::cout << "Enter anything to exitn" ;
cin >> anything ;
cin.ignore() ;
}
如果您输入一个'h',或者不是Y、Y、N或N的字符,它不会打印错误脚本,其中包含不合适的答案。它打印我的一行代码(在代码的前面部分使用cout打印到屏幕上的代码),然后"输入任何内容以退出"并等待输入。如果你输入'g',它会直接跳过错误信息,输出"Enter anything to exit line",然后等待输入。
最后这个if/else的要点是允许用户查看他们的消息,然后决定是发送它,编辑它的一部分,还是完全忽略它。出于错误检查的目的,我希望处理不是Y、Y、N和N的输入,尽管理论上不应该有其他输入。
你的问题在这里:
cout << "Invalid entry review: " + reviewChar << endl ;
当你使用加号时,你实际上是在把一个整数的值加到一个指针上,而不是串接字符串。
试试这个:
cout << "Invalid entry review: " << reviewChar << endl ;
相关文章:
- 我的简单if-else语句是如何无法访问的代码
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- 如何删除peer if else分支中的冗长句子
- 我似乎对if/else的基本语句有问题:/
- 使用if-else将数字转换为单词
- c++ std::enable_if .... else?
- 如果 else 语句不断循环
- 为什么这个案例陈述需要一个"if else"而不仅仅是一个"if"?
- C++ If/Else 语句被跳过
- 为什么我的最后一个 ELSE 条件无法正确执行
- 如果条件不相关,我应该更喜欢两个 if 语句而不是 if-else 语句吗?
- 如何将 if else 语句重写为 switch 语句
- 在 C++ 中的 if-else if- else 语句期间更改变量
- 为什么我的 if else 语句不起作用并从数组中输出正确的索引?
- 我的 If Else 语句无法在向量 (C++) 中提供最大值
- 具有嵌套 if-else 的循环的时间复杂度
- 如果 else 语句使用大写和小写字母
- 如何改进一堆在已知值范围内评估变量的 else-if 条件?
- 为什么无论输入如何,所有 if-else 语句都会打印?
- 多个'if'语句,不带'else'