Thread: while loop problem

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    6

    Unhappy while loop problem

    i made this program to print the fibnacci series. everything gone okey. but the variable m i used in this program, when i printed it's value in the end i get very unwanted result.

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int n,f=0,s=1,t,m=2;
    clrscr();
    printf("This program will print fibnacci series\n");
    printf("How many Number to be printed in the series?\n");
    scanf("%d",&n);
    printf("0 1");

    do
    {
    t=f+s;
    printf(" %d",t);
    f=s;
    s=t;
    m++;
    }

    while(m<=n);
    printf("%d",m)

    getch();
    }


    this was the output-

    This program will print fibnacci series
    How many Number to be printed in the series?
    5
    0 1 1 2 3 56

    is 56 not unobvious?
    i think it shoul give me 6

  2. #2
    Quote Originally Posted by jaipandya
    #include<stdio.h>
    Please use the 'code' tags (the [#] button).
    Why don't you read the rules before posting ?
    Emmanuel Delahaye

    "C is a sharp tool"

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    before we get to your problem, main() returns and int
    Code:
    int main(void)
    {
        return 0;
    }
    Try not to use non standard headers like conio.h
    replace getch() with getchar() (from stdio.h)
    clrscr() would need to be taken out of the program completely

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    6
    okey........for all but nowhere was the solution!

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >is 56 not unobvious?
    It's perfectly obvious, and exactly what you asked for.
    Code:
    printf("0 1"); /* 0 1 */
    printf(" %d",t); /* 1 2 3 5 */
    printf("%d",m); /* 6 */
    >i think it shoul give me 6
    When printing m after the loop terminates, print a newline first:
    Code:
    printf("\n%d\n",m);
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912

    Smile

    Here is your code with the already suggested fixes.

    Code:
    #include<stdio.h>
    int main()
    {
    	int n,f=0,s=1,t,m=2;
    	printf("This program will print fibnacci series\n");
    	printf("How many Number to be printed in the series?\n");
    	scanf("%d",&n);
    	printf("0 1");
    	do
    	{
    		t=f+s;
    		printf(" %d",t);
    		f=s;
    		s=t;
    		m++;
    	}
    	while(m<=n); 
    	printf("%d",m)
    	getchar();
    }
    The output should not give you 6, the next number should be 8. The whole series up to 100 is 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89.

    I don't see an immediate problem without digging too deep into your logic (which no one likes to do when nothing is commented). I imagine the problem is coming from somewhere in your do, while loop. This is a common program for beginners to write, so do a board search for 'fibonacci', and see if your logic statements differ from what others have used.

    edit: Blast... beaten again... Or you could just follow Prelude's suggestion.

  7. #7
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    It's not "56" it's "5" and then "6" is printed next to it by the statement:
    Code:
    printf("%d",m);
    which prints the counter after the loop finishes (which is equal to 6 at the end).
    Just remove it and everything will be OK!

    [edit]
    Sorry, someone has already posted before I finished writting my reply!
    none...

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    6
    thanks very much all the ppl.
    it was the same problem.......it was 5 and 6, not 56.

    i am totally a newbie to c language, it has hardly been 10 days, i started learning c language.

    thanks again

  9. #9
    Deleting... VOX's Avatar
    Join Date
    Oct 2004
    Location
    VA
    Posts
    94
    To get rid of that ugly clear-screen function, make your own.

    At the top of your code right after you include the header files:
    Code:
    #define VIDEO 0x10
    #define COLS 80
    #define ROWS 25
    then after those:

    Code:
    void cls(void);
    at the very bottom of your code after everything else:
    Code:
    void cls(void)
    {
    	union REGS r;
    
    	r.h.ah=0x06;
    	r.h.al=0x00;
    	r.h.bh=0x07;
    	r.h.ch=0x00;
    	r.h.cl=0x00;
    	r.h.dh=ROWS-1;
    	r.h.dl=COLS-1;
    	int86(VIDEO,&r,&r);
    
    	locate(0,0);
    }
    Now use cls(); to clear the screen instead. For this to work you will need to include the dos.h header.
    Boy you stink, go take a shower before you continue to code. Better do your laundry and spray your chair too.

    The one and only resource for finding information.

    Next version of windows released!!!!

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    For this to work you will need to include the dos.h header.
    I'm don't see the point in replacing one non-standard/non-portable method with another non-standard/non-portable method?

    If you want portable:
    Code:
    void mycls(void)
    {
      int i = 200;
    
      while(i--)
        putchar('\n');
    }
    There's my method :P
    Last edited by itsme86; 12-27-2004 at 09:21 AM.
    If you understand what you're doing, you're not learning anything.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >To get rid of that ugly clear-screen function, make your own.
    His ugly clear-screen function is more likely to work than your ugly clear-screen function. If you don't know why then maybe you shouldn't be suggesting non-portable solutions to a non-existent problem.
    My best code is written with the delete key.

  12. #12
    ---
    Join Date
    May 2004
    Posts
    1,379
    let me quote something prelude said to me a long while ago

    Quote Originally Posted by Prelude
    >so what is an alternative to system("cls") ?
    There isn't a good solution to the clearing-the-screen problem. I usually recommend that you either think long and hard about why you want to clear the screen, or isolate the non-portable parts:
    Code:
    void
    clear_screen(void)
    {
      system("cls");
    }
    By only using clear_screen to clear the screen, you only have to make minor changes when porting the program. It won't run everywhere unchanged, but when it comes to useful and nontrivial programs, you have to loosen your definition of "portable" a bit.

    You can find various ways to clear the screen in the FAQ.

  13. #13
    Registered User
    Join Date
    Sep 2004
    Posts
    6
    sorry for i am replying after a long time:

    point 1)
    I couldn't understand why people are saying to remove the clrscr() function from my program.

    point 2)
    My teacher told me to use getch() function in the end to stop the screen when viewing the output, if we don't do that the output screen stops for a sec only.

  14. #14
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    The clrscr function in your program is not portable and the code is unlikely to work on someone else's compiler.

  15. #15
    ---
    Join Date
    May 2004
    Posts
    1,379
    so is getch()
    to stop the console from closing, getch() can be replace with getchar()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  2. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  3. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM
  4. Loop problem
    By Tesnik in forum C++ Programming
    Replies: 29
    Last Post: 08-23-2007, 10:24 AM
  5. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM