C ++ Basic while循环,按键退出

时间:2015-02-12 01:03:15

标签: c++

我是编程新手,这是我的第二个任务。它应该接受来自用户的输入,直到他们进入|退出程序。我做错了什么?

int main()

    int i = 0;
    char a = 0;
        while ( a != "|" ){  //has also told me this is an
                             // invalid operator
    int numeric;
    cout << "Give character: ";
    cin >> a ;
    cout << "Its ascii value is: " << (int) a << endl;
    ++i;

    }
}

这是错误:

2   IntelliSense: operand types are incompatible ("char" and "const char *")    

2 个答案:

答案 0 :(得分:3)

不要使用"|"。改为'|'。您想要将charchar进行比较,而不是将charchar*进行比较。

答案 1 :(得分:1)

您应使用while ( a != '|' )代替

'|'是一个字符,"|"是一个只有1个字符的字符串

相关问题