954,479 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Read strings into array

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?

Cpp_Addict
Newbie Poster
4 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

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.

MosaicFuneral
Posting Virtuoso
1,691 posts since Nov 2008
Reputation Points: 888
Solved Threads: 116
 

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 */
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

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

me_ansh
Light Poster
44 posts since Jan 2009
Reputation Points: 13
Solved Threads: 5
 

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

MosaicFuneral
Posting Virtuoso
1,691 posts since Nov 2008
Reputation Points: 888
Solved Threads: 116
 

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

me_ansh
Light Poster
44 posts since Jan 2009
Reputation Points: 13
Solved Threads: 5
 

What are the rules Mosaic Funeral?

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

mboyco
Newbie Poster
1 post since Jul 2010
Reputation Points: 10
Solved Threads: 0
 
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();
}
urjapandya
Newbie Poster
4 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You