这个程序只是一个有趣的项目,我想我会用 C++ 练习,它所做的只是计算公路旅行的价格。
所有这些编码都是使用 CODE::BLOCKS 键入和编译的,它在启动程序时没有错误地完成,无论我输入什么,if 语句的 react 都好像第一个带函数的 if 语句为真。我确信编码是正确的,但我觉得我可能遗漏了一些东西。
main.cpp
#include <iostream>
#include "Answers.h"
using namespace std;
int main()
{
Answers roadTrip;
roadTrip.intro();
return 0;
}
Answers.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
Answers.cpp
#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();
}
}
非常感谢任何帮助,我以前遇到过这个问题,但我仍然不确定如何解决它。
最佳答案
类似something == somethingelse || 的表达式第三点 || ...
没有按照您的想法去做,因为 operator precedence你的比较对编译器来说是这样的
(answer=="Calculate")||"calculate"||"Road trip"||"road trip"
换句话说,它将 answer
与字符串 "Calculate"
进行比较,并将结果用于逻辑或运算符与文字字符串常量和文字字符串常量将是一个非空指针,因此始终为真,这意味着比较将始终为真。
你反而需要做例如
answer=="Calculate" || answer=="calculate" || answer=="Road trip" || answer=="road trip"
关于c++ - 带有 if 语句的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38041902/