Solved

Parse error

Posted on 2004-08-22
19
239 Views
Last Modified: 2010-04-17
I am trying to store 5 sports names in a sequence but I am getting a parse error on the "for" line? I can not figure it out? Thanks

#include <stdio.h>
#include<string.h>
int main()

{
   char sports [5][15];
   
    strcpy(sports[0],"tennis");
    strcpy(sports[1],"football");
    strcpy(sports[2],"basketball");
    strcpy(sports[3],"baseball");
    strcpy(sports[4],"gymnastics");
 
     for(int i=0; i<5; i++)
{        
printf("%s\n",sports[i]);
printf("%s\n",sports[i]);
printf("%s\n",sports[i]);
printf("%s\n",sports[i]);
printf("%s\n",sports[i]);
printf("exiting...\n");  

}
getch();
return 0;                          
}          
0
Comment
Question by:RebeccaFusco
  • 8
  • 6
  • 5
19 Comments
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 166 total points
ID: 11865363
Are you using a C compiler instead of C++ compiler?
Declare "int i" just below char sports[5][15];
and use it inside the "for" without declaring:
for (i=0; .... etcetera)
0
 
LVL 3

Expert Comment

by:kumvjuec
ID: 11865384
hi!!

Isn't your question answered elsewhere :). Why don't you just delete it and reclaim your points
0
 

Author Comment

by:RebeccaFusco
ID: 11865397
Still not right? It is the "for" thing again.

#include <stdio.h>
#include<string.h>
int main()

{
   char sports [5][15];
   int i;
    strcpy(sports[0],"tennis");
    strcpy(sports[1],"football");
    strcpy(sports[2],"basketball");
    strcpy(sports[3],"baseball");
    strcpy(sports[4],"gymnastics");
 
    for(i<5;i++)  
       
printf("%s\n",sports[i]);
printf("exiting...\n");  
}
}
getch();
return 0;                          
}          

0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 3

Assisted Solution

by:kumvjuec
kumvjuec earned 84 total points
ID: 11865410
hi rebecca :)
see, the for statement has three parts, separated by semicolons. the first part is where you initialize, 2nd is where you check the condition and 3rd where you make some change to the variables. Since your code above doesn't have two semicolons, it will give error.
Refer to my last comment in your earlier question
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 166 total points
ID: 11865412
You must use:
    for(i=0;i<5;i++)  

Altough you have declared i above, still you must initialize it
0
 
LVL 3

Expert Comment

by:kumvjuec
ID: 11865448
thanks for the points though ;-)
0
 

Author Comment

by:RebeccaFusco
ID: 11865455
Thanks for your help I really appreciate it. Give me a year of this web-site and C programming and I will know what I am doing ;-)  
0
 

Author Comment

by:RebeccaFusco
ID: 11865479
I fixed the final parse error I was gettting and it does compile but still does not display the names in a sequence? Does that mean the "for" loop is not working?

 #include <stdio.h>
#include<string.h>
int main()

{
   char sports [5][15];
   int i;
    strcpy(sports[0],"tennis");
    strcpy(sports[1],"football");
    strcpy(sports[2],"basketball");
    strcpy(sports[3],"baseball");
    strcpy(sports[4],"gymnastics");
 
    for(i=0;i<5;i++)  /*use a \0*somewhere? */
       
printf("%s\n",sports[i]);
printf("exiting...\n");  

getch();
return 0;                          
}          

0
 
LVL 3

Expert Comment

by:kumvjuec
ID: 11865486
what are you meaning by sequence?? is it not printing anything??
0
 

Author Comment

by:RebeccaFusco
ID: 11865493
It is just printing out the five different sports and I was trying to get it to print out like 2 rows of the five across and 10 of the 5 sports down like a continous or connected series.
0
 
LVL 3

Expert Comment

by:kumvjuec
ID: 11865508
try removing "\n" from the
printf("%s\n",sports[i]);

btw, if that is still not what you want, type here how you want it to be.
0
 

Author Comment

by:RebeccaFusco
ID: 11865510
example

1234512345
1234512345
1234512345
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11865514
Alternatively, you can declare your array as follow:

char *sports[] = { "tennis", "football", "basketball", "baseball", "gymnastics" };
0
 

Author Comment

by:RebeccaFusco
ID: 11865547
Thanks that works. How do I get it to go down 5 rows?   I though the array would do that?        
#include <stdio.h>
#include<string.h>
int main()

{
   char *sports[]={"tennis","football","basketball","baseball","gymnastic"};
   int i;
    strcpy(sports[0],"tennis");
    strcpy(sports[1],"football");
    strcpy(sports[2],"basketball");
    strcpy(sports[3],"baseball");
    strcpy(sports[4],"gymnastics");
 
    for(i=0;i<5;i++)  /*use a \0*somewhere? */
       
printf("%s",sports[i]);
getch();
return 0;                          
}          


0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11865562
No, don't need to use strcpy:

Thanks that works. How do I get it to go down 5 rows?   I though the array would do that?        
#include <stdio.h>
#include<string.h>
int main()

{
   char *sports[]={"tennis","football","basketball","baseball","gymnastic"};
   int i;
 
    for(i=0;i<5;i++)  /*use a \0*somewhere? */
        printf("%s",sports[i]);

    getch();
    return 0;                          
}          

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11865571
Not sure about your requirement

#include <stdio.h>
int main()
{
   char *sports[]={"tennis","football","basketball","baseball","gymnastic"};
   int i, j;
 
    for(i=0;i<5;i++)
        for (j=0; j<10; j++)
             printf("%s",sports[j%5]);

    getch();
    return 0;                          
}          

0
 

Author Comment

by:RebeccaFusco
ID: 11865602
That is more what I was trying to do. So I just have to add in the other "for" loop to run them in row and columns? I have been wanting to play around with the \0 function so I am going to try that next. Thank you both for your help today.  Rebecca
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11865615
if you want to put a space between words, use

printf("%s  ", ..... etcetera

to jump at the end of each line use

printf ("\n\r");

Here is the resume:

#include <stdio.h>
int main()
{
   char *sports[]={"tennis","football","basketball","baseball","gymnastic"};
   int i, j;
 
    for(i=0;i<5;i++) {  // 5 rows
        for (j=0; j<10; j++)   // 10 elements per line
             printf("%s  ",sports[j%5]);
        printf ("\n\r");    // this line is outside the for(j... loop
    }

    getch();
    return 0;                          
}      
0
 

Author Comment

by:RebeccaFusco
ID: 11865630
cool thanks :-) I did not know about the jump at the end of each line.  
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 95
mapAB Challlenge 35 143
how to send memory stream from ics Client To ics server ? 11 105
simplest php form 3 77
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question