dimanche 26 juin 2016

Strings with if statements


This program was just a fun project I thought I'd work on to practice with C++, all it does is calculate the price of a road trip.

All of this coding was typed and compiled using CODE::BLOCKS, it finished with no errors yet when launching the program, the if statements react as if the first if statement listed withing a function is true no matter what I enter. I'm sure the coding is correct yet I feel I may be missing something.


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 awayn" << 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();
}
}

Any help would be GREATLY appreciated, I've had the issue before yet I'm still not sure how to fix it.


Aucun commentaire:

Enregistrer un commentaire