C++ 如果字符串不等于常量字符,则中断while循环

C++ 如果字符串不等于常量字符,则中断while循环,c++,C++,我的代码中出现了一个打破while循环的问题。如果用户输入完项目,我希望他们按q退出。这不起作用,q由用户输入,循环没有中断。如果还有更好的方法,那就太好了 #include <stdio.h> #include "CashRegister.h" #include <stdlib.h> #include <iostream> #include <string> using namespace std; CashRegister::CashRegi

我的代码中出现了一个打破while循环的问题。如果用户输入完项目,我希望他们按q退出。这不起作用,q由用户输入,循环没有中断。如果还有更好的方法,那就太好了

#include <stdio.h>
#include "CashRegister.h"
#include <stdlib.h>
#include <iostream>
#include <string>
using namespace std;

CashRegister::CashRegister(void)
{
}

CashRegister::~CashRegister(void)
{
}

void CashRegister::addItem(void)
{
string temp;
int k = 0;
int exit = 0;
CashRegister item[10];

for(int i = 0; i < 10; ++ i)
{
    item[i].price = 0.00;
}

cout << "Enter price of items. Up to 10 maximum\n" << endl;

while(item[9].price != 0.00 || temp != "q")
{
    cout << "Enter price of item number " << k + 1 << "\n" << endl;
    cin >> temp;
    cout << temp << endl;
    double tempPrice = (double)atof(temp.c_str());
    item[k].price = tempPrice;
    cout << "Price of item number " << k + 1 << " is $" << item[k].price << endl;
    ++k;
}
#包括
#包括“cash register.h”
#包括
#包括
#包括
使用名称空间std;
收银机::收银机(无效)
{
}
收银机::~收银机(无效)
{
}
作废收银机::附加项(作废)
{
字符串温度;
int k=0;
int exit=0;
收银机项目[10];
对于(int i=0;i<10;++i)
{
第[i]项,价格=0.00;
}
库特

考虑这句话:“当价格不为0,<强>或Top不为Q”时,“现在考虑为停止循环所必须发生的事情。

< P>您的while循环需要读取:

while(item[9].price != 0.00 && temp != "q")

当这两个条件都为真时,循环需要继续,因此您需要说
&&
而不是
|

您应该使用它来中断循环:

while(item[9].price != 0.00 && temp != "q")
{
  // ur stuff
}

循环将迭代,因为项[9]。price将包含0.00以外的值。对不起,错误单击,我的循环现在似乎提前结束。嗯,是
项[9].price
ever
0.00
?这似乎是唯一可能发生的原因。当我执行if语句检查它是否为0.00时,结果显示为0.00。我只是将代码更改为
while(项[9]。price
while(item[9].price != 0.00 && temp != "q")
{
  // ur stuff
}