Solved

displays names in Arrays

Posted on 2004-08-22
12
249 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
 

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
A short article about problems I had with the new location API and permissions in Marshmallow
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…

708 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

15 Experts available now in Live!

Get 1:1 Help Now