Solved

Parse error

Posted on 2004-08-22
19
237 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
countHi challenge 25 98
Specific format 21 181
File.WriteAllLines problem at random C# ASP.NET 6 65
How can i compile this github project?? 2 74
RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
This is an explanation of a simple data model to help parse a JSON feed
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

914 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now