Solved

displays names in Arrays

Posted on 2004-08-22
12
255 Views
Last Modified: 2010-04-17
I am trying to write an array program that stores 5 sports and displays the sports in a sequence. I am not sure of the correct way to go about it? This is how I started and now I am stuck. It gives me a parse error on line 6 and I do not know why? Thanks
#include <stdio.h>
#include<string.h>
int main()

{
    int 1,2,3,4,5;
    char sports [5][10];
   
    strcopy(sports[1],"tennis");
    strcpy(sports[2],"football");
    strcpy(sports[3],"basketball");
    strcpy(sports[4],"baseball");
    strcpy(sports[5],"gymnastics");
}
printf("%s\n"[1]");
printf("%s"[2]");
printf("%s\n"[3]");
printf("%s"[4]");
printf("%s\n"[5]");
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
12 Comments
 
LVL 2

Expert Comment

by:Parsi
ID: 11863809
Dear RebeccaFusco

I wonder why you are declaring numbers 1 to 5 as int?
And also why you are not using variable sports in printf?
0
 

Author Comment

by:RebeccaFusco
ID: 11863824
Because I am not sure of what I am doing and playing around with this. I was declaring number 1-5 as int to assign to the 5 different sports. And do you mean to use the variable in printf like I changed them to? Thanks

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

{
    int [5]=1,2,3,4,5;
    char sports 10];
   
    strcopy(sports[1],"tennis");
    strcpy(sports[2],"football");
    strcpy(sports[3],"basketball");
    strcpy(sports[4],"baseball");
    strcpy(sports[5],"gymnastics");
}
printf("%s\n"[sports]");
printf("%s"[sports]");
printf("%s\n"[sports]");
printf("%s"[sports]");
printf("%s\n"[sports]");
printf("exiting...\n");  

getch();
return 0;                          
}          
0
 

Expert Comment

by:JJ9867
ID: 11864002
ok here is what is going on...
First don't just use numbers for variable names. I think it is illegal but am not sure but here is why:

your first program was closer to correct than your second one.
First I you don't need the line:

int 1,2,3,4,5;

just delete that line.

Your Sting copy statements look good to me.
However you must print your strings like so:
printf("%s\n",Sports[0]);

Lastly the first value in an array is normally [0]
So you should assign that value first as an example an array of 10 things
would be accessed as follows
array[0-9]
if you tried array[10]
you would get a pointer to an area of memory that is out of bounds.


0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Author Comment

by:RebeccaFusco
ID: 11864087
I played around with it but it still is not right? I am clueless.

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

{
   char sports [5][10];
   
    strcopy(sports[1],"tennis");
    strcpy(sports[2],"football");
    strcpy(sports[3],"basketball");
    strcpy(sports[4],"baseball");
    strcpy(sports[5],"gymnastics");
{
printf("%s\n",sports[0]);
printf("%s\n",sports[1]);
printf("%s\n",sports[2]);
printf("%s\n",sports[3]);
printf("%s\n",sports[4]);
printf("exiting...\n");  

}
getch();
return 0;                          
}          

0
 
LVL 3

Expert Comment

by:kumvjuec
ID: 11864308
hi,
couple of things..
>>strcopy(sports[1],"tennis");
1. Replace this strcopy with strcpy

>>char sports [5][10];
2. Make it [5][15], since "gymnastics" has 10 characters, so it will need memory of 11.
0
 

Author Comment

by:RebeccaFusco
ID: 11864353
Ah ha! I missed that "o" in the str line. Dah! I was not thinking that I had to count the characters but it does make sense for the null character, I think. What would be the best way to display the names in a sequence? Thanks

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

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

}
getch();
return 0;                          
}          

0
 
LVL 3

Expert Comment

by:kumvjuec
ID: 11864369
You have to start from index 0 while copying above.
To print in sequence, use the for loop

for (int i=0; i<5; i++)
  printf("%s\n",sports[i]);

Also, get hold of a nice tutorial to start with.
http://www.glenmccl.com/index.htm
0
 

Author Comment

by:RebeccaFusco
ID: 11864402
Like this? It does not compile now.

#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
 
LVL 3

Expert Comment

by:kumvjuec
ID: 11864433
Replace this by --
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");  

}

for (int i=0; i<5; i++)
  printf("%s\n",sports[i]);

You should not place a ; after a for statement.
0
 

Author Comment

by:RebeccaFusco
ID: 11864478
Still getting a parse error on the for line? 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
 
LVL 3

Accepted Solution

by:
kumvjuec earned 250 total points
ID: 11864522
what compiler are you using??
#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++)
{        
printf("%s\n",sports[i]);
}
printf("exiting...\n");  
getch();
return 0;                          
}          
0
 

Author Comment

by:RebeccaFusco
ID: 11864532
Miracle C
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

626 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