带有if语句的字符串 这个程序只是一个有趣的项目,我想我会用C++来练习,它只不过是计算一次公路旅行的价格。
所有这些编码都是使用CODE::BLOCKS键入和编译的,在启动程序时没有错误,if语句的反应就像在函数中列出的第一条if语句是真的一样,无论我输入什么。我确信编码是正确的,但我觉得我可能遗漏了什么带有if语句的字符串 这个程序只是一个有趣的项目,我想我会用C++来练习,它只不过是计算一次公路旅行的价格。,c++,string,if-statement,C++,String,If Statement,所有这些编码都是使用CODE::BLOCKS键入和编译的,在启动程序时没有错误,if语句的反应就像在函数中列出的第一条if语句是真的一样,无论我输入什么。我确信编码是正确的,但我觉得我可能遗漏了什么 main.cpp #include <iostream> #include "Answers.h" using namespace std; int main() { Answers roadTrip; roadTrip.intro(); return 0; } #include
main.cpp
#include <iostream>
#include "Answers.h"
using namespace std;
int main()
{
Answers roadTrip;
roadTrip.intro();
return 0;
}
#include "Answers.h"
#include <iostream>
using namespace std;
Answers::Answers(){
}
Answers::intro(){
cout << "Hello and welcome to the trip calculator, how may I be of service?" << endl;
cout << "As of now,I am only able to EXIT from this program, or calculate a simple road trip" << endl;
cout << "Which would you like to do, exit, or calculate?: ";
cin >> answer; cout << "\n" << endl;
if(answer=="Calculate"||"calculate"||"Road trip"||"road trip"){
dataInput();
}
if(answer=="Exit"||"exit"||"Escape"||"escape"||"Quit"||"quit"){
return 0;
}
}
void Answers::dataInput(){
cout << "How many miles to the gallon does your car get?: ";
cin >> mpg; cout << "\n";
cout << "What are the average gas prices along your trip?: ";
cin >> gasPrices; cout << "\n";
cout << "How far away in miles is your destination?: ";
cin >> distance; cout << "\n" << endl;
doubleChecking();
}
void Answers::doubleChecking(){
cout << "Alright, so if I'm not mistaken:" << endl;
cout << "Your car gets " << mpg << " miles per gallon" << endl;
cout << "The average price of gas along the trip is $" << gasPrices << endl;
cout << "And your destination is approximately " << distance << " miles away\n" << endl;
cout << "Is this information correct?: ";
cin >> answer; cout << "\n" << endl;
if(answer=="Yes"||"yes"||"Y"||"y"){
calculations();
}
if(answer=="No"||"no"||"N"||"n"){
incDataInput();
}
}
void Answers::incDataInput(){
cout << "Oh? Well that wont be a problem" << endl;
cout << "So what information was entered incorrectly?" << endl;
cout << "Was it MPG, gas prices, or distance?: ";
cin >> answer; cout << "\n" << endl;
if(answer=="MPG"||"mpg"||"Miles per gallon"||"miles per gallon"){
incDataMpg();
}
if(answer=="Gas"||"gas"||"Gas prices"||"gas prices"){
incDataGasPrices();
}
if(answer=="Distance"||"distance"){
incDataDistance();
}
}
void Answers::incDataMpg(){
cout << "So tell me again, how many miles to the gallon does your car get?: ";
cin >> mpg; cout << "\n";
cout << "Your car gets " << mpg << " miles per gallon, is that correct?: ";
cin >> answer; cout << "\n" << endl;
if(answer=="Yes"||"yes"||"Y"||"y"){
doubleChecking();
}
if(answer=="No"||"no"||"N"||"n"){
incDataMpg();
}
}
void Answers::incDataGasPrices(){
cout << "So tell me again, what's the average price of gas along your road trip?: ";
cin >> gasPrices; cout << "\n";
cout << "The average price of gas along the trip is $" << gasPrices << ", is that correct?: ";
cin >> answer; cout << "\n" << endl;
if(answer=="Yes"||"yes"||"Y"||"y"){
doubleChecking();
}
if(answer=="No"||"no"||"N"||"n"){
incDataGasPrices();
}
}
void Answers::incDataDistance(){
cout << "So tell me again, approximately how many miles away is your destination?: ";
cin >> distance; cout << "\n";
cout << "Your destination is approximately " << distance << " miles away, is that correct?: ";
cin >> answer; cout << "\n" << endl;
if(answer=="Yes"||"yes"||"Y"||"y"){
doubleChecking();
}
if(answer=="No"||"no"||"N"||"n"){
incDataDistance();
}
}
Answers::calculations(){
int extraMoney = distance*.05;
int total = (distance/mpg)*gasPrices;
cout << "So according to my calculations, in order for your car to travel the " << distance << " miles needed" << endl;
cout << "It'd be smart to have at least $" << total << "for your overall road trip, but if I were you, I'd consider" << endl;
cout << "making it $" << total+extraMoney << "Leaving you " << extraMoney << " extra dollars to spend on food an necessities for the trip" << endl;
cout << "I think it should cover " << distance << " miles"; cout << "\n" << endl;
goodbye();
}
Answers::goodbye(){
cout << "Well, hasn't this been fun? We should do it again sometime. I hope I was able to help you out" << endl;
cout << "That being said, would you like me exit this program?: ";
cin >> answer;
if("Yes"||"yes"||"Y"||"y"){
return 0;
}
if("No"||"no"||"N"||"n"){
finalGoodbye();
}
}
Answers::finalGoodbye(){
cout << "Listen, it's nice that you wanna keep me around and all, but literally all I can do is calculate road trips" << endl;
cout << "... and quit out of myself" << endl;
cout << "That being said, would you like me exit this program?: ";
cin >> answer;
if("Yes"||"yes"||"Y"||"y"){
return 0;
}
if("No"||"no"||"N"||"n"){
finalGoodbye();
}
}
#包括
#包括“答案.h”
使用名称空间std;
int main()
{
回答路途问题;
roadTrip.intro();
返回0;
}
答案。h
#ifndef ANSWERS_H
#define ANSWERS_H
#include <iostream>
using namespace std;
class Answers
{
public:
Answers();
int intro();
void dataInput();
void doubleChecking();
void incDataInput();
void incDataMpg();
void incDataGasPrices();
void incDataDistance();
int goodbye();
int finalGoodbye();
int calculations();
int mpg;
float gasPrices;
int distance;
string answer;
string incValue;
protected:
private:
};
#endif // ANSWERS_H
#如果没有答案#
#定义答案
#包括
使用名称空间std;
课堂答案
{
公众:
答案();
int intro();
void dataInput();
void doubleChecking();
void incDataInput();
void incDataMpg();
无效价格();
void incDataDistance();
int再见();
int finalGoodbye();
int计算();
int mpg;
浮米;
整数距离;
字符串回答;
字符串值;
受保护的:
私人:
};
#endif//ANSWERS\u H
答案。cpp
#include <iostream>
#include "Answers.h"
using namespace std;
int main()
{
Answers roadTrip;
roadTrip.intro();
return 0;
}
#include "Answers.h"
#include <iostream>
using namespace std;
Answers::Answers(){
}
Answers::intro(){
cout << "Hello and welcome to the trip calculator, how may I be of service?" << endl;
cout << "As of now,I am only able to EXIT from this program, or calculate a simple road trip" << endl;
cout << "Which would you like to do, exit, or calculate?: ";
cin >> answer; cout << "\n" << endl;
if(answer=="Calculate"||"calculate"||"Road trip"||"road trip"){
dataInput();
}
if(answer=="Exit"||"exit"||"Escape"||"escape"||"Quit"||"quit"){
return 0;
}
}
void Answers::dataInput(){
cout << "How many miles to the gallon does your car get?: ";
cin >> mpg; cout << "\n";
cout << "What are the average gas prices along your trip?: ";
cin >> gasPrices; cout << "\n";
cout << "How far away in miles is your destination?: ";
cin >> distance; cout << "\n" << endl;
doubleChecking();
}
void Answers::doubleChecking(){
cout << "Alright, so if I'm not mistaken:" << endl;
cout << "Your car gets " << mpg << " miles per gallon" << endl;
cout << "The average price of gas along the trip is $" << gasPrices << endl;
cout << "And your destination is approximately " << distance << " miles away\n" << endl;
cout << "Is this information correct?: ";
cin >> answer; cout << "\n" << endl;
if(answer=="Yes"||"yes"||"Y"||"y"){
calculations();
}
if(answer=="No"||"no"||"N"||"n"){
incDataInput();
}
}
void Answers::incDataInput(){
cout << "Oh? Well that wont be a problem" << endl;
cout << "So what information was entered incorrectly?" << endl;
cout << "Was it MPG, gas prices, or distance?: ";
cin >> answer; cout << "\n" << endl;
if(answer=="MPG"||"mpg"||"Miles per gallon"||"miles per gallon"){
incDataMpg();
}
if(answer=="Gas"||"gas"||"Gas prices"||"gas prices"){
incDataGasPrices();
}
if(answer=="Distance"||"distance"){
incDataDistance();
}
}
void Answers::incDataMpg(){
cout << "So tell me again, how many miles to the gallon does your car get?: ";
cin >> mpg; cout << "\n";
cout << "Your car gets " << mpg << " miles per gallon, is that correct?: ";
cin >> answer; cout << "\n" << endl;
if(answer=="Yes"||"yes"||"Y"||"y"){
doubleChecking();
}
if(answer=="No"||"no"||"N"||"n"){
incDataMpg();
}
}
void Answers::incDataGasPrices(){
cout << "So tell me again, what's the average price of gas along your road trip?: ";
cin >> gasPrices; cout << "\n";
cout << "The average price of gas along the trip is $" << gasPrices << ", is that correct?: ";
cin >> answer; cout << "\n" << endl;
if(answer=="Yes"||"yes"||"Y"||"y"){
doubleChecking();
}
if(answer=="No"||"no"||"N"||"n"){
incDataGasPrices();
}
}
void Answers::incDataDistance(){
cout << "So tell me again, approximately how many miles away is your destination?: ";
cin >> distance; cout << "\n";
cout << "Your destination is approximately " << distance << " miles away, is that correct?: ";
cin >> answer; cout << "\n" << endl;
if(answer=="Yes"||"yes"||"Y"||"y"){
doubleChecking();
}
if(answer=="No"||"no"||"N"||"n"){
incDataDistance();
}
}
Answers::calculations(){
int extraMoney = distance*.05;
int total = (distance/mpg)*gasPrices;
cout << "So according to my calculations, in order for your car to travel the " << distance << " miles needed" << endl;
cout << "It'd be smart to have at least $" << total << "for your overall road trip, but if I were you, I'd consider" << endl;
cout << "making it $" << total+extraMoney << "Leaving you " << extraMoney << " extra dollars to spend on food an necessities for the trip" << endl;
cout << "I think it should cover " << distance << " miles"; cout << "\n" << endl;
goodbye();
}
Answers::goodbye(){
cout << "Well, hasn't this been fun? We should do it again sometime. I hope I was able to help you out" << endl;
cout << "That being said, would you like me exit this program?: ";
cin >> answer;
if("Yes"||"yes"||"Y"||"y"){
return 0;
}
if("No"||"no"||"N"||"n"){
finalGoodbye();
}
}
Answers::finalGoodbye(){
cout << "Listen, it's nice that you wanna keep me around and all, but literally all I can do is calculate road trips" << endl;
cout << "... and quit out of myself" << endl;
cout << "That being said, would you like me exit this program?: ";
cin >> answer;
if("Yes"||"yes"||"Y"||"y"){
return 0;
}
if("No"||"no"||"N"||"n"){
finalGoodbye();
}
}
#包括“Answers.h”
#包括
使用名称空间std;
答案::答案(){
}
答案::简介(){
不能必须这样
如果(回答==“每加仑汽油”|回答==“每加仑汽油”|回答==“每加仑英里数”|回答==“每加仑英里数”)
对于所有其他的ifs也是如此。否则,您将检查指向“mpg”的指针和所有其他字符串指针是否不等于0。因此,该条件的计算结果始终为true。answer==“Yes”| |“Yes”|“Y”|“Y”
不会像您认为的那样。基本上,它将始终返回true,因为在“or”中您刚刚输入的const char*
值总是被认为是真的
为了使此逻辑正常工作,您必须将其替换为answer==“Yes”| | answer==“Yes”| answer==“Y”| | answer==“Y”
。更好的是,toupper(answer.c|str()[0])=='Y'
。这同样适用于其他字符串比较if
语句。类似于something==somethingelse | | | somethingthird | | |..
的表达式不会执行您认为它会执行的操作,因为您的比较在编译器看来是这样的
(answer=="Calculate")||"calculate"||"Road trip"||"road trip"
换句话说,它将answer
与字符串“Calculate”
进行比较,并将逻辑or运算符的结果与文字字符串常量一起使用,文字字符串常量将是非空指针,因此始终为true,这意味着比较将始终为true
相反,你需要这样做
answer=="Calculate" || answer=="calculate" || answer=="Road trip" || answer=="road trip"
if语句的反应就像第一个if语句中列出了一个函数[sic;例如,这个函数:
]无论我输入什么都是正确的。我确信编码是正确的,但我觉得我可能遗漏了什么
那么是哪一个呢?代码正确与否
事实上,代码与您的意图不符。您似乎想要:
if(answer=="Calculate"||answer=="calculate"||answer=="Road trip"||answer=="road trip"){
您最初的问题是answer==“Calculate”
是否为真(可能是真的,也可能不是真的),或者“Calculate”
是否为真(始终是真的),或者两个其他条件也是真的。使代码编译。
首先,虽然代码编译的G+编译器没有选项,但它没有用VisualC++来编译,当使用选项<代码> -F NO-MS-ExtExt/Cuth>禁用微软代码中使用的语言扩展:
时,它不使用G++编译。
[C:\my\forums\so\169]
> doskey /macros
g++=g++ -std=c++14 -pedantic-errors -Wall -Wextra -fexec-charset=cp1252 $*
[C:\my\forums\so\169]
> g++ main.cpp Answers.cpp -fno-ms-extensions
Answers.cpp:12:16: error: ISO C++ forbids declaration of 'intro' with no type [-fpermissive]
Answers::intro() {
^
Answers.cpp:137:23: error: ISO C++ forbids declaration of 'calculations' with no type [-fpermissive]
Answers::calculations() {
^
Answers.cpp: In member function 'int Answers::calculations()':
Answers.cpp:147:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
Answers.cpp: At global scope:
Answers.cpp:149:18: error: ISO C++ forbids declaration of 'goodbye' with no type [-fpermissive]
Answers::goodbye() {
^
Answers.cpp:163:23: error: ISO C++ forbids declaration of 'finalGoodbye' with no type [-fpermissive]
Answers::finalGoodbye() {
^
Answers.cpp: In member function 'int Answers::intro()':
Answers.cpp:26:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
[C:\my\forums\so\169]
> _
…始终执行dataInput()
,而不考虑answer
的值
是,C++操作符被设计成支持这样的表达式:
if( x1 != x2 || y1 != y2 ) // The points are different
if( contains( answer, Strings{ "Calculate", "calculate", "Road trip", "road trip" } ) )
{
// Whatever.
}
if( answer=="Calculate" ||
answer == "calculate" ||
answer == "Road trip" ||
answer == "road trip"
)
{
// Whatever.
}
< > >在<<强>布尔运算符< /St> > < <代码> > <代码>之前,对关系运算符如<代码>!>代码>=代码>和代码>进行评估。即关系运算符具有更高的<强>优先< /强>。您可以在所有地方找到包括C++运算符的优先表,包括。
与Python不同,运算符链没有特殊的规则,因此answer
检查代码被解释为
if( ((((answer=="Calculate") || "calculate") || "Road trip") || "road trip") ) {
dataInput();
}
这是因为在任何可能的上下文中,一个简单的字符串文字衰减为指向第一个char
的指针,而在这里,这是可能的,因为一个指针进一步衰减(隐式转换)为布尔false
和true
空指针产生false
,任何其他指针产生true
由于文本中的指针不是空指针,因此上述内容相当于
if( ((((answer=="Calculate") || true) || true) || true) ) {
dataInput();
}
现在您可以看到,这是如何完全独立于answer
的值的
如何表达“s是这些字符串中的一个吗…?”。
意识到这是为了检查给定字符串是否在给定字符串集中,可以编写一个名为
包含的函数,并像这样使用它:
if( x1 != x2 || y1 != y2 ) // The points are different
if( contains( answer, Strings{ "Calculate", "calculate", "Road trip", "road trip" } ) )
{
// Whatever.
}
if( answer=="Calculate" ||
answer == "calculate" ||
answer == "Road trip" ||
answer == "road trip"
)
{
// Whatever.
}
定义这样一个函数很简单:
#include <string>
#include <unordered_set>
using namespace std;
using Strings = unordered_set<string>;
auto contains( string const& item, Strings const& set )
-> bool
{ return (set.count( item ) > 0); }
但这很尴尬,冗长,而且不可伸缩。你必须写:如果(答案==“计算”| |答案==“计算”| |答案==“公路旅行”)…)否则会有一个比较(答案==“计算”)还有很多const char*值永远不会计算为false…谢谢你的反馈,我很感激!我要哭了…我不敢相信这么简单,非常感谢你,你是个救命恩人。非常感谢你的反馈,你让它更容易理解!感谢你的反馈!感谢你的反馈!我感谢你的反馈!我感谢inpuT