这是我的代码。它应该取一个数字(n)并计算1到n之间所有整数的总和。该部分代码工作正常。不起作用的是可以选择再次运行代码的部分。我正在尝试做的是提示用户最后输入Y或N。如果用户输入Y,则应将布尔变量设置为true,以使代码再次运行。当我尝试运行它时,会告诉我“ Y”和“ y”是未识别的标识符和未声明的标识符。
#include <iostream>
#include<string>
using namespace std;
int main()
{
int n;
int sum = 0;
int sum_helper;
int n_original;
string goagainstr;
bool goagainbool;
do
{
cout << "Enter a positive integer: ";
cin >> n;
n_original = n;
if (n <= 0)
{
do
{
cout << "I said enter a POSITIVE integer: ";
cin >> n;
} while (n <= 0);
}
if (n > 0)
{
do
{
sum_helper = n;
sum = sum + sum_helper;
n = n - 1;
} while (n != 0);
cout << "The sum of all the integers from 1 up to "; cout << n_original; cout << " is "; cout << sum; cout << ". ";
}
cout << "Do you want to enter another number?(Y/N)";
getline(cin, goagainstr);
if (goagainstr==Y||goagainstr==y)
{
goagainbool = true;
}
} while (goagainbool == true);
return 0;
}