Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C while loop issue

  1. Oct 9, 2008 #1
    #include <stdio.h>
    #include <string.h>

    double faktoriel (double y);

    void main()
    {
    double x, fac;
    char yes[5] ="yes";
    char answer[5];

    do
    {
    printf("Enter number:\n");
    scanf("%f", &x);

    while (50>x || x>100)
    {
    printf("Number doesn't match \n"); //50<x<100
    printf("Enter number again:\n");
    scanf("%f", &x);
    }
    fac = faktoriel (x);
    }while(strcmp (yes, answer)!=0);
    return;
    }

    double faktoriel (double y)
    {
    double i, factor;


    for(i=y; i>1; i--)
    {
    factor*=i;
    }
    printf("%f", factor);
    return factor;
    }
    ----------------------------------------------------------------------------------------
    There are proly many mistakes xD
     
  2. jcsd
  3. Oct 9, 2008 #2

    Borek

    User Avatar

    Staff: Mentor

    No idea what you are trying to do, but this is inconsistent.
     
  4. Oct 9, 2008 #3
    to ppl enter number lower than 100 higher than 50
     
  5. Oct 9, 2008 #4

    Borek

    User Avatar

    Staff: Mentor

    x>100 doesn't mean lower than 100.
     
  6. Oct 9, 2008 #5
    but while x is higher than 100 or lower than 50 to to that code
     
  7. Oct 9, 2008 #6
    or how should it be done ?
     
  8. Oct 9, 2008 #7

    KTC

    User Avatar

    Code (Text):
    x > 100
    means precisely what it suggest, x is greater than 100.

    And || is OR, I think you were wanting && (AND).
     
  9. Oct 10, 2008 #8

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    The line of code that Borek pointed out is, in fact, correct. It will keep asking the user to enter a value until it's between 50 and 100.

    Your post contains no questions, though, so I have no idea what you want from us.

    - Warren
     
  10. Oct 10, 2008 #9

    Borek

    User Avatar

    Staff: Mentor

    Oops, sorry. I was absolutely sure it was x>50 || x>100. Must be senior moment :eek:
     
  11. Oct 13, 2008 #10
    than why it is always repeating it self even if i enter number between 50 and 100 ?
     
  12. Oct 13, 2008 #11
    for exaple :
    #include <stdio.h>

    main()
    {
    double x;

    printf("Enter number:");
    scanf("%f", &x);

    while (50>x || x>100)
    {
    printf("Number doesn't match \n");
    printf("Enter number again:\n");
    scanf("%f", &x);
    }
    }
     
  13. Oct 13, 2008 #12
    it works with int ... why it doesn't work with double ?
     
  14. Oct 13, 2008 #13

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    It is always a good idea to run a program with a debugger. If you do this you will find that the value of x is nowhere close to the value you typed in. Hint: Your example program will work if you change x from a double to a float.
     
  15. Oct 13, 2008 #14
    but how do i do it in double ?
     
  16. Oct 13, 2008 #15
    You write programs to solve problems. Based on the complexity of your problem, you devise a solution that is most efficient/productive. What is your problem?
     
  17. Oct 13, 2008 #16
    my professor ... he demands double :grumpy:
     
  18. Oct 13, 2008 #17
    ah solution :
    #include <stdio.h>

    main()
    {
    float x;


    (double) x;
    printf("Enter number:");
    scanf("%f", &x);
    while (50>=x || x>=100)
    {
    printf("Number doesn't match \n");
    printf("Enter number again:\n");
    scanf("%f", &x);
    }
    }
    :)
     
  19. Oct 13, 2008 #18

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    I could tell you right now how to solve this problem, but that will not help you solve related problems in the future. You need to learn how to use reference material. There is no way to keep all the stuff associated with a language and its associated libraries in your head. The ability to quickly look things up in the reference material is key to having any success in programming.


    So, I will instead tell you to RTFM: Read The Fine (or if you prefer, some other F word that is a tad more vulgar) Manual. I'll even give a specific thing to read up on this time: scanf. If you are still vexed, ask again, but please only do so after you have read the documentation.
     
  20. Oct 13, 2008 #19

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    You will (or at least should) get points off if you offer this as your solution. In particular, (double) x; does absolutely nothing useful. In particular, it is not doing what your professor has asked of you.
     
  21. Oct 13, 2008 #20
    All i have bout scanf is int, char and float ... double is not mentioned :grumpy:
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: C while loop issue
  1. Loop in c (Replies: 1)

Loading...