Solved

Parse error

Posted on 2004-08-22
19
236 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
Comment Utility
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
Comment Utility
hi!!

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

Author Comment

by:RebeccaFusco
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks for the points though ;-)
0
 

Author Comment

by:RebeccaFusco
Comment Utility
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
Comment Utility
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
Comment Utility
what are you meaning by sequence?? is it not printing anything??
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:RebeccaFusco
Comment Utility
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
Comment Utility
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
Comment Utility
example

1234512345
1234512345
1234512345
0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
Alternatively, you can declare your array as follow:

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

Author Comment

by:RebeccaFusco
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
cool thanks :-) I did not know about the jump at the end of each line.  
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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 …
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…

744 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

17 Experts available now in Live!

Get 1:1 Help Now