简体   繁体   English

while 循环和 if 和 else 语句

[英]while loop and if and else statement

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

So what the program supposed to is: - Get user's input until user types 'n or N' to show a sign of stop - When user type in 'n or N' then the program sum of positive number as well as sum of negative numbers.所以程序应该是: - 获取用户的输入,直到用户输入“n 或 N”以显示停止标志 - 当用户输入“n 或 N”时,程序的正数之和以及负数之和.

And what I've been getting而我得到的

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

this error message, and I'm not sure what the problem is.此错误消息,我不确定问题是什么。 Thank you in advance!先感谢您!

This line:这一行:

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

Should be应该

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

The OR operator in C++ is a double line || C++ 中的 OR 运算符是双线|| , a single line | , 单行| is typically used in scripting languages for piping multiple commands.通常在脚本语言中用于管道多个命令。

You were also using an assignment operator in a comparison, = instead of == , which was rejected by the compiler.您还在比较中使用了赋值运算符=而不是== ,编译器拒绝了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM