The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
[C Language][Code::Blocks][Win 7]Syntax error in code problem.
Discuss [C Language][Code::Blocks][Win 7]Syntax error in code problem. in the C Programming forum on Dev Shed. [C Language][Code::Blocks][Win 7]Syntax error in code problem. C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 22nd, 2011, 05:27 AM
|
|
Registered User
|
|
Join Date: Jun 2011
Posts: 1
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.
|

June 22nd, 2011, 06:08 AM
|
|
|
Have you not heard of indentation??? Here's what your code should look like, properly indented:
c Code:
Original
- 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("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 ]); } } /*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:
Original
- c Code |
|
|
|
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.
|

June 22nd, 2011, 06:13 AM
|
 |
Contributed User
|
|
|
|
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();
}
|

October 29th, 2012, 12:05 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 2
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??
|

October 29th, 2012, 12:59 PM
|
 |
Commie Mutant Traitor
|
|
Join Date: Jun 2004
Location: Norcross, GA (again)
|
|
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--;
}
}
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|