while循环以及if和else语句

时间:2019-10-08 01:45:25

标签: c++ if-statement while-loop

#include <iostream>
using namespace std;

int main()
{
    int a, sumPositive, sumNegative;

    string promptContinue = "\n To continue enter 'y or Y', and to discontinue and get calculation result enter 'n or N' \n";

    string promptNum = "\nEnter a number : ";

    char response;

    cout << promptContinue;
    cin >> response;

    while (response = 'y'|'Y')
    {
        cout << promptNum;
        cin >> a;

        if(a >= 0 ){

            sumPositive += a;
        }
        else
            sumNegative += a;

        cout << promptContinue;
    }   

    cout << "Sum of all the positive numbers is : " << sumPositive <<endl;
    cout << "Sum of all the positive numbers is : " << sumNegative <<endl;

    return 0 ;
}

所以程序应该是: -获取用户的输入,直到用户键入“ n或N”以显示停止信号 -当用户键入“ n或N”时,程序的正数以及 负数之和。

我一直在得到

Permission denied
collect2.exe: error: ld returned 1 exit status
[Finished in 0.5s with exit code 1]

此错误消息,我不确定是什么问题。 预先谢谢你!

1 个答案:

答案 0 :(得分:-1)

此行:

while(response = 'y' | 'Y' ){

应该是

while(response == 'y' || response == 'Y'){

C ++中的OR运算符是双行||,脚本语言通常使用单行|来传递多个命令。

您还在比较中使用赋值运算符=而不是==,编译器拒绝了它。