473,385 Members | 1,647 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,385 software developers and data experts.

Why am I getting an infinite loop when I add a while statement?

Hi!
im working on this modified fibianoci problem for class and although i cant ever spell fibinoci right
I did manage to work out the function as my teacher has requested.

unforutnetly when i put the finishing touches on the code and added a while loop so the user could exit by typing -1 i am presented with an infinite loop of seemingly random numbers.

I assume Im missing a bracket somewhere but i just cant find it.

thoughts?
hints?
suggestions?

thank you to everyone

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3. #include <math.h>
  4. // This is Lab4Part1
  5. //  998275842
  6. // Houston Keil-Vine
  7. // Creates a sting of numbers
  8. // using the previous 2 numbers.
  9.  
  10. int main (void)
  11. {
  12.     int f3=1;
  13.     int f2=0;
  14.     int f1=1;
  15.     int f4=1;
  16.     int n=0;
  17.     int counter=0;
  18.  
  19.  
  20.  
  21.     printf("Enter a non-negative integer  or enter -1 to quit:\n");
  22.     scanf("%d",&n);
  23.  
  24.         while(n>0) {
  25.  
  26.  
  27.             for(counter=0; counter<=n; counter++)
  28.  
  29.             {
  30.                 printf("%d ",f4);
  31.                 f4=f1+f2;
  32.                 f1=f2;
  33.                 f2=f3;
  34.                 f3=f4;    
  35.             }
  36.             printf("\n");
  37.         }
  38.  
  39. return 0;
  40. }
  41.  
  42.  
Oct 8 '10 #1

✓ answered by Meetee

For eg, you give n = 5. Now when it goes in while loop, every time it gets n = 5 which is > 0. You are not decreasing n's value during each iteration. So it's going into infinite loop.

4 1914
Meetee
931 Expert Mod 512MB
For eg, you give n = 5. Now when it goes in while loop, every time it gets n = 5 which is > 0. You are not decreasing n's value during each iteration. So it's going into infinite loop.
Oct 8 '10 #2
I understand!
how can i get the program to keep repeating (asking for new integers) with out getting the infinite loop?
(well still having the typ -1 to exit?
Oct 8 '10 #3
Meetee
931 Expert Mod 512MB
Remove while loop to get rid of this problem. (for loop is OK)

And please check your logic again. I see problem in that too.
Oct 8 '10 #4
thanks so much for responding so quickly
I think i got it though!

we get a tester program to run it again and apperantly it all checks out.

I did finally catch the logic mistake.
thanks for the heads up
and for all your help.
Oct 8 '10 #5

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...
11
by: Alexander Bosch | last post by:
Hi, I'm having a problem similar to the one that's stated in this KB http://support.microsoft.com/default.aspx?scid=kb;en-us;839521 When I'm posting a page to itself with the bool value as true it...
59
by: rami | last post by:
please everybody ,can anyone tell me how to do an infinite loop in C
4
by: Michael Goerz | last post by:
Hi, why does the following result in an infinite loop when the user does *not* enter a number? do{ printf("Enter Number: "); } while ( ! scanf("%i", &input) );
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...
1
by: xfroggy | last post by:
Alright guys, If someone could give me some ideas or examples if possible since I'm really stuck on this one without a clue how to proceed. I'm trying to write a little logging program for my...
1
by: Sleepwalker | last post by:
Hi, I'm currently in the process of constructing a stripped down version of the company website that will be show to people logging in from a mobile device. When the user logs in to the homepage,...
2
by: Lawrence Krubner | last post by:
Imagine a template system that works by getting a file, as a string, and then putting it through eval(), something like this: $formAsString = $controller->command("readFileAndReturnString",...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.