2012-04-18 71 views
0

不是很好的调试,但我收到一些错误。一些预期的'('')'和';' 也没有以前的'如果',没有匹配的'运营商>>'coutC++很少有初学者错误

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

#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"; 
    } 


} 
+0

如果这是家庭作业,请将其标记为此类。 – 2012-04-18 20:58:28

+0

这里没有什么可调试的,因为你所有的问题看起来都是语法错误。 – Chad 2012-04-18 20:59:19

+1

这不是家庭主妇,我不知道如何解决语法错误 – Foxic 2012-04-18 21:00:16

回答

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

这是错误的,std::ostreams只提供operator<<来插入格式化的d ATA。改为使用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; 
} 
+0

非常感谢你,修复并从你那里学到了很多 – Foxic 2012-04-18 21:11:06

+0

不客气。如果你使用'g ++'编译,然后使用'-Wall -Wextra'来获得更多的警告 - 这将帮助你更快地找到你的错误。 – Zeta 2012-04-18 21:23:02

0

上面列出的所有内容都是语法错误。这可以通过对C的语法++

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

它应该看起来更像这个阅读起来很容易解决:

while (x != y) 
{ 

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

cout需要使用“<<”运算符而不是“>>”运算符。 – 2012-04-18 21:18:28

+0

并没有计划解决所有的错误,而是从第一个被击中的开始。 – 2012-04-18 21:26:27

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"; 
    } 
} 
+0

这是怎么回事?与cout试图发送一些东西到一个字符串? – 2012-04-18 21:11:29

+0

@PaoloBrandoli这是错字。更正它。 – 2012-04-18 21:14:35

1

不是过于熟悉C++,但我敢肯定的同时,/如果应该是这个样子

while (x != y) 
{ 

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

如果这两个条件while循环应该用括号嵌套。

+0

cout需要使用“<<”运算符而不是“>>”运算符。 – 2012-04-18 21:16:36

+0

看起来像我不熟悉比我想大声笑谢谢。 – 2012-04-18 21:17:19

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; 
} 
+0

cout需要使用“<<”运算符而不是“>>”运算符。 – 2012-04-18 21:18:21