Solved

Parse error

Posted on 2004-08-22
19
240 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 116
Modify a small python script 19 116
Restore Windows 7 Snipping Tool function to Windows 10 10 77
ejb message driven bean mdb creation steps 2 24
I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

821 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