Solved

displays names in Arrays

Posted on 2004-08-22
12
252 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
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

A short article about a problem I had getting the GPS LocationListener working.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…

828 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