Solved

displays names in Arrays

Posted on 2004-08-22
12
251 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
strDist challenge 35 108
Counting documents in a Domino View 3 84
Export Table to CSV - Access to CSV - using python 18 139
Math Equation 23 88
I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
This is about my first experience with programming Arduino.
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…
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…

803 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