C ++代码,bools和循环

时间:2010-11-10 00:21:42

标签: c++ boolean

我是C ++编程的新手,并且在一周内没有做任何事情,所以到目前为止,我一直在搞乱我所知道的事情,看看我是否还要重新审视一下。

然而,我遇到了bools的问题(之前没有真正使用过它们)。

来源:

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
   signed long int x;
   unsigned short int y = 12345;
   bool POSX;
   bool yes;

   cin >> x;

   if (x >= 0)
   {
      POSX = true;
   }//end of if for bool
   else
   {
      POSX = false;
   }

   cout << "This is the current value of X:  " << x << endl;
   if(x < y)
   {
      cout << "x is less than the integer y \n \n";
   }//end of if
   else
   {
      cout << "y is greater than the integer x \n \n";
   }//end of else
   cout << "The current value of Y is: " << y << endl << endl << endl;
   cout << "Is X positive?: " << POSX << endl << endl;

   cout << "How much more would X need to be to surpass y? The answer is: " << y - x << endl;
   if(x > y)
   {
      cout << "well, actually, x is greater than y by: " << y - x << " so you would need to add that to get to the value of x" <<endl <<endl;
   }//end of if

   cout << "Do you like cookies? Enter below. . ." <<endl;
   cin >> yes;

   if(yes = "yes") // should this be if(yes = 1)?
   {
      cout << "I do too! But only when they are soft and gooey!";
   } //end of if for bool yes
   else
   {
      cout << "Well, join the dark side, and you may be persuaded by the power of cookies and the power of the dark forces!";
   }//end of else for bool yes
   char f;
   cin >> f;
   return 0;
 } //end of main

我遇到的问题是当我尝试编译时,程序退出之前我才能看到cookie问题的结果[所以我必须在编译器中放置一个断点],其次,当我可以看到答案,总是会得到肯定的答案,而不是其他任何东西。 所以,如果我把no作为输入,它仍然输出if为bool是真的。我不确定我是否在最后一个语句中正确定义了if子句。 有人可以帮忙吗?

5 个答案:

答案 0 :(得分:4)

好的,有两件事。你的主要问题是:

if(yes = "yes")

'yes'将definend定义为bool类型,即它可以保存值“true”或“false”。您试图比较比较(实际上是因为仅使用一个=而不是==,这是检查相等性的方式),一个布尔值为字符串“是”。嗯,这没有任何意义。它应该是:

if( yes )

就是这样。 'yes'已经是一个布尔值,if语句中的表达式不再需要。

其次,这样的结构是多余的和不必要的:

 if (x >= 0)
  {
  POSX = true;
  }//end of if for bool
 else
  {
  POSX = false;
  }

您正在检查布尔值,然后分配一个。就这样做一行:

POSX = (x >=0 );

此外,您通常不会对局部变量使用全部大写。

还有一件事;你正在输入字符串数据(“no”或“yes”),cin期待一个int。我建议您花一些时间了解哪些数据类型。

答案 1 :(得分:2)

  

我遇到的问题是当我尝试编译时,程序退出之前我才能看到cookie问题的结果

这是因为当您将“否”作为值时,您使cin无效。 operator>>(std::istream&, bool&)假定数字输入。值不等于零将被解释为true,等于零的值将被解释为false

如果您提供无法解析的输入,则将在流上设置数字badbit。尝试在处于此失败状态时使用流将导致读取垃圾(或者更确切地说,未定义的行为),并且流中的get指针没有进展。

答案 2 :(得分:1)

将yes更改为字符串(#include <string>),然后将其比较如下:

if (yes == "yes")

答案 3 :(得分:1)

这里有一些问题,但我认为让你沮丧的是cin >> yes;在C ++中,false0。因此,对于任何非零输入值,这可能会返回true。更可靠的方法是要求并评估其他内容,例如字符输入:

cout << "Do you like cheese? (y/n) ";
char c;
cin >> c;

if ( c == 'y' || c == 'Y' )
   do whatever;

此外,在测试条件时,请务必使用“double-equals”,condition == true。更好的是,采用速记:

  • (condition)表示(condition == true)
  • (! condition)表示(condition == false)

希望能为你提供一个正确的方向。

答案 4 :(得分:0)

这是编辑过的源代码,我做了更改,所以研究它并与原始代码进行比较。 * E *

#include <iostream>
#include <string>
#include <iomanip> //headerfile for boolalpha
using namespace std;

int main()
{
    string answer;
   signed long int x;
   unsigned short int y = 12345;
   bool POSX;
   bool yes;
   cout<<"Enter any value: ";
   cin >> x;

   if (x >= 0)
   {
       cout << boolalpha; // print bools as true or false
      POSX = true;
   }//end of if for bool
   else
   {
    cout << boolalpha; // print bools as true or false
      POSX = false;
   }

   cout << "This is the current value of X:  " << x << endl;
   if(x < y)
   {
      cout << "x is less than the integer y \n \n";
   }//end of if
   else
   {
      cout << "y is greater than the integer x \n \n";
   }//end of else
   cout << "The current value of Y is: " << y << endl << endl << endl;
   cout << "Is X positive?: " << POSX << endl << endl;

   cout << "How much more would X need to be to surpass y? The answer is: " << y - x << endl;
   if(x > y)
   {
      cout << "well, actually, x is greater than y by: " << y - x << " so you would need to add that to get to the value of x" <<endl <<endl;
   }//end of if

   cout << "Do you like cookies? Yes/No. . ." <<endl;
   cin >> answer; //this is a string 

   if(answer =="yes") // it can be a simple if statement
   {
      cout << "I do too! But only when they are soft and gooey!";
   } //end of simple if 
   else
   {
      cout << "Well, join the dark side, and you may be persuaded by the power of cookies and the power of the dark forces!";
   }//end of else for simple if

   return 0;
 } //end of main`