Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

displays names in Arrays

Posted on 2004-08-22
12
Medium Priority
?
256 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 

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 750 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Simple Linear Regression

664 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