Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C++
#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);
}
Posted
Updated 8-Dec-18 2:36am
Comments
Richard MacCutchan 8-Dec-18 9:20am    
Did you also post https://www.codeproject.com/Questions/1270843/Write-a-Cplusplus-program[^] with a different account? If so please delete the duplicate account, and do not repost the same question.

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!
 
Share this answer
 
C++
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.
 
Share this answer
 
v2
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900