在一个程序中使用for、while、do-while循环
Using for, while, do-while loops in one program
这个程序应该得到用户输入的任何数字(x)的倍数,直到它达到(y)。所有三个循环都正确运行,但是当一起使用时,我们得到了前两个循环,但第三个循环没有输出。我还想在新的一行中输出每个循环的输出。我的问题是我能做些什么来让我的输出在三个单独的行中,为什么我的第三个循环没有输出任何东西。
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
bool error(const string & msg);
int main(){
unsigned x, y;
cout << "Give me a number you want the multiples of and give me the max limit: ";
cin >> x >> y || error("Input Failure: ");
if (x == 0) error("Input can't be 0");
unsigned increment = x;
x = 0;
while( x < y)
{
cout << x << ", ";
x += increment;
if (x > y) break;
}
cout << endl; // This was the other problem. I kept putting it inside the loop
for(int x; x < y; x += increment){
cout << x << ", ";
if (x > y) break;
}
cout << endl; // This was the other problem. I kept putting it inside the loop
x = 0; // This is what originally was wrong
do{
cout << x << ", ";
x += increment;
if ( x > y){
break;
}
}while (x < y);
}
bool error(const string & msg){
cout <<"Fatal error: " << msg << endl;
exit(EXIT_FAILURE);
}
这并不奇怪。当您单独运行第三个循环时,变量x
不会被其他两个循环修改。实际上,第三个循环正在执行。第一个循环输出0,5,…, 35,然后第二个循环打印0,5,…, 35(注意
unsigned increment = x;
x = 0;
while( x < y)
{
cout << x << ", ";
x += increment;
if (x > y) break;
}
for(x = 0; x < y; x += increment){
cout << x << ", ";
if (x > y) break;
}
x=0;
do{
cout << x << ", ";
x += increment;
if ( x > y){
break;
}
}while (x < y);
for循环不会输出,因为您重新定义了x
并且没有给它一个新值。我不确定为什么你使用三个循环,实际上做同样的事情,除非你只是好奇他们是如何工作的。例如,你可以这样做,用不同的循环输出相同的内容:
#include <iostream>
using namespace std;
int main()
{
unsigned counter, increment, limit;
cout << "Give me a number you want to increment by of and give me the max limit: ";
cin >> increment >> limit;
//Check to see if the input stream failed
if(cin.fail())
{
cout << "Fatal Error: Input Failure" << endl;
return -1;
}
if(increment == 0 || limit == 0)
{
cout << "Input Cannot Be 0" << endl;
return -1;
}
if(limit <= increment)
{
cout << "Limit <= Increment" << endl;
return -1;
}
cout << "First Loop" << endl;
counter = 0; //Set counter to 0 for 1st loop
while(counter <= limit)
{
cout << counter << ", ";
counter += increment;
}
cout << endl;
cout << "Second Loop" << endl;
//for loop resets counter for 2nd loop
for(counter = 0; counter <= limit; counter += increment)
{
cout << counter << ", ";
}
cout << endl;
cout << "Third Loop" << endl;
//Reset counter again for 3rd loop
counter = 0;
do{
cout << counter << ", ";
counter += increment;
}while (counter <= limit);
return 0;
}
相关文章:
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- 如何通过替换顺序代码的while循环来添加OpenMP for循环
- 为什么 for 循环和 while 循环之间的 continue 不同?
- 将嵌套的 for 循环更改为 do-while 循环
- For 和 while 循环在全局定义的函数中不起作用
- 将 for 循环与嵌套的 while 循环并行化时出现 OpenMP 分段错误
- 使用嵌套的 for 循环比嵌套的 while 循环更好吗?
- for(;;) vs do..while() for main program loop
- 将 for 循环替换为 while 循环
- 如何将一个 while 循环与 for 循环组合在一起?
- C++创建一个类似于 while、if 或 for 的函数
- 为什么 For 循环中的 While 循环在我的程序中不起作用?
- 如何在 c++ 中使用 while 或 for 循环将用户输入的数字相加
- 为什么 for 循环的条件是可选的,而 while 循环不是?
- 如何查找用户使用 do/while、While 语句或 For 语句输入的几个整数的总和
- 如何突破内部 while 循环中的 for 循环
- C++迭代器行为在 for vs while 循环中
- Is while faster than for?
- c++中的文件读取:for和while嵌套循环没有按预期工作:串行
- 为什么 for/while/do-while 在执行相同的任务时被放置在 c/c++ 中