C ++很少有初学者错误

时间:2012-04-18 20:57:03

标签: c++

调试不是很好但是我遇到了一些错误。一些预期'('')'和';' 另外'else'没有先前的'if',与'operator>>'不匹配在cout中

我知道这很容易,但仍然试图让我的脚踏上门。谢谢:))

#include <iostream>
#include <cstdlib>
using namespace std;

int main()   // guess number game
{
    int x;
    cout >> "Please enter a number\n";
    getline(cin x);
    int y = rand();

    while x != y
    {

        if x < y;
           cout >> "Go higher";
        else;
           cout >> "Go lower";
    }


}

5 个答案:

答案 0 :(得分:5)

cout >> "Please enter a number\n";

这是错误的,std::ostreams仅提供operator<<来插入格式化数据。请改用cout << "Please enter a number\n";

getline(cin x);

首先,您错过了,,因为getline需要两个或三个参数。但由于xinteger而不是std::string,因此仍然存在错误。想一想 - 你能在一个整数内存储一个文本行吗?请改用cin >> x

int y = rand();

虽然这似乎没有错,但是存在逻辑错误。 rand()是伪随机数生成器。它使用种子作为起始值和某种算法(a*m + b)。因此,您必须指定一个起始值,也称为种子。您可以使用srand()指定此项。相同的种子将产生相同的数字顺序,因此请使用srand(time(0))

之类的内容
while x != y
if x < y;

使用括号。并删除额外的;。程序中的杂散分号;类似于空表达式。

编辑:工作代码:

#include <iostream>
#include <cstdlib>
#include <ctime>

int main(){
    int x;
    int y;
    srand(time(0));
    y = rand();
    std::cout << "Please enter a number: ";
    do{
        if(std::cin >> x){
            if(x < y)
                std::cout << "Go higher: ";
            if(x > y)
                std::cout << "Go lower: ";    
        }
        else{
            // If the extraction fails, `std::cin` will evaluate to false
            std::cout << "That wasn't a number, try again: ";
            std::cin.clear(); // Clear the fail bits
        }
    }while(x != y);
    std::cout << "Congratulations, you guessed my number :)";

    return 0;
}

答案 1 :(得分:1)

试试这个:

void main()
{
  int x;
  cout << "Please enter a number\n";
  getline(cin, x);
  int y = rand();

  while(x != y)
  {
   if(x < y)
    cout << "Go higher";
   else
     cout << "Go lower";
  }
}

答案 2 :(得分:1)

不太熟悉C ++,但我很确定while / if应该看起来像这样

 while (x != y)
{

 if (x < y)
    cout << "Go higher";
 else
    cout << "Go lower";
}

if和while循环的条件应该用括号括起来。

答案 3 :(得分:0)

上面列出的所有内容都是语法错误。通过阅读c ++

的语法可以很容易地解决这个问题

http://www.cs.duke.edu/csed/tapestry/howtoa.pdf

看起来应该更像这样:

 while (x != y)
 {

     if (x < y)
        cout >> "Go higher";
     else
        cout >> "Go lower";
  }

答案 4 :(得分:0)

让我们看看所有的错误:

#include <iostream>
#include <cstdlib>
using namespace std;

int main()   // guess number game
{
    int x;
    cout >> "Please enter a number\n"; // should be `cout <<` cin uses >>
    getline(cin x); // incorrect number of arguments should be 2 or 3 plus x is an int not string could use cin instead
    int y = rand();

    while x != y // error condition checks should be parentheses like (x != y)
    {

        if x < y; // error condition should be parentheses also by adding a semicolon here you terminate the statement
           cout >> "Go higher";
        else; // technically not an error but unintended code is executed cout >> "Go higher" is always executed because of the semi colon ;
           cout >> "Go lower";
    }

// no return of value, you declared main to return an int so you should
}

试试这个:

#include <iostream>
#include <cstdlib>
using namespace std;

int main()   // guess number game
{
    int x;
    cout << "Please enter a number\n";
    cin >> x;
    int y = rand();

    while (x != y)
    {

        if (x < y)
           cout >> "Go higher";
        else
           cout >> "Go lower";
    }

    return 0;
}