1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Please help me to fix my codes. problem in while loop?

Discussion in 'C' started by Tim541, Nov 7, 2015.

  1. Tim541

    Tim541 New Member

    Joined:
    Nov 7, 2015
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #‎include‬ <stdio.h>
    
    int main(){
    char name[8];
    char ordername[8];
    int ordernumber=0;
    float bill=0;
    
    printf("Enter X for exit");
    do{
    printf(" ordernumber: &s bill: &f", ordernumber, bill);
    printf("Enter your name: /n");
    scanf(" %s", name);
    
    printf("Enter your order (Pizza: 2$ Burger: 3$)");
    scanf(" %s", ordername);
    
    if(order == 'Pizza'){
    bill = 2;
    
    }else{
    bill = 3;
    }
    
    ordernumber++;
    
    }while(ordername != 'X');
    
    
    return 0;
    
    }
     
  2. ChavezAngela

    ChavezAngela New Member

    Joined:
    Jun 24, 2016
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    London
    Code:
    Line 1: error: invalid preprocessing directive #�
    In function 'main':
    Line 18: error: 'order' undeclared (first use in this function)
    Line 18: error: (Each undeclared identifier is reported only once
    Line 18: error: for each function it appears in.)
    Line 12: warning: character constant too long for its type
    Line 27: warning: comparison between pointer and integer
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    I'm not sure what the error is about on line 1. Looks fine to me.

    The variable "order" is undeclared, perhaps you meant to use "ordername" instead?
    And that is not how you do string comparison in C. First, strings are embedded in "double quotes". The reason for the errors about integers is that a character, or escape sequence, enclosed in 'single quotes' is a single char and is effectively an integer constant; for example 'A' means exactly the same as 65, if you're using ASCII.

    To compare strings you need to use the strcmp function, which returns zero if the strings are equal.
    Code:
    if (!strcmp(ordername, "Pizza"))
    
    should fix a few of those errors.
     

Share This Page