By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,302 Members | 1,429 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,302 IT Pros & Developers. It's quick & easy.

Can you find the infinite loop

P: 1
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
Share this Question
Share on Google+
3 Replies


P: 7
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

P: 7
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

Expert
P: 96
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

Post your reply

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