#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    1
    Rep Power
    0

    [C Language][Code::Blocks][Win 7]Syntax error in code problem.


    Hi there,

    As the title suggests I have created a C program and I am using Code::Blocks to compile it. However I am experiencing problems when I try to compile due to a syntax error. I have tried to rectify the issue myself but for the life of me I cannot seem to find where the problem is. I am using Windows 7 by the way. Here is my code...

    Code:
    #include<stdio.h>
    main(){
    int iSelection,a[5],i,n,temp;
    do
    {
    printf("\n"); /*create new line*/
    printf("1-Add numbers to array\n"); /*Add prompt*/
    printf("2-Display numbers in array\n"); /*Display prompt*/
    printf("3-Remove number from array\n"); /*Remove prompt*/
    printf("4-Quit\n"); /*Quit prompt*/
    printf("Please enter your selection -> "); /*prompt for selection*/
    scanf("%d",&iSelection); /*get input from user for selection*/
    
    /*Add Numbers to array*/
    if(iSelection ==1){
    for(i=0;i<=4;i++){
    printf("\n");
    printf("Please enter a number for element [%d]->",i);
    scanf("%d",&a[i]);
    }
    }
    
    /*Display numbers and sort*/
    if(iSelection ==2){
    
    
    /*bubble sort*/
    do{
    n=0;
    for(i=0;i<=3;i++)
    {
    if(a[i]>a[i+1]){
    temp = a[i];
    a[i]= a[i+1];
    a[i+1]=temp;
    n++;
    }
    }
    } while (n!=0);
    
    if(a[0]==0){
    a[0]=a[1];
    a[1]=a[2];
    a[2]=a[3];
    a[3]=a[4];
    a[4]= 0;
    }
    
    if(a[0]==0 && a[4]==0){
    a[0]=a[1];
    a[1]=a[2];
    a[2]=a[3];
    a[3]= 0;
    a[4]= 0;
    }
    
    if(a[0]==0 && a[3]==0 && a[4]==0){
    a[0]=a[1];
    a[1]=a[2];
    a[2]= 0;
    a[3]= 0;
    a[4]= 0;
    }
    
    
    if(a[0]==0 && a[2]==0 && a[3]==0 && a[4]==0){
    a[0]=a[1];
    a[1]= 0;
    a[2]= 0;
    a[3]= 0;
    a[4]= 0;
    }
    
    /*display element and contained values*/
    for(i=0;i<=4;i++){
    printf("Element[%d] contains the value %d",i,a[i]);
    printf("\n");
    }
    }
    
    /*Remove lowest number*/
    if(iSelection ==3){
    a[0]= 0;
    }
    
    while (iSelection!=5);{
    printf("Thankyou for using this c program goodbye\n");
    }
    The error I keep receiving suggests that the syntax problem occurs in the last line of the code and it expects a statement or declaration at the end.

    If somebody could point me in the right direction it would be greatly appreciated. Thanks for your time.
  2. #2
  3. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,548
    Rep Power
    2337
    Have you not heard of indentation??? Here's what your code should look like, properly indented:

    c Code:
    #include<stdio.h>
    main(){
        int iSelection,a[5],i,n,temp;
        do
        {
            printf("\n"); /*create new line*/
            printf("1-Add numbers to array\n"); /*Add prompt*/
            printf("2-Display numbers in array\n"); /*Display prompt*/
            printf("3-Remove number from array\n"); /*Remove prompt*/
            printf("4-Quit\n"); /*Quit prompt*/
            printf("Please enter your selection -> "); /*prompt for selection*/
            scanf("%d",&iSelection); /*get input from user for selection*/
     
    /*Add Numbers to array*/
            if(iSelection ==1){
                for(i=0;i<=4;i++){
                    printf("\n");
                    printf("Please enter a number for element [%d]->",i);
                    scanf("%d",&a[i]);
                }
            }
     
    /*Display numbers and sort*/
            if(iSelection ==2){
     
     
    /*bubble sort*/
                do{
                    n=0;
                    for(i=0;i<=3;i++)
                    {
                        if(a[i]>a[i+1]){
                            temp = a[i];
                            a[i]= a[i+1];
                            a[i+1]=temp;
                            n++;
                        }
                    }
                } while (n!=0);
     
                if(a[0]==0){
                    a[0]=a[1];
                    a[1]=a[2];
                    a[2]=a[3];
                    a[3]=a[4];
                    a[4]= 0;
                }
     
                if(a[0]==0 && a[4]==0){
                    a[0]=a[1];
                    a[1]=a[2];
                    a[2]=a[3];
                    a[3]= 0;
                    a[4]= 0;
                }
     
                if(a[0]==0 && a[3]==0 && a[4]==0){
                    a[0]=a[1];
                    a[1]=a[2];
                    a[2]= 0;
                    a[3]= 0;
                    a[4]= 0;
                }
     
     
                if(a[0]==0 && a[2]==0 && a[3]==0 && a[4]==0){
                    a[0]=a[1];
                    a[1]= 0;
                    a[2]= 0;
                    a[3]= 0;
                    a[4]= 0;
                }
     
    /*display element and contained values*/
                for(i=0;i<=4;i++){
                    printf("Element[%d] contains the value %d",i,a[i]);
                    printf("\n");
                }
            }
     
    /*Remove lowest number*/
            if(iSelection ==3){
                a[0]= 0;
            }
     
            while (iSelection!=5);{
                printf("Thankyou for using this c program goodbye\n");
            }


    Can you see the problem now?

    And before you come back with "it just runs and runs and never stops", note that this:
    c Code:
    while (iSelection!=5);{

    results in an infinite loop by virtue of the semi-colon. If you turn up the warnings in the compiler you should/might get a warning about this. I think in fact this while is supposed to belong to the do further up, in which case the semi-colon would be appropriate, but really...this code is a mess. Clean it up.
    Last edited by ptr2void; June 22nd, 2011 at 06:11 AM.
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  4. #3
  5. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    Well it would help if you indented your code.

    Like so.
    Code:
    #include<stdio.h>
    main()
    {
        int iSelection, a[5], i, n, temp;
        do {
            printf("\n");           /*create new line */
            printf("1-Add numbers to array\n"); /*Add prompt */
            printf("2-Display numbers in array\n"); /*Display prompt */
            printf("3-Remove number from array\n"); /*Remove prompt */
            printf("4-Quit\n");     /*Quit prompt */
            printf("Please enter your selection -> ");  /*prompt for selection */
            scanf("%d", &iSelection); /*get input from user for selection */
    
            /*Add Numbers to array*/
            if (iSelection == 1) {
                for (i = 0; i <= 4; i++) {
                    printf("\n");
                    printf("Please enter a number for element [%d]->", i);
                    scanf("%d", &a[i]);
                }
            }
    
            /*Display numbers and sort*/
            if (iSelection == 2) {
                /*bubble sort*/
                do {
                    n = 0;
                    for (i = 0; i <= 3; i++) {
                        if (a[i] > a[i + 1]) {
                            temp = a[i];
                            a[i] = a[i + 1];
                            a[i + 1] = temp;
                            n++;
                        }
                    }
                } while (n != 0);
    
                if (a[0] == 0) {
                    a[0] = a[1];
                    a[1] = a[2];
                    a[2] = a[3];
                    a[3] = a[4];
                    a[4] = 0;
                }
    
                if (a[0] == 0 && a[4] == 0) {
                    a[0] = a[1];
                    a[1] = a[2];
                    a[2] = a[3];
                    a[3] = 0;
                    a[4] = 0;
                }
    
                if (a[0] == 0 && a[3] == 0 && a[4] == 0) {
                    a[0] = a[1];
                    a[1] = a[2];
                    a[2] = 0;
                    a[3] = 0;
                    a[4] = 0;
                }
    
    
                if (a[0] == 0 && a[2] == 0 && a[3] == 0 && a[4] == 0) {
                    a[0] = a[1];
                    a[1] = 0;
                    a[2] = 0;
                    a[3] = 0;
                    a[4] = 0;
                }
    
                /*display element and contained values*/
                for (i = 0; i <= 4; i++) {
                    printf("Element[%d] contains the value %d", i, a[i]);
                    printf("\n");
                }
            }
    
            /*Remove lowest number*/
            if (iSelection == 3) {
                a[0] = 0;
            }
    
            while (iSelection != 5); {
                printf("Thankyou for using this c program goodbye\n");
            }
    Notice how the last } is not at the left hand side. That's because you're missing some of them.

    Here's a tip. Always write empty constructs before filling them in with detail.
    Examples
    Code:
    for ( ; ; ) {
    }
    
    do {
    } while ( 0 );
    
    while ( 0 ) {
    }
    Here's another tip - compile OFTEN!
    The above snippets will compile just fine, even before you start adding code.

    Finally, add some functions. You have a single main that is heading for 100+ lines, and is nowhere near finished yet.
    Code:
    void doInput ( ) {
      printf("Input\n");
    }
    void doDisplay ( ) {
      printf("Display\n");
    }
    int main ( ) {
      /// prompt
      if ( iSelection == 1 ) doInput();
      if ( iSelection == 2 ) doDisplay(); 
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Hey prob in the program!!


    The program in short is about printing graphs (with X axis the word number and Y axis is the no of letters) from typed input of letters.

    Programming done in Codeblocks environment:I have created 2 files main.c and body.c;
    code in main.c :::
    Code:
    #include<stdio.h>
    #include "body.h"
    #include<stdlib.h>
    //white spaces to a letter is a word
     int count=-1,wc[10];
     int main()
    {
     int prev,i; char ch='\0';
    //initializing array elements to zero
     for(i=0;i<10;i++)
      wc[i]=0;
    //getting word count
     for(i=0;(ch=getchar())!=EOF;i++)
     {
      if(ch==' ' || ch=='\n'|| ch=='\t')
      prev=i;
      else
      {
       if(i==0)                                //Check1--no problem
       count++;
    
       if(i-prev==1)
       count++;
       wc[count]++;                  //inclusion of wc[count] inside else since white spaces not included in letter count
      }                              //error change from wc[i] to wc[count]
     }
     for(i=0;i<=count;i++)
     printf("\nW%d has %d letters",i,wc[i]);
     return 0;
    }
    ...............................................................................
    Code in body.c::
    Code:
    #include<stdio.h>
    //plotting histogram
    void plot()
    {
     int MAXVALUE=10,i;
     while(MAXVALUE>0)
     {
      for(i=0;i<=count;i++)
      {
       if(wc[i]>=MAXVALUE)
       printf("* ");
      }
      printf("\n");
      MAXVALUE--;
     }
    }
    ....................................................................................
    When i build and run i get this error:::

    Code:
    Compiling: body.c
    C:\Users\Pavan R Settigunte\Desktop\dr\body.c: In function 'plot':
    C:\Users\Pavan R Settigunte\Desktop\dr\body.c:8: error: 'count' undeclared (first use in this function)
    C:\Users\Pavan R Settigunte\Desktop\dr\body.c:8: error: (Each undeclared identifier is reported only once
    C:\Users\Pavan R Settigunte\Desktop\dr\body.c:8: error: for each function it appears in.)
    C:\Users\Pavan R Settigunte\Desktop\dr\body.c:10: error: 'wc' undeclared (first use in this function)
    Process terminated with status 1 (0 minutes, 0 seconds)
    4 errors, 0 warnings
    ...................................................................................
    i have made sure to declare wc and count as global variables and created header file of body.h in the correct format?? Wat's the problem??
  8. #5
  9. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Norcross, GA (again)
    Posts
    1,805
    Rep Power
    1570
    First off, please start a new thread; this one is over a year old, and unrelated to the problem you're having.

    Second, you should always, always, ALWAYS put code samples in code tags. Use the [code] or highlighter button to insert your code, otherwise it becomes an unreadable mess.

    Third, the problem is that you have declared the two globals in the main.c file, but do not have an external reference to them in body.c which would make them visible in that file. You need to either add an extern declaration of them inside body.c:
    Code:
    extern int count, wc[10];
    or, preferably, pass them to plot() as arguments:

    Code:
    #include<stdio.h>
    //plotting histogram
    void plot(int count, int wc[])
    {
        int MAXVALUE=10,i;
        while(MAXVALUE>0)
        {
            for(i=0;i<=count;i++)
            {
                if(wc[i]>=MAXVALUE) 
                    printf("* ");
            }
            printf("\n"); 
            MAXVALUE--; 
        }
    }
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov

IMN logo majestic logo threadwatch logo seochat tools logo