C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 22nd, 2011, 05:27 AM
MarkGL410 MarkGL410 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2011
Posts: 1 MarkGL410 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 m 22 sec
Reputation 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.

Reply With Quote
  #2  
Old June 22nd, 2011, 06:08 AM
ptr2void ptr2void is offline
I haz teh codez!
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Dec 2003
Posts: 2,476 ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 4 h 42 m 37 sec
Reputation Power: 2194
Have you not heard of indentation??? Here's what your code should look like, properly indented:

c Code:
Original - c Code
  1. #include<stdio.h>
  2. main(){
  3.     int iSelection,a[5],i,n,temp;
  4.     do
  5.     {
  6.         printf("\n"); /*create new line*/
  7.         printf("1-Add numbers to array\n"); /*Add prompt*/
  8.         printf("2-Display numbers in array\n"); /*Display prompt*/
  9.         printf("3-Remove number from array\n"); /*Remove prompt*/
  10.         printf("4-Quit\n"); /*Quit prompt*/
  11.         printf("Please enter your selection -> "); /*prompt for selection*/
  12.         scanf("%d",&iSelection); /*get input from user for selection*/
  13.  
  14. /*Add Numbers to array*/
  15.         if(iSelection ==1){
  16.             for(i=0;i<=4;i++){
  17.                 printf("\n");
  18.                 printf("Please enter a number for element [%d]->",i);
  19.                 scanf("%d",&a[i]);
  20.             }
  21.         }
  22.  
  23. /*Display numbers and sort*/
  24.         if(iSelection ==2){
  25.  
  26.  
  27. /*bubble sort*/
  28.             do{
  29.                 n=0;
  30.                 for(i=0;i<=3;i++)
  31.                 {
  32.                     if(a[i]>a[i+1]){
  33.                         temp = a[i];
  34.                         a[i]= a[i+1];
  35.                         a[i+1]=temp;
  36.                         n++;
  37.                     }
  38.                 }
  39.             } while (n!=0);
  40.  
  41.             if(a[0]==0){
  42.                 a[0]=a[1];
  43.                 a[1]=a[2];
  44.                 a[2]=a[3];
  45.                 a[3]=a[4];
  46.                 a[4]= 0;
  47.             }
  48.  
  49.             if(a[0]==0 && a[4]==0){
  50.                 a[0]=a[1];
  51.                 a[1]=a[2];
  52.                 a[2]=a[3];
  53.                 a[3]= 0;
  54.                 a[4]= 0;
  55.             }
  56.  
  57.             if(a[0]==0 && a[3]==0 && a[4]==0){
  58.                 a[0]=a[1];
  59.                 a[1]=a[2];
  60.                 a[2]= 0;
  61.                 a[3]= 0;
  62.                 a[4]= 0;
  63.             }
  64.  
  65.  
  66.             if(a[0]==0 && a[2]==0 && a[3]==0 && a[4]==0){
  67.                 a[0]=a[1];
  68.                 a[1]= 0;
  69.                 a[2]= 0;
  70.                 a[3]= 0;
  71.                 a[4]= 0;
  72.             }
  73.  
  74. /*display element and contained values*/
  75.             for(i=0;i<=4;i++){
  76.                 printf("Element[%d] contains the value %d",i,a[i]);
  77.                 printf("\n");
  78.             }
  79.         }
  80.  
  81. /*Remove lowest number*/
  82.         if(iSelection ==3){
  83.             a[0]= 0;
  84.         }
  85.  
  86.         while (iSelection!=5);{
  87.             printf("Thankyou for using this c program goodbye\n");
  88.         }


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:
Original - c Code
  1. 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.
__________________
I ♥ ManiacDan & requinix

This is a sig, and not necessarily a comment on the OP:
Please don't be a help vampire!

Last edited by ptr2void : June 22nd, 2011 at 06:11 AM.

Reply With Quote
  #3  
Old June 22nd, 2011, 06:13 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,836 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 16 h 15 m 5 sec
Reputation Power: 1774
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

Reply With Quote
  #4  
Old October 29th, 2012, 12:05 PM
ps3 ps3 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 2 ps3 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 29 sec
Reputation 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??

Reply With Quote
  #5  
Old October 29th, 2012, 12:59 PM
Schol-R-LEA's Avatar
Schol-R-LEA Schol-R-LEA is offline
Commie Mutant Traitor
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2004
Location: Norcross, GA (again)
Posts: 1,759 Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 3 h 38 m 3 sec
Reputation Power: 1568
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 ShortUnderstanding the C/C++ Preprocessor
Taming PythonA 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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > [C Language][Code::Blocks][Win 7]Syntax error in code problem.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap