Thread: Cant make "do while" work

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    1

    Cant make "do while" work

    Hello I can run the program but the problem is that I want it to end it when I type the correct number(so ">0") but it doesnīt work and well I donīt know why.

    Code:
    #include <stdio.h>#include <stdlib.h>
    void converttokoruna(float euro);
    float euro;
    
    
    int main()
    {
            float koruna= 0;
        do{
        scanf("%f", &koruna);
    converttokoruna(koruna);
        }while(euro = !0);
    
    
        return 0;
    }
    
    
    void converttokoruna(float euro){
        float koruna = euro * 24.79;
        if(euro > 0)
        {
        printf("%.2f Eur - %.2f Korun\n", euro, koruna);
        }
        else
        {
            printf("False number ! Write number again\n");
        }
        return;
    }
    Last edited by Alexandr__; 11-26-2017 at 05:55 PM.

  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861
    you're reading in one thing and checking something else. euro does not even have a set value. do this in your code;
    Code:
    void converttokoruna(float euro);
    float euro;
     
     
    int main()
    {
      print("%f\n", euro); // to see its value
    
    
            float koruna= 0;
        do{
        scanf("%f", &koruna);
    converttokoruna(koruna);
        }while(euro = !0);
     
     
        return 0;
    }

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The problem is in the condition:
    while(euro = !0)
    Several issues with this.
    1. !0 is actually a Boolean expression, so we can simplify it to 1 (NOT 0 is 1)
    2. Then you have the euro = 1 itself, which assigns (not compares, which would still be wrong) 1 to euro, so we can simplify again:

    while(1)
    Looks like an infinite loop to me.

    You probably meant this: while(euro != 0.00f)
    Last edited by whiteflags; 11-26-2017 at 08:06 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't get "return()" to work.Problem in "if" part
    By coder1 in forum C Programming
    Replies: 13
    Last Post: 09-10-2013, 01:08 AM
  2. Replies: 4
    Last Post: 10-18-2012, 08:43 AM
  3. nbin=fopen("input.txt","a"); doesn't work?
    By Adam Rinkleff in forum C Programming
    Replies: 2
    Last Post: 06-23-2011, 02:57 PM
  4. Replies: 2
    Last Post: 10-19-2010, 05:40 PM
  5. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM

Tags for this Thread