2012-07-10 46 views
-3

对不起,忘记了代码我在做什么错了这个C++代码?

这是不是的代码。 我一直试图让这个工作,但所有的逻辑运算符不起作用。

#include <iostream> 
#include <string> 
using namespace std; 
string repeat; 
string repeatnum; 
string prompt = "|-[]->"; 
int main() 
{ 
string entry; 
bool Running = true; 
while(Running == true) 
{ 
cout << "\n"; 
cout << prompt; 
cin >> entry; 
if(entry == "Exit") return 0; 
if(entry == "Help") cout << "HELP:\nThsi is a simple program, try an input"; 
if(entry == "ChangePrompt") 
{ 
    cout << "What do you want to change the prompt to?: "; 
    cin >> prompt; 
} 
if(entry == "Repeat" || "repeat") 
{ 
    cout << "What string do you want to repeat?: "; 
    cin >> repeat; 
    cout << "How many times do you want to repeat" << repeat << "(1-9)?: "; 
    cin >> repeatnum; 
    if(repeatnum > 0){} 
} 
} 
char f; 
cin >> f; 
return 0; 
} 

这是我得到的错误。

Error: 
C:\Users\Packard Bell\Desktop\test\main.cpp||In function 'int main()':| 
C:\Users\Packard Bell\Desktop\test\main.cpp|29|error: no match for 'operator>' in 'repeatnum > 0'| 
||=== Build finished: 1 errors, 0 warnings ===| 
+7

你没有张贴任何代码是什么。 – 2012-07-10 05:54:57

+5

代码在哪里? – Naveen 2012-07-10 05:55:11

+0

看来'repeatnum'不是一个数字。 – 2012-07-10 05:56:07

回答

2

因为在main.cpp 29行,你试图做repeatnum > 0repeatnum是没有超载operator >一个类型。

+0

我不明白,你能不能解释一下? – Dasttann777 2012-07-10 10:35:03

0

现在看到代码后。 repeatnum是一个字符串。您读取字符串的输入,然后将其与整数进行比较。现在字符串没有运算符> - 定义为整数,因此您需要在比较之前将该字符串转换为整数。

atoi(repeatnum.c_str()); 

或者使用stringstream来做到这一点。

0

从给定的信息,我只能猜测变量repeatnum是一个类或结构的对象,你不能用它直接与0比较。如果repeatnum的类型由您定义,请添加一个重载operator >的成员函数并正确处理它。

class YourType 
{ 
    // Class definition 
public: 
    int operator >(int var) 
    { 
     // Code for comparison 
     // return result 
    } 
}; 
1

除了repeatnum问题,这段代码是不是做你想做

if(entry == "Repeat" || "repeat") 

应该

if(entry == "Repeat" || entry == "repeat")