473,386 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Can you find the infinite loop

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 2369
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
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 Expert
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
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...
4
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
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...
13
by: Vector | last post by:
Is any infinite loop better than other? Is there any difference between there efficiency?
1
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...
44
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.