所以我编写这段代码只是为了练习,我遇到了一个问题,我连续有几个 if 语句,但即使用户输入不满足第一个 if 语句的任何条件,它也会从第一个 if 语句。我遇到问题的部分位于代码的底部,就在“return 0”部分的正上方。
// ConsoleApplication4.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <fstream>
#include <istream>
using namespace std;
class all_supp{
private:
string name, type;
public:
all_supp();
all_supp(string y);
void enterType(string x);
string getType();
void enterName(string y);
string getName();
};
//Define Variables
all_supp::all_supp()
{
name = "unknown";
type = "unknown";
}
all_supp::all_supp(string y)
{
name = y;
type = "unknown";
}
void all_supp::enterType(string x)
{
type = x;
}
string all_supp::getType()
{
return type;
}
void all_supp::enterName(string y)
{
name = y;
}
string all_supp::getName()
{
return name;
}
// MusclePharm Assault structure
struct preworkout_assault{
static int creatine_hcl, creatine_mono;
static double cost;
};
int preworkout_assault::creatine_hcl = 250;
int preworkout_assault::creatine_mono = 1000;
double preworkout_assault::cost = 29.99;
// C4 Extreme structure
struct preworkout_c4{
static int creatine_hcl, creatine_mono, creatine_nitrate, caffeine;
static double cost;
};
int preworkout_c4::creatine_hcl = 0;
int preworkout_c4::creatine_mono = 0;
int preworkout_c4::caffeine = 135;
int preworkout_c4::creatine_nitrate = 1000;
double preworkout_c4::cost = 29.99;
// BSN N.).-Xplode structure
struct preworkout_bsn{
static int creatine_hcl, creatine_mono, creatine_nitrate, caffeine;
static double cost;
};
int preworkout_bsn::creatine_hcl = 0;
int preworkout_bsn::creatine_mono = 0;
int preworkout_bsn::caffeine = 135;
int preworkout_bsn::creatine_nitrate = 1000;
double preworkout_bsn::cost = 29.99;
// How the structures are accessed within main
preworkout_assault assault_data;
preworkout_c4 c4_data;
preworkout_bsn bsn_data;
int _tmain(int argc, _TCHAR* argv[])
{
//Access general class
all_supp all;
//Variables
string set_type, set_name;
cout << "Welcome to the bodybuilding supplement recommendation application!" << endl << endl;
cout << "The categories of supplements that this application includes are:" << endl << "- Pre-workouts" << endl << "- Intra-workouts" << endl << "- Fat-loss boosters" << endl << "- Recovery boosters" << endl << endl;
cout << "White kind of supplement are you looking for? ";
// Choose the type of supplement
cin >> set_type;
all.enterType(set_type);
if(getline(cin,set_type) == "pre-workout" || "preworkout" || "pre workout" || "preworkouts" || "pre-workouts" || "pre workouts")
{
cout << "Okay, so you're looking for a pre-workout!" << endl;
cout << "These are the top five pre-workout products currently on the market:" << endl << endl;
cout << "- MusclePharm Assault\n- Cellucor C4 Extreme\n- BSN N.O.-Xplode 2.0\n- Pro Supps MR. HYDE\n- Driven Sports CRAZE" << endl << endl;
cout << "Which of the listed pre-workouts would you like to learn more about? Type \"exit\"if you would like to switch the type of supplement you are looking for.";
cin >> set_name;
all.enterName(set_name);
if(all.getName() == "musclepharm assault", "assault", "musclepharm", "muscle", "pharm", "assaullt", "asault")
{
//Print MusclePharm Assault data from struct here
cout << endl << endl << "Awesome! Here's some information about MusclePharm Assault:" << endl << "- Cost per 30 servings: $" << assault_data.cost << endl << "- Creatine monohydrate content per serving: " << assault_data.creatine_mono << "mg" << endl << "- Creatine HCl per serving: " << assault_data.creatine_hcl << "mg" << endl;
}
else if(all.getName() == "Cellucor C4 Extreme", "cellucor", "c4", "extreme", "c", "4")
{
//Print C4 data from struct here
cout << endl << endl << "Awesome! Here's some information about Cellucor C4 Extreme:" << endl << "- Cost per 30 servings: $" << c4_data.cost << endl << "- Creatine monohydrate content per serving: " << c4_data.creatine_mono << "mg" << endl << "- Creatine HCl per serving: " << c4_data.creatine_hcl << "mg" << endl << "- Creatine Nitrate per serving: " << c4_data.creatine_nitrate << "mg" << endl << "- Caffeine per serving: " << c4_data.caffeine << endl << endl;
}
else if(all.getName() == "BSN N.O.-Xplode 2.0", "bsn", "no", "bsn no", "n.o.", "n.o")
{
//Print C4 data from struct here
cout << endl << endl << "Awesome! Here's some information about BSN N.O.-Xplode 2.0:" << endl << "- Cost per 30 servings: $" << bsn_data.cost << endl << "- Creatine monohydrate content per serving: " << bsn_data.creatine_mono << "mg" << endl << "- Creatine HCl per serving: " << bsn_data.creatine_hcl << "mg" << endl << "- Creatine Nitrate per serving: " << bsn_data.creatine_nitrate << "mg" << endl << "- Caffeine per serving: " << bsn_data.caffeine << endl << endl;
}
}
cout << endl << endl;
return 0;
}