为用户提供循环for循环的选项

时间:2014-09-18 20:13:20

标签: c++ for-loop do-while

我无法为用户提供循环循环的选项。这个程序工作正常

#include <iostream>
using namespace std;

int main()
    {
    // goal is to calculate the sum of the first 10 terms of Leibniz's Series....
    // calculated by 1 - 1/3 + 1/5 - 1/7 + 1/9 ..... - 1/19

    int termNumber;  // keeps track of term numbers
    int numberOfTerms = 0;

    cout << "Enter number of Terms";
    cin >> numberOfTerms;


    double sum = 0.0;
    int sign = +1;

    for (termNumber = 1; termNumber <= numberOfTerms; termNumber++)
        {
        sum += ( sign / (2.0 * termNumber - 1));
        sign *= -1;
        }

    cout << "\n\n The sum is " << ( 4 * sum) << "\n\n";


    } // end body of loop

我需要给用户一个重复程序的选项,如果他愿意,我认为我可以把它放在do-while循环中,但是当我这样做时它只会循环&#34;输入术语数量&# 34;我尝试格式化的任何方式。这是我目前最好的。

  #include <iostream>
using namespace std;

int main()
{

// goal is to calculate the sum of the first 10 terms of Leibniz's Series....
// calculated by 1 - 1/3 + 1/5 - 1/7 + 1/9 ..... - 1/19

cout << "\nGiven a positive integer specifying some number of terms, this program\n approximates "
    "pi using Leibniz' Formula and the given number of terms.\n\n" ;
cout << "Leibniz' formula is 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + ... = Pi / 4.\n\n";

char yes = 0;
double sum = 0.0;

do {
    int termNumber;  // keeps track of term numbers
    int numberOfTerms = 0;


    int sign = +1;
    cout << "enter number of terms.\n";
    cin >> numberOfTerms;

    for (termNumber = 1; termNumber <= numberOfTerms; termNumber++)
        {
        sum += (sign / (2.0 * termNumber - 1));
        sign *= -1;
        }

    } 
    while (yes = 1);
cout << "\n\n The sum is " << (4 * sum) << "\n\n";
cout << "would you like to go again? " << yes;

} // end body of loop

我想让他们选择使用y / Y和n / N的术语编号来尝试不同的金额。

感谢您提供的帮助。

5 个答案:

答案 0 :(得分:2)

您需要cin设置是,而您的cout行位置错误。他们需要在你的do while循环中。在你的时间里==

do {
int termNumber;  // keeps track of term numbers
int numberOfTerms = 0;


int sign = +1;
cout << "enter number of terms.\n";
cin >> numberOfTerms;

for (termNumber = 1; termNumber <= numberOfTerms; termNumber++)
    {
    sum += (sign / (2.0 * termNumber - 1));
    sign *= -1;
    }
cout << "\n\n The sum is " << (4 * sum) << "\n\n";
cout << "would you like to go again? " << yes;
cin >> yes
} 
while (yes == 1);

试试

答案 1 :(得分:0)

您是否学会了使用

continue;
在for循环中

?我认为这符合您的描述,基本上是什么

continue;     

在某种情况下,它会返回到for循环的开头。

答案 2 :(得分:0)

只是我的两分钱,但也许你可以结合你的想法和使用功能。将上面的do部分中的代码放入一个带有参数个数的函数中。也可以在此函数的主体中使用for循环。然后在你的for循环的外面调用另一个函数,询问用户是否要继续,并询问一个新的数字&#34; terms&#34;如果用户提供“&#39;”并将其传回第一个功能。或者&#39; Y&#39;否则,如果&#39; n&#39;或者&#39; N&#39;你可以停止程序

答案 3 :(得分:0)

do while循环的问题是while()语句中使用的变量必须已经在循环体之外定义,即使语法上属于循环。使用for循环时可以避免这种情况,这允许在for语句中定义此类变量。这是一个例子。

inline double leibniz (int n) noexcept   // function for Leibniz sum
{
  double result=0;
  for(int k=1,sign=1; n; --n,++++k,sign=-sign)
    result += double(sign)/double(k);
  return result;
};

int main()
{
                                      // no loose variables defined outside loop
  for(bool again=true; again; ) {     // control variable only lives withing loop body
    int n;                            // number of terms, only needed within loop body
    std::cout<<"number of terms = ";
    std::cin >> n;
    std::cout<<"result = "<<leibniz(n)<<'\n'
             <<"try again? (1/0)";
    std::cin >>again;
  }
}

答案 4 :(得分:0)

感谢大家的帮助。

我还不知道continue;,但我了解了它。

主要问题是我的cincout并非如前所述。将最后一行重写为while (yes == 'Y' || yes == 'y');,现在一切正常。

你们很棒!