Issues with Printing Array

hello! I have been having difficulty with printing an array in my homework assignment and I have been able to find a solution online. Could you guys help me?

I am using a C++ programming system! I have tried using the printf("%s"); to print it, an I have also attempted for loops, but to no avail.

I want to print from the char state[3][10] array.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>


main()
{

char state[3][10];

char str1[]="Florida";
char str2[]="Oregon";
char str3[]="California";
char str4[]="Georgia";

strcpy(str1, state[0]);
strcpy(str2, state[1]);
strcpy(str3, state[2]);
strcpy(str4, state[3]);

//way to print would go here


system("PAUSE");
return 0;

}

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main() // missing type int
{

    char state[4][10]; // You have 4 elements here

    char str1[]="Florida";
    char str2[]="Oregon";
    char str3[]="California";
    char str4[]="Georgia";

    strcpy(state[0],str1); // strcpy (destination,source);
    strcpy(state[1],str2); // You had it backwards
    strcpy(state[2],str3);
    strcpy(state[3],str4);

    printf("%s", state[0]); // use printf("%s",  "A String"); to print a string
    printf("%s", state[1]);
    printf("%s", state[2]);
    printf("%s", state[3]);


    return 0;
}

Last edited on
The program is now working properly! I really appreciate the help, it was such a silly, simple mistake yet it slipped right under my nose, ahhaa.

Thank you! c:
You're very welcome
Topic archived. No new replies allowed.