473,609 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can you find the infinite loop

1 New Member
i am trying to write a program but it is not working the way it should?

can you find the infinite loop...



#include <iostream>
using namespace std;

int main() {
int input;
int InputCount=0;
int RowSize=2;
int ColumId = 0;

cin >> input;
if (input == 0);
cout << "done";

while((input <= -1)||(input >= 1));
{
while (InputCount <= RowSize){

if (InputCount == 0);
ColumId =(InputCount % RowSize);
cout << ColumId << endl;
cout << input;
InputCount ++;

if (InputCount == 1);
ColumId =(InputCount % RowSize);
cout << ColumId << endl;
cout << input;
InputCount ++;

if (InputCount == 2);
ColumId =(InputCount % RowSize);
cout << ColumId << endl;
cout << input;
InputCount ++;

}

}}
Oct 4 '06 #1
3 2384
analwarsi
7 New Member
The Outer most while loop of ur program will exit only if the value of input is 0.but the value of input is never modified which means if u enter input=2. then its going to retain that value throughout the program and that is why u r getting infinite loop. i hope that this should solve ur problem
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. int input;
  6. int InputCount=0;
  7. int RowSize=2;
  8. int ColumId = 0;
  9.  
  10. cin >> input;
  11. if (input == 0);
  12. cout << "done";
  13.  
  14. while((input <= -1)||(input >= 1));
  15. {
  16. while (InputCount <= RowSize){
  17.  
  18. if (InputCount == 0);
  19. ColumId =(InputCount % RowSize);
  20. cout << ColumId << endl;
  21. cout << input;
  22. InputCount ++;
  23.  
  24. if (InputCount == 1);
  25. ColumId =(InputCount % RowSize);
  26. cout << ColumId << endl;
  27. cout << input;
  28. InputCount ++;
  29.  
  30. if (InputCount == 2);
  31. ColumId =(InputCount % RowSize);
  32. cout << ColumId << endl;
  33. cout << input;
  34. InputCount ++;
  35.  
  36. }
  37.  
  38. }}
  39.  
Oct 4 '06 #2
analwarsi
7 New Member
One more problem in ur code.see the highlighted line i.e; the first while loop u have put a semicolon at the end of the line which should be removed.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. int input;
  6. int InputCount=0;
  7. int RowSize=2;
  8. int ColumId = 0;
  9.  
  10. cin >> input;
  11. if (input == 0);
  12. cout << "done";
  13.  
  14. while((input <= -1)||(input >= 1));
  15. {
  16. while (InputCount <= RowSize){
  17.  
  18. if (InputCount == 0);
  19. ColumId =(InputCount % RowSize);
  20. cout << ColumId << endl;
  21. cout << input;
  22. InputCount ++;
  23.  
  24. if (InputCount == 1);
  25. ColumId =(InputCount % RowSize);
  26. cout << ColumId << endl;
  27. cout << input;
  28. InputCount ++;
  29.  
  30. if (InputCount == 2);
  31. ColumId =(InputCount % RowSize);
  32. cout << ColumId << endl;
  33. cout << input;
  34. InputCount ++;
  35.  
  36. }
  37.  
  38. }}
  39.  
Oct 4 '06 #3
pragatiswain
96 Recognized Expert New Member
Optimized code is given below. Hope this helps.

#include <iostream>
using namespace std;

int main()
{
int input;
int InputCount=0;
int RowSize=2;
int ColumId = 0;

cout << "Please specify the Input (Integer only): ";
cin >> input;

if (input == 0) cout << "Done" << endl;

if ((input <= -1)||(input >= 1))
{
while (InputCount <= RowSize)
{
ColumId =(InputCount % RowSize);
cout << ColumId << endl;
InputCount ++;
}
cout << "Input Value: " << input << endl;
}
}
Nov 16 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

43
5564
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a fallacy built on the assumption of mythical infinite all powerfull machines. In reality we deal with finite machines that are capable of two states in a loop, they either terminate, or repeat themselves. In the mythical halting problem scenario...
4
2625
by: LOPEZ GARCIA DE LOMANA, ADRIAN | last post by:
Hi all, I have a question with some code I'm writting: def main(): if option == 1: function_a()
7
1720
by: Marc Fauser | last post by:
How can I find the textbox (handle?) e.g. from http://www.microsoft.com/ on the right top corner from my program? And how can I identify the textbox again, if a new browser window is opened with the same page? Both textboxes have different handles. How can I recognize the same textbox? Marc
13
2278
by: Vector | last post by:
Is any infinite loop better than other? Is there any difference between there efficiency?
1
8763
by: winston.heng | last post by:
Hi, Thanks for reading this posting. I have been cracking my head on solving the infinite loop when i call the following section code. Any help or advise is greatly appreciated =D Thanks in advance. Cheers!
44
4320
by: James Watt | last post by:
can anyone tell me how to do an infinite loop in C/C++, please ? this is not a homework question .
0
8109
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8534
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8509
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8188
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8374
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6969
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6034
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4002
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1630
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.