Another frustrating error in C

Discussion in 'Mac Programming' started by Quboid, May 19, 2007.

  1. Quboid macrumors 6502

    Joined:
    Oct 16, 2006
    Location:
    everywhere
    #1
    Hello all,
    I've probably posted over 10 programing questions here already and i don't think this would be the last one, so before i start let me just thank you guys (and girls) for being so helpful, I think you are an exceptionally resourcefull bunch of internet freinds.

    Now, I am trying to practice structure in c, So I am writing a program that is suppose to capture the vocals and their corresponding ascii in a list-structure.
    I wrote the code to capture the data but now i am trying to print it on the screen and it i am getting a strange error( to me atleast) that says that i am missing a ")" in a while statement. I'll post hte code so you guys( and girls) can take a look. Thanks in advance for the help.

    #include<stdio.h>


    #define null 0;
    // Prototypes

    void menu();
    void insert();
    void fill();
    void mostrar();
    void delete_data();

    // LIST DeClArAtIoN

    struct lista{
    char vocal;
    int ascii;
    struct lista *liga;
    };

    struct lista *inicio,*nodo,*fin,*aux;
    char voc; int ascii;
    char respuesta;









    void main()
    {
    menu();
    getch();

    }

    void menu()
    { int option;
    do{
    printf("\n\t\t ####**** MENU ****####");
    printf("\n\n\t1. Insert data from the begining of the list");
    printf("\n\n\t2. insert data from the end of the list");
    printf("\n\n\t3. Show Data on screen");
    printf("\n\n\t4. Delete data");
    printf("\n\n\t5. exit");
    scanf("%i",&option);
    if(option==1)
    {
    insert();
    }
    if(option==2)
    {
    fill();
    }
    if(option==3)
    {
    mostrar();
    }




    }while(option!=5);
    }



    void insert()
    {
    struct lista *inicio,*nodo,*fin;
    char voc; int ascii;
    char respuesta;

    inicio=(struct lista*)malloc(sizeof(struct lista));
    printf("\n\t introduce el voval\t");
    fflush(stdin);
    scanf("%c",&voc);
    (*inicio).vocal=voc;
    printf("\n\t Introduce el ascii para %c\t",voc);
    fflush(stdin);
    scanf("%i",&ascii);
    (*inicio).ascii=ascii;
    fin=inicio;
    (*inicio).liga=null;

    do{
    nodo=(struct lista*)malloc(sizeof(struct lista));
    printf("\n\tIntroduce vocal\t");
    fflush(stdin);
    scanf("%c",&voc);
    (*nodo).vocal=voc;
    printf("\n\t Introduce el ascii\t");
    fflush(stdin);
    scanf("%i",&ascii);
    (*nodo).ascii=ascii;
    (*nodo).liga=inicio;
    inicio=nodo;
    printf("\n\t Quieres continuar? si(s)/no(n)\t");
    fflush(stdin);
    scanf("%c",&respuesta);
    }while(respuesta!='n');
    }



    void fill()
    {

    inicio=(struct lista*)malloc(sizeof(struct lista));
    printf("\n\t Give me a vowel\t");
    fflush(stdin);
    scanf("%c",&voc);
    printf("\n\t Give me the ascii for %c",voc);
    fflush(stdin);
    scanf("%i",&ascii);
    (*inicio).ascii=ascii;
    (*inicio).vocal=voc;
    (*inicio).liga=null;
    fin=inicio;

    do{
    nodo=(struct lista*)malloc(sizeof(struct lista));
    printf("\n\t Give me another vowel");
    fflush(stdin);
    scanf("%c",&voc);
    printf("\n\t Give me the ascii for %c", voc);
    fflush(stdin);
    scanf("%i",&ascii);
    (*nodo).vocal=voc;
    (*nodo).ascii=ascii;
    (*nodo).liga=null;
    (*fin).liga=null;
    fin=nodo;
    printf("\n\t Do you want to continue? yes(y), no(n)");
    fflush(stdin);
    scanf("%c",&respuesta);
    }while(respuesta!='n');
    }

    void mostrar()
    {
    aux=inicio;
    while(aux!=null){
    printf("\n\n\t %c",(*aux).vocal);
    printf("\n\n\t %i",(*aux).ascii);
    aux=(*aux).liga;
    };
    }
     
  2. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #2
    The problem is that you shouldn't have the semicolon here:

    #define null 0;

    The define directive doesn't use a trailing semicolon, so it treats that as one of the tokens in your definition. You are defining "null" to mean a zero and a semicolon, when you only want it to be a zero.

    Your line

    while( aux!=null ){

    was being interpreted as

    while( aux!= 0; ){

    which generated the error.

    * * *

    Also, you should not use a trailing semicolon in this syntactic form

    while(a==b) { ... };

    because the closing brace is enough. That extra semicolon creates an empty statement, equivalent to this:

    while(a==b) { ... }
    /* statement that does nothing */ ;


    Normally, an empty statement is harmless, but it would break code like this:

    if (c==d) while(a==b) { ... }; else ... ;
     
  3. caveman_uk Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #3
    Whilst we're on the subject never do
    Code:
    for (...);
    
    as the semicolon makes the for loop do nothing.
     
  4. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #4
    Good advice. But there's an exception: cases where side effects are the goal. Here's code, for example, to find the end of a string:
    Code:
    for ( mypointer = mystring ; *mypointer != '\0' ; mypointer++ ) ;
    Because extraneous semicolons can so easily lead to errors, I've established a habit that I always observe: If I really do want a null statement, I use braces instead of a semicolon, to make it obvious that it's on purpose. So I'd code my example as follows:
    Code:
    for ( mypointer = mystring ; *mypointer != '\0' ; mypointer++ ) {}
     

Share This Page