C ++忽略if语句条件

时间:2013-08-13 21:09:18

标签: c++ if-statement ignore

所以我只是为了练习编写这段代码,而且我遇到了一个问题,即我连续有多个if语句,但即使用户输入不满足任何第一个if语句的条件,它也会打印出来来自第一个if语句的数据。我遇到问题的部分位于代码的底部,就在“返回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;
}

我知道这是半长的,但我感谢您的任何建议!对不起,如果它很乱,我是编码的新手。

干杯

2 个答案:

答案 0 :(得分:3)

在C和C ++中,|| operator是二元运算符。非零值是真的。

你的表达:

   getline(cin,set_type) == "pre-workout" || "preworkout" || "pre workout" || "preworkouts" || "pre-workouts" || "pre workouts")

首先测试getline()返回的istream是否等于字符串。不是。然后呢 测试下一个表达式“preworkout” - 一个文字字符串,其值为非零整数地址,被认为是真的。因此,执行if语句的主体。

请改为尝试:

 getline(cin,set_type);
 if( set_type == "pre-workout" 
     || set_type == "preworkout" 
     || set_type == "pre workout" 
     || set_type == "preworkouts" 
     || set_type == "pre-workouts" 
     || set_type == "pre workouts")

答案 1 :(得分:1)

很多问题。

getline返回流而不是输入字符串。所以,你需要一个像

这样的陈述
getline(cin,set_type);

应该在set_type中输入您的输入。然后,您的if声明将是:

if ( set_type == "pre-workout" || set_type == "preworkout" || set_type == "pre workout" || set_type == "preworkouts" || set_type == "pre-workouts" || set_type == "pre workouts")