Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Parse error

Posted on 2004-08-22
19
Medium Priority
?
243 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 5
19 Comments
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 498 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Assisted Solution

by:kumvjuec
kumvjuec earned 252 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 498 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

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.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
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 …
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…

661 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