C++ 当循环不工作时,是否执行此操作?
在我的节目里。我的do-while循环不起作用,我不知道为什么。我希望do-while循环所要做的就是,当用户想要输入另一个号码时,减少重新运行程序的需要。试试这个:C++ 当循环不工作时,是否执行此操作?,c++,C++,在我的节目里。我的do-while循环不起作用,我不知道为什么。我希望do-while循环所要做的就是,当用户想要输入另一个号码时,减少重新运行程序的需要。试试这个: #include<iostream> #include<string> double tester; using namespace std; void stoupper (std::string& s) { std::string::iterator i = s.begin();
#include<iostream>
#include<string>
double tester;
using namespace std;
void stoupper (std::string& s)
{
std::string::iterator i = s.begin();
std::string::iterator end = s.end();
while (i != end) {
*i = std::toupper((unsigned char)*i);
++i;
}
}
double squarerootfinder (double number, double divisor){
tester = (number / (divisor * divisor));
if (divisor == 1) {
return 1;
}
else {
if (tester != (int)tester) divisor = squarerootfinder(number, divisor - 1);
}
return divisor;
}
int main() {
string runagain;
double number, divisor, squareroot, insidepart;
do {
cout << "Enter a whole number to find the square root of it \n";
cin >> number;
divisor = number;
squareroot = squarerootfinder(number, divisor);
insidepart = number / (squareroot * squareroot);
if (insidepart != 1) {
cout << squareroot << (char)251 << insidepart;
cout << endl;
}
else {
cout << squareroot << endl;
}
cout << "written by Arpan Gupta! \n";
cout << "Enter run again to run the program again. \n";
cin >> runagain;
stoupper(runagain);
} while (runagain == "RUN AGAIN");
return 0;
}
#包括
#包括
双测试仪;
使用名称空间std;
void stoupper(std::string&s)
{
std::string::iterator i=s.begin();
std::string::iterator end=s.end();
while(i!=结束){
*i=std::toupper((无符号字符)*i);
++一,;
}
}
双平方根查找器(双数字、双除数){
测试器=(数字/(除数*除数));
if(除数=1){
返回1;
}
否则{
if(tester!=(int)tester)除数=squarerootfinder(数字,除数-1);
}
返回除数;
}
int main(){
字符串再次运行;
双数、除数、平方根、内部零件;
做{
数量;
除数=数;
平方根=平方根查找器(数字、除数);
insidepart=数字/(平方根*平方根);
如果(内部零件!=1){
您不能在主循环中再次声明字符串;
两次,这是不必要的。此外,double tester
应该在squarerootfinder
函数中声明,因为您不会在程序中的任何其他地方使用它
cin
忽略空格您应该查看getline函数。这提供了一个如何使用它的示例。使用cin
这是问题的根源。您可以通过简单地添加行来测试这一点:
cout<<runagain;
您可以设置除数=number;
然后调用squarerootfinder
,但是不使用除数,为什么不这样做呢:
cout << "Enter a whole number to find the square root of it \n";
cin >> number;
squareroot = squarerootfinder(number, number);
cout>number;
平方根=平方根查找器(数字,数字);
因为“
除数”
和“数”
毕竟是相等的。出了什么问题?它不是在编译吗?它不是在做你期望的事情吗?因为它是家庭作业,我会给你一些提示。不要在循环中声明第二个再次运行的字符串。你上面声明的那个字符串很好。请在之后尝试打印出字符串包含的内容您输入“再次运行”。您会注意到cin在空白处停止。请改为查看getline。如果用户输入再次运行
???
cout << "Enter a whole number to find the square root of it \n";
cin >> number;
squareroot = squarerootfinder(number, number);