C ++程序中的无限循环 [英] Infinite loop in C++ program

查看:215
本文介绍了C ++程序中的无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个比较两个阵列的程序,他们有两个员工的工作日,用户输入月份和休息日,我做了下面的代码,但它给了我一个无限循环



我的尝试:



  #include   <   iostream  >  
使用 命名空间标准;
int main(){
int 个月,大小,daysOff1,daysOff2,出口,WORK1,WORK2;
执行 {
cout<< 请输入月份编号:<< endl;
cin>>月;

switch (月){
case 1
case 3
case 5
case 7
case 8
case 10
case 12
size = 31 ;
break ;
case 4
case 6
case 9
case 11
size = 30 ;
break ;
case 2
size = 28 ;
break ;
默认
cout<< 请输入正确的值<< endl;
继续; // 如果用户输入的值错误,请再次询问
}

cout<< 请输入fahad的关闭天数:< ;< ENDL;
cin>> daysOff1;
daysOff1 ++;
cout<< 请输入talal的关闭天数:< < ENDL;
cin>> daysOff2;
daysOff2 ++;

int fahad [size],talal [size];

for int i = 0 ; i< size; i ++)
{
while (work1< size)
{
if (fahad [i- 1 ] + daysOff1< size){
fahad [i] = fahad [i- 1 ] + daysOff1 + 1 ;
work1 = fahad [i];
i ++;

}
}
}

for int i = 0 ; i< size; i ++)
{
while (work2< size)
{
if (talal [i- 1 ] + daysOff2< size){
talal [i] = talal [i- 1 ] + daysOff2 + 1 ;
work2 = talal [i];
i ++;

}
}
}

for int i = 0 ; i< size; i ++)
{
for int j = 0 ; j< size; j ++){
if (fahad [i] == talal [j]){
cout<< i + 1<< endl;
}}}

cout<< 0退出1继续;
cin>>退出;
} while (退出!= 0 );
}

解决方案

我不知道该代码试图做什么:它根本没有多大意义,特别是没有你实际的家庭作业问题 - 我们甚至不知道你输入什么给你这些结果!



所以,这取决于你。

幸运的是,您有一个工具可以帮助您了解正在发生的事情:调试器。你如何使用它取决于你的编译器系统,但是一个快速的谷歌用于你的IDE名称和调试器应该给你你需要的信息。



放一个断点在函数的第一行,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!


 < span class =code-keyword> for ( int  i =  0 ; i< ; size; i ++)
{
while (work1< size)
{
if (fahad [i- 1 ] + daysOff1< size){
fahad [i] = fahad [i- 1 ] + daysOff1 + 1 ;
work1 = fahad [i];
i ++;

}
}
}



第一次循环此循环变量 i 的值为 0 。但是你试图通过 [i-1] 来解决数组元素,这是无效的,并且会产生一个随机值。这使你很可能将 fahad [i] ,然后 work1 设置为大于<的数字code> size 这将导致无限循环。



你需要检查你不试图解决的循环元素开始之前或结束之后的元素。


引用:

我做了下面的代码但它给了我一个无限循环



观察你的代码在调试器中真正做了什么,你会看到代码循环的位置,你会在执行时看到变量值。有些事情并不像你认为的那样。



你的代码没有你想象的那样,或者你不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向你展示你的代码在做什么和你的任务是与应该做的事情进行比较。

调试器中没有魔法,它不知道你的代码应该做什么,它没有找到bug,它只是帮助你向你展示发生了什么。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]


掌握调试Visual Studio 2010 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



1.11 - 调试程序(步进和断点)|学习C ++ [ ^ ]



调试器仅显示您的代码正在执行的操作,您的任务是与应该执行的操作进行比较。


am trying to make a program that compares between two arrays which they have working days of two employees and the user enter the month and the off days and i did the code below but it giving me an infinite loop

What I have tried:

#include<iostream>
using namespace std;
int main(){
	int months,size,daysOff1,daysOff2,exit,work1,work2;
	do{
		cout<<"Please enter the month's number: "<<endl;
		cin>>months;
		
		switch(months){
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12: 
				size = 31;
				break;
			case 4:
			case 6:
			case 9:
			case 11: 
				size = 30;
				break;
			case 2: 
				size = 28;
				break;
			default : 
				cout<<"please enter a correct value"<<endl;
			continue;  // to Make it ask again if the user entered a wrong value
		}
		
		cout<<"Please enter the number of off days for fahad: "<<endl;
		cin>>daysOff1;
		daysOff1++;
		cout<<"Please enter the number of off days for talal: "<<endl;
		cin>>daysOff2;
		daysOff2++;
		
		int fahad[size],talal[size];
	
		for (int i=0; i < size; i++)
		{
			while( work1 < size )
			{
				if(fahad[i-1]+daysOff1 < size){
					fahad[i] = fahad[i-1] + daysOff1 + 1;
					work1 = fahad[i];
					i++;
					
				}
			}
		}
		
		for(int i=0; i < size; i++)
		{
			while (work2 < size)
			{
				if(talal[i-1]+daysOff2 < size){
				talal[i] = talal[i-1] + daysOff2 + 1;
				work2 = talal[i];
				i++;
				
				}
			}
		}
	
		for(int i=0; i<size ;i++)
		{
			for(int j=0; j < size ;j++){	
		if(fahad[i] == talal[j]){
			cout<<i+1<<endl;
		}}}
		
		cout<<"0 To exit 1 to continue";
		cin>>exit;
	}while(exit != 0);
}

解决方案

I have no idea what that code is trying to do: it doesn't make much sense at all, especially without your actual homework question - we don't even know what you enter to give you these results!

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


for (int i=0; i < size; i++)
{
    while( work1 < size )
    {
        if(fahad[i-1]+daysOff1 < size){
            fahad[i] = fahad[i-1] + daysOff1 + 1;
            work1 = fahad[i];
            i++;
            
        }
    }
}


The first time round this loop variable i will have the value 0. But you are trying to address an element of the array by [i - 1] which is invalid, and will produce a random value. This makes it quite likely that you will set fahad[i], and thence work1, to a number larger than size which will cause the infinite loop.

You need to check in your loops that you do not try to address elements before the beginning or beyond the end of your arrays.


Quote:

i did the code below but it giving me an infinite loop


Watch what your code is really doing with the debugger, you will see where the code is looping and you will see the variables values as it execute. Something is not as you think it should be.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于C ++程序中的无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆