Here is how I defined and assigned a string array at the same time
#include <stdio.h>
#include <cs50.h>
#include <string.h>
int main()
{
string word = get_string("Enter a Word;");
for(int i = 0; i<strlen(word); i++)
{
printf("%c\n",word[i]);
}
}
Now I want to apply the same technique for taking integer values from user
#include <stdio.h>
#include <cs50.h>
int main()
{
int scores[] = get_int("Enter score:");
int len = sizeof(scores)/sizeof(int);
for(int i = 0; i<len; i++)
{
printf("%i\n", scores[i]);
}
}
But the code doesn't get compiled and compiler says like this:
IntArray.c:5:9: error: array initializer must be an initializer list or wide string literal
int scores[] = get_int("Enter integer:");
^
1 error generated.
make: *** [<builtin>: IntArray] Error 1
I know we have a for loop for that solution, which goes like this: That's why I have to keep using for loop, which looks like this:
#include <stdio.h>
#include <cs50.h>
int main()
{
int scores[3];
for(int I = 0; I<3; I++)
{
score[I] = get_int("Enter score: ");
}
for(int j =0; j<3; j++)
{
printf("%I\n", scores[j]);
}
}
My question is can I define and assign the integer array at the same time?
I tried to define and assign an integer array at the same time as we do with the single string, but I couldn't.