i have infinite loops. why?

This is a discussion on i have infinite loops. why? within the C Programming forums, part of the General Programming Boards category; #include <stdio.h> #include <conio.h> int main() { int answer, num, count = 0, total = 0, max = 0; float ...

  1. #1
    Registered User hermit's Avatar
    Join Date
    Apr 2002
    Posts
    213

    i have infinite loops. why?

    #include <stdio.h>
    #include <conio.h>

    int main()
    {

    int answer, num, count = 0, total = 0, max = 0;
    float average;

    do
    {
    clrscr();
    printf("Enter an integer: ");
    scanf("%i", &num);


    while ( count < 3 && num > 0 )
    {
    count=count + 1;
    total += num;
    if ( num > max )
    {
    max = num;

    }

    printf("Enter next integer: ");
    scanf("%i", &num);
    }

    if ( count > 0 )
    {
    printf("\n");
    printf("The maximum number is %i\n", max);
    printf("the count is %i\n", count);
    printf("the total is %i\n", total);
    average= (float)total / count;
    printf("the average in two decimal point is %.2f\n", average);
    }

    else
    printf("No valid number were entered\n");
    printf("Another run? Y/N ");
    scanf("%i", &answer);
    } while ( answer != 'N');
    return(0);
    }



    i have problems doing the while loop! help me out if possible . .

    unsure about while ( answer !- 'N')???

    thanks mate

  2. #2
    Registered User hermit's Avatar
    Join Date
    Apr 2002
    Posts
    213
    cmon lads, shouldnt be hard to answer . .
    just need a click in the head

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Aha, I've seen this before. This is one little disadvantage of using scanf the wrong way. There are a lot of discussions about what's the best way to read from input (gets, fgets, scanf, etc). Try replacing:
    Code:
    scanf("%i", &answer);
    by:
    Code:
    answer = getchar();

  4. #4
    Registered User hermit's Avatar
    Join Date
    Apr 2002
    Posts
    213
    i dont think is scanf , tried, but still faulty . .

    im suppose to prompt the user for Y/N?
    if yes[y], re-run the whole program,
    if no [ N ] exit;

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    I've tried your program and it works fine if I replace the scanf with getchar (for reading the answer).

    b.t.w. you can also consider using the toupper function so the program also accept 'n' (lowercase).

    Code:
    while ( toupper(answer) != 'N');

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Try make answer a char instead of an int.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    I wasn't going to get involved, as I normally skip over code postings that lack [code] tags, but here are some hints:[list=1][*]If the user indicates s/he wants another run, reinitialize your count variable (it still holds the value from the last run).[*]scanf() is leaving a carriage return in the input buffer when you ask the user if they want another turn. This carriage return causes the next scanf() ("Enter an integer") to return early.[/list=1]If that helps in any way, you can thank me by using [code] tags in the future ;)
    Jason Deckard

  8. #8
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >scanf("%i", &num);

    Shouldn't this be

    scanf("%d", &num);

    Put some extra printf's in your code to check the loops.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    Oh what a mish-mash of half truths we have here....

  10. #10
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    Originally posted by Salem
    Oh what a mish-mash of half truths we have here....
    That is my biggest problem with this board. There is alot of people answearing things they don't really know. As a rule of thumb "Always check the replies you get in here with a book or some reliable source" There is only a hand full of people I really trust to know what they are talking about in here.
    Last edited by Barjor; 04-16-2002 at 02:52 PM.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >There is alot of people answearing things they don't really know.
    That I can tolerate, that is why these are called discussion forums. Anyone can answer and if they are wrong they will be corrected in short order.

    >Oh what a mish-mash of half truths we have here....
    Quite.

    >>scanf("%i", &num);
    >Shouldn't this be
    >scanf("%d", &num);
    Why? In actual practice %i and %d can be used the same way, though %i is a bit more generic as it can be used to scan in non-decimal values whereas %d doesn't have that feature.

    -Prelude
    My best code is written with the delete key.

  12. #12
    Registered User hermit's Avatar
    Join Date
    Apr 2002
    Posts
    213

    Thumbs down i have modified, but i still cant read the char

    #include <stdio.h>
    #include <conio.h>

    int main()
    {
    char answer;
    int num, count, total, max;
    float average;

    clrscr();
    do
    {
    count=0, total=0, num=0;
    printf("\n");
    printf("Enter an integer: ");
    scanf("%d", &num);


    while ( count < 3 && num > 0 )
    {
    count=count + 1;
    total += num;
    if ( num > max )
    {
    max = num;

    }

    printf("Enter next integer: ");
    scanf("%d", &num);
    }

    if ( count > 0 )
    {
    printf("\n");
    printf("The maximum number is %i\n", max);
    printf("the count is %i\n", count);
    printf("the total is %i\n", total);
    average= (float)total / count;
    printf("the average in two decimal point is %.2f\n", average);
    printf("\n");


    }

    else
    {
    printf("No valid number were entered\n");
    }

    printf("End of run. Another run? Y/N: ");
    answer=getchar();

    }while ( answer !='N' || answer != 'n');

    return(0);
    }


    Problems: the loops is soft of working, but can read char,
    once i read char, i have an infinite loops . .
    Last edited by hermit; 04-16-2002 at 10:06 PM.

  13. #13
    Registered User hermit's Avatar
    Join Date
    Apr 2002
    Posts
    213
    thanks deckard u got me thinking a little, i set count =0, max=0, and total =0
    Last edited by hermit; 04-16-2002 at 09:07 PM.

  14. #14
    Registered User hermit's Avatar
    Join Date
    Apr 2002
    Posts
    213

    Got it working!

    Here is a working code:

    #include <stdio.h>
    #include <conio.h>

    int main()
    {
    char answer;
    int num, count, total, max;
    float average;

    clrscr();
    do
    {
    count=0, total=0, num=0;
    printf("\n");
    printf("Enter an integer: ");
    scanf("%d", &num);


    while ( count < 9 && num > 0 )
    {
    count += 1;
    total += num;
    if ( num > max )
    {
    max = num;

    }

    printf("Enter next integer: ");
    scanf("%i", &num);

    }

    if ( count > 0 )
    {
    printf("\n");
    printf("The maximum number is %i\n", max);
    printf("the count is %i\n", count);
    printf("the total is %i\n", total);
    average= (float)total / count;
    printf("the average in two decimal point is %.2f\n", average);
    printf("\n");


    }

    else
    {
    printf("No valid number were entered\n");
    }

    printf("End of run. Another run? Y/N: ");
    answer=getchar();

    }while ( (answer=getchar()) != 'n' && (answer=getchar()) != 'N');

    return(0);
    }


    Thanks to those that helped me out, getchar() do work
    why scanf dont work in getting a character?
    - - fUnKy F3m@le - -

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here is how to get scanf working to read a single character, in a single line of code:

    scanf("%c%*c", &mychar );

    Or:

    scanf("%c%*[^\n]", &mychar );

    I think the second one will work also. That one should be "buffer safe".

    Quzah.
    Last edited by quzah; 04-16-2002 at 10:29 PM.
    Hope is the first step on the road to disappointment.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Infinite Loops in C
    By DaniiChris in forum C Programming
    Replies: 8
    Last Post: 07-06-2008, 12:21 PM
  2. Infinite While Loops Question
    By hern in forum C++ Programming
    Replies: 4
    Last Post: 08-13-2005, 07:27 PM
  3. please help, infinite loops!
    By HybridM in forum C++ Programming
    Replies: 18
    Last Post: 01-14-2003, 08:27 PM
  4. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 03:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21