Hey guys.

In C++, if I wanted to read strings into an array I would do this:

std::string theWord = "";

for ( int i = 0; i <= SIZE; i++ ) {
   std::getline ( std::cin, stringArray[ i ] );
}

In C, I am having trouble comming up with a method that works
as simply.

I have come up with this:

nt i;

	char data[ ARRAY_SIZE ] = { '\0' },
		 theWord[ 80 ];

 	puts("Enter ten strings:");

	for ( i = 0; i < ARRAY_SIZE; i++ ) {
		fgets( theWord, sizeof( theWord ), stdin );
		data[ i ] += theWord;
	}

But I am getting an error. I know using gets() is bad so I am trying to use fgets() like above. Am I on the right track?

Actually in C++ it's just istream.getline(array_name, block_size); , or with strings getline(istream, string_name); .

Also don't you need strncat() to append data? You would also probably need a 2D array to hold multiple strings.

data[ i ] += theWord; Remember, strings are not variables in C, but a sequence of chars terminated by the '\0'. Assignment can not be achieved directly, you have to use a piece of code that would assign one char at a time respectively.
strcpy(), strncpy(), strcat() and strncat() are standard functions that will handle some of these tasks. Make sure you include string.h header file. fgets( theWord, sizeof( theWord ), stdin ); Do not assume that fgets() will always be able to read successfully.
Only if..

if (fgets(theWord, sizeof theWord, stdin) != NULL)
    /* then copy or concatenate the read string */

What i understood from your question is that you are trying to read multiple strings in a single array..Then i think you need to take two dimensional array...see the code below:

#include<stdio.h>

int main()
{
    char  arr[5][20];
    int i;
    for (i=0;i<5;i++)
    {
        printf("\n Enter the string :");
        fgets(arr[i],20,stdin);
    }

    for(i=0;i<5;i++)
    {
        printf("\n %s \n",arr[i]);
    }

    return 0;
}

Hope this helps you out...

Please read the rules, me_ansh, and you'll find what you've done wrong.

Fine 'MosaicFuneral'......Thanks for the alert...i'll keep this thing in mind

What are the rules Mosaic Funeral?

Me ansh, your code was actually exactly what I happened to be looking for, thank you!

hey cpp addict check this out. It is same as me_ansh but i have done some different type of coding. hope this will help u....
#include<stdio.h>
#include<conio.h>
void main()
{
char n[30];
int i=0,len=0;
clrscr();
printf("Enter the string: ");
scanf("%s",n);
while (n[i++]!='\0')
len++;

for(i=0;i<len;i++)
printf("\n%c",n[i]);
getch();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.