[Dev-C++] Dynamic Storage or Sentences
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Alexsandro M. <mei...@gm...> - 2005-04-27 03:33:13
|
Hi, all!
I am trying to develop a program to store sentences in an array as you can=
=20
see below. I am following the steps: 1) Ask the user the number of=20
sentences; 2) Given the number of sentences, ask the user to enter Sentence=
=20
1 to Sentence n.
As can be seen in my code, if the user enter 10. I will get a char=20
array[10]. I was trying to store the sentences like: array[0] =3D "sentence=
=20
1"... array[n] =3D"sentence n", but that was stupid. Because I was trying t=
o=20
put a sentence in the place of a character.
If I use a pointer to the array maybe I can solve this, but how can a put =
a=20
pointer for each sentence generically for afterwards reach them????? With 1=
0=20
sentences I will need 10 pointers for 10 arrays!!!!
Any suggestions!
Best,
Alex.
PS: You can perfectly compile the source but it is not working properly of=
=20
course.
=20
/* Storing sentences dynamically in a array. */
#include <stdio.h>
int numberOfSentences (void);
int max, count;
main()
{
int max =3D numberOfSentences(); //GAMBIARRA TO PUT MAX INTO THE ARRAY
char *array[max];
printf("The number of sentences is %d.\n", max);
/*Getting sentences from the keyboard*/
for (count =3D 0; count < max; count++) //Pay attention if the user doesn't=
=20
enter the number >ERROR
{
printf("Enter sentence # %d: ", count+1);
array[count] =3D gets(*array); //This line is working BIZARRE
/*Print the sentences on screen*/
printf("Sentence %d is %s\n", count+1, array[count]);
system("pause"); //ONLY ACCEPTS A SENTENCE WITH AT MOST 10 CHARACTERS.=20
OTHERWISE IT CLOSES.
}
system("pause");
system("pause");
return 0;
}
/* Input number of sentences */
int numberOfSentences (void)
{
printf("Enter the number of sentences: ");
scanf("%d", &max);
//printf("max =3D %d.", max);
printf("\n");
return max;
}
--=20
Alexsandro Meireles
|