RE: [Dev-C++] Dynamic Storage or Sentences
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <Mat...@Ve...> - 2005-04-27 16:59:04
|
Alright, what you need here is called the C++ vector class, which is an
endless array that reallocates itself when it runs out of space.
It's in the <vector> header.
Then you can go:
vector<string> v; v.reserve(numSentences); v.push_back(sentence);
It automatically sets its length. So v.length() can be used as a limit in
loops.
And it supports []. So you can for-loop through the contents.
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...]On Behalf Of Alexsandro
Meireles
Sent: Tuesday, April 26, 2005 8:33 PM
To: Dev C++
Subject: [Dev-C++] Dynamic Storage or Sentences
Hi, all!
I am trying to develop a program to store sentences in an array as you can
see below. I am following the steps: 1) Ask the user the number of
sentences; 2) Given the number of sentences, ask the user to enter Sentence
1 to Sentence n.
As can be seen in my code, if the user enter 10. I will get a char
array[10]. I was trying to store the sentences like: array[0] = "sentence
1"... array[n] ="sentence n", but that was stupid. Because I was trying to
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
pointer for each sentence generically for afterwards reach them????? With 10
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
course.
/* Storing sentences dynamically in a array. */
#include <stdio.h>
int numberOfSentences (void);
int max, count;
main()
{
int max = 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 = 0; count < max; count++) //Pay attention if the user
doesn't enter the number >ERROR
{
printf("Enter sentence # %d: ", count+1);
array[count] = 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. 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 = %d.", max);
printf("\n");
return max;
}
--
Alexsandro Meireles
___________________________________________________________________
The information contained in this message and any attachment may be
proprietary, confidential, and privileged or subject to the work
product doctrine and thus protected from disclosure. If the reader
of this message is not the intended recipient, or an employee or
agent responsible for delivering this message to the intended
recipient, you are hereby notified that any dissemination,
distribution or copying of this communication is strictly prohibited.
If you have received this communication in error, please notify me
immediately by replying to this message and deleting it and all
copies and backups thereof. Thank you.
|