C++ question
-
Hi guys! I see that most of people here are very smart programmer so my question may seem like a silly one but keep in mind that I am just started to learn C++. I am writing a sorting program using dynamically allocated array. I know how to write a program when the size of the array is known. Well, this time I don't know the size of it. My program suppose to read the positive numbers from the keyboard and it need to quit only when i hit ^C. How do you declare your array and populate it when you don't know the size?! I know that I should use something like this: int *pointer; int size; pointer = new int [size]; but when I go to my for loop what do I say my size should be? Any help would be greatly appriciated!!!! Sveta -- modified at 14:36 Sunday 11th September, 2005
Sveta80 wrote: I know that I should use something like this: Actually, you should be using C# so you can write: ArrayList myChars=new ArrayList(); ... myChars.Add(myKeyboardChar); Isn't that so much simpler than pointers and constructors and destructors and malloc and realloc and STL and...(errr, oops!). :-D Marc My website Traceract Understanding Simple Data Binding Diary Of A CEO - Preface
-
Trollslayer wrote: The "No programming questions please." wasn't big enough... Actually, it appears to have disappeared. Or at least, I can't see it. Hmmm.... Marc My website Traceract Understanding Simple Data Binding Diary Of A CEO - Preface
Marc Clifton wrote: Trollslayer wrote: The "No programming questions please." wasn't big enough... Actually, it appears to have disappeared. Or at least, I can't see it. Hmmm.... Yes, in fact it is "Please do NOT post programming questions or ads to this forum.". Maybe too long and confusing sentence? :) Never forget: "Stay kul and happy" (I.A.)
David's thoughts / dnhsoftware.org / MyHTMLTidy -
Marc Clifton wrote: Trollslayer wrote: The "No programming questions please." wasn't big enough... Actually, it appears to have disappeared. Or at least, I can't see it. Hmmm.... Yes, in fact it is "Please do NOT post programming questions or ads to this forum.". Maybe too long and confusing sentence? :) Never forget: "Stay kul and happy" (I.A.)
David's thoughts / dnhsoftware.org / MyHTMLTidydnh wrote: Yes, in fact it is "Please do NOT post programming questions or ads to this forum.". Maybe too long and confusing sentence? Oh, I see it now! Posting Guideline: The Lounge is rated PG. If you're about to post something you wouldn't want your kid sister to read then don't post it. * Do not post programming questions (use the programming forums for that) and please don't post ads. I stopped reading that paragraph where I placed the "*", thinking the rest of the paragraph was more rambling about PG and kid sisters. It used to be more obvious, IIRC. Oh well. I'm blind. I admit it. :) Marc My website Traceract Understanding Simple Data Binding Diary Of A CEO - Preface
-
dnh wrote: Yes, in fact it is "Please do NOT post programming questions or ads to this forum.". Maybe too long and confusing sentence? Oh, I see it now! Posting Guideline: The Lounge is rated PG. If you're about to post something you wouldn't want your kid sister to read then don't post it. * Do not post programming questions (use the programming forums for that) and please don't post ads. I stopped reading that paragraph where I placed the "*", thinking the rest of the paragraph was more rambling about PG and kid sisters. It used to be more obvious, IIRC. Oh well. I'm blind. I admit it. :) Marc My website Traceract Understanding Simple Data Binding Diary Of A CEO - Preface
Marc Clifton wrote: I stopped reading that paragraph where I placed the "*", thinking the rest of the paragraph was more rambling about PG and kid sisters. hmm maybe that's the reason why people post programming questions? Never forget: "Stay kul and happy" (I.A.)
David's thoughts / dnhsoftware.org / MyHTMLTidy -
Hi guys! I see that most of people here are very smart programmer so my question may seem like a silly one but keep in mind that I am just started to learn C++. I am writing a sorting program using dynamically allocated array. I know how to write a program when the size of the array is known. Well, this time I don't know the size of it. My program suppose to read the positive numbers from the keyboard and it need to quit only when i hit ^C. How do you declare your array and populate it when you don't know the size?! I know that I should use something like this: int *pointer; int size; pointer = new int [size]; but when I go to my for loop what do I say my size should be? Any help would be greatly appriciated!!!! Sveta -- modified at 14:36 Sunday 11th September, 2005
Hi, thanks for posting your interesting C++ question in Lounge! I'll be glad to help out! You'll probably be looking for something like this:
while ( get_keybrd_inpt()->is_num_pos() ? true : false >> 0 ) {
int s = 1234 * (2 >> 8 / 3 % 4 <<1) * 3 ? 200: 7;
int&* p = new int[s];
for ( (*p.->len()++).ossfet+1; ; ) {
s = p->query_length_ctr() + 1 * (*p->offset);
p = new (int)(p)[s];
}
}
else ( break; ) { set_jmp(120); asm{ _int 3;} exit( if_ctl_C_ht() ); }That should do the trick. Let us know how this works for you! ¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! -- modified at 16:08 Sunday 11th September, 2005
-
Sveta80 wrote: I know that I should use something like this: Actually, you should be using C# so you can write: ArrayList myChars=new ArrayList(); ... myChars.Add(myKeyboardChar); Isn't that so much simpler than pointers and constructors and destructors and malloc and realloc and STL and...(errr, oops!). :-D Marc My website Traceract Understanding Simple Data Binding Diary Of A CEO - Preface
Or alternatively in C++ vector myChars; ... myChars.push_back(myKeyboardChar); I can see how much more complex the C++ solution is over C# here. :-) It is a fact that there are almost not circumstances when it is necessary to use new and delete in C++ anymore, it is almost always better to use a Standard library container, which handles all that stuff for you. However, Microsoft's secret plan to replace C++ with C# is that they make these containers very hard to use within the debugger. Debugging support of the STL really blows in VS 2003. Has anybody tried it out in VS 2005?
-
Or alternatively in C++ vector myChars; ... myChars.push_back(myKeyboardChar); I can see how much more complex the C++ solution is over C# here. :-) It is a fact that there are almost not circumstances when it is necessary to use new and delete in C++ anymore, it is almost always better to use a Standard library container, which handles all that stuff for you. However, Microsoft's secret plan to replace C++ with C# is that they make these containers very hard to use within the debugger. Debugging support of the STL really blows in VS 2003. Has anybody tried it out in VS 2005?
nde_plume wrote: It is a fact that there are almost not circumstances when it is necessary to use new and delete in C++ anymore, Oh, I don't know about that. There's plenty of times when I need a countainer to maintain a reference to an object that isn't constructed on the stack. nde_plume wrote: However, Microsoft's secret plan to replace C++ with C# is that they make these containers very hard to use within the debugger. Not really. STL has been a PITA to debug since the days when I was using Sun's STL. Drilling into a collection in STL is like bumbling around in a universe consisting of 3.2 dimensions. If anything, maybe Microsoft has made is easier, not harder, to debug. Marc My website Traceract Understanding Simple Data Binding Diary Of A CEO - Preface
-
Hi guys! I see that most of people here are very smart programmer so my question may seem like a silly one but keep in mind that I am just started to learn C++. I am writing a sorting program using dynamically allocated array. I know how to write a program when the size of the array is known. Well, this time I don't know the size of it. My program suppose to read the positive numbers from the keyboard and it need to quit only when i hit ^C. How do you declare your array and populate it when you don't know the size?! I know that I should use something like this: int *pointer; int size; pointer = new int [size]; but when I go to my for loop what do I say my size should be? Any help would be greatly appriciated!!!! Sveta -- modified at 14:36 Sunday 11th September, 2005
Not sure at the end of the day if you feel your question has been answered. The answer is std::vector. Put #include at the top of your code, and then using std::vector just below. Then in your code you define a vector like this vector = name; For example #include #include using std::vector; using std::string; int main() { vector vecInt; vector vecString; vecInt.push_back(1); vecInt.push_back(2341); vecInt.push_back(341); vecInt.push_back(176); vecString.push_back("fsdfsdfdf"); vecString.push_back("fgkkf"); vecString.push_back("45j4"); // Now this is how we use it vector::iterator begin = vecInt.begin(); int total = 0; for(;begin != vecInt.end();++it) { total += *begin; // You need to dereference the iterator to get the value. } // You could also use the algorithm in to do stuff like this. } Christian Graus - Microsoft MVP - C++
-
Trollslayer wrote: You might care to post this in the Visual C++[^] forum. what? no axe swinging? I guess I need to go back to playing BloodRayne 2, no excitement here.... _________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)
Sorry, too busy playing with my rubber troll :) The tigress is here :-D
-
nde_plume wrote: It is a fact that there are almost not circumstances when it is necessary to use new and delete in C++ anymore, Oh, I don't know about that. There's plenty of times when I need a countainer to maintain a reference to an object that isn't constructed on the stack. nde_plume wrote: However, Microsoft's secret plan to replace C++ with C# is that they make these containers very hard to use within the debugger. Not really. STL has been a PITA to debug since the days when I was using Sun's STL. Drilling into a collection in STL is like bumbling around in a universe consisting of 3.2 dimensions. If anything, maybe Microsoft has made is easier, not harder, to debug. Marc My website Traceract Understanding Simple Data Binding Diary Of A CEO - Preface
However, Microsoft's secret plan to replace C++ with C# is that they make these containers very hard to use within the debugger You really think it isn't possible to present containers in an easier way in the debugger? Heck, if you are using a vector you can hack the debugger to display the contents by typing this in the watch window: my_vector._Myfirst,10 shows the first ten items in the vector in a fairly nice way. There is no reason at all why the debugger can't do that itself. It is much harder with tree structures like maps and sets to view by hand in the debugger (though you can walk throught it, so the data is certainly renderable) it is just that MS didn't think it was very important.
-
However, Microsoft's secret plan to replace C++ with C# is that they make these containers very hard to use within the debugger You really think it isn't possible to present containers in an easier way in the debugger? Heck, if you are using a vector you can hack the debugger to display the contents by typing this in the watch window: my_vector._Myfirst,10 shows the first ten items in the vector in a fairly nice way. There is no reason at all why the debugger can't do that itself. It is much harder with tree structures like maps and sets to view by hand in the debugger (though you can walk throught it, so the data is certainly renderable) it is just that MS didn't think it was very important.
nde_plume wrote: You really think it isn't possible to present containers in an easier way in the debugger? Not at all. I think it should be quite possible. And since Microsoft provides both the STL and the debugger, I don't see why the debugger couldn't be more STL aware, as you pointed out. Marc My website Traceract Understanding Simple Data Binding Diary Of A CEO - Preface
-
Hi guys! I see that most of people here are very smart programmer so my question may seem like a silly one but keep in mind that I am just started to learn C++. I am writing a sorting program using dynamically allocated array. I know how to write a program when the size of the array is known. Well, this time I don't know the size of it. My program suppose to read the positive numbers from the keyboard and it need to quit only when i hit ^C. How do you declare your array and populate it when you don't know the size?! I know that I should use something like this: int *pointer; int size; pointer = new int [size]; but when I go to my for loop what do I say my size should be? Any help would be greatly appriciated!!!! Sveta -- modified at 14:36 Sunday 11th September, 2005
Sveta80 wrote: pointer = new int [size]; but when I go to my for loop what do I say my size should be? You would loop from
0
tosize
times. For example:int size = 10;
int *pointer = new int[size];
for (int x = 0; x < size; x++)
{
cout << "Enter number " << x << ": ";
cin >> pointer[x];
}
// now sort them
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
-
Sorry, too busy playing with my rubber troll :) The tigress is here :-D
Ummmmmmm. I am not even going to hazard a guess what that means ... :~
-
Sveta80 wrote: pointer = new int [size]; but when I go to my for loop what do I say my size should be? You would loop from
0
tosize
times. For example:int size = 10;
int *pointer = new int[size];
for (int x = 0; x < size; x++)
{
cout << "Enter number " << x << ": ";
cin >> pointer[x];
}
// now sort them
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
But the problem is that I don't know the actual size of my array. My size depends on the user input and I don't know in advance how many numbers my user is going to enter. User can enter 10, 20, 100 numbers. After the user finished entering the numbers, my code should read through them and whenever the input number is negative, the code should start sort the numbers it already got (except the negative number) and prints the orders.
-
Sveta80 wrote: pointer = new int [size]; but when I go to my for loop what do I say my size should be? You would loop from
0
tosize
times. For example:int size = 10;
int *pointer = new int[size];
for (int x = 0; x < size; x++)
{
cout << "Enter number " << x << ": ";
cin >> pointer[x];
}
// now sort them
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
But the problem is that I don't know the actual size of my array. My size depends on the user input and I don't know in advance how many numbers my user is going to enter. User can enter 10, 20, 100 numbers. After the user finished entering the numbers, my code should read through them and whenever the input number is negative, the code should start sort the numbers it already got (except the negative number) and prints the orders.
-
But the problem is that I don't know the actual size of my array. My size depends on the user input and I don't know in advance how many numbers my user is going to enter. User can enter 10, 20, 100 numbers. After the user finished entering the numbers, my code should read through them and whenever the input number is negative, the code should start sort the numbers it already got (except the negative number) and prints the orders.
In that case, one of the other suggestions is more in line with what you need. You can, however, ask the user how many numbers s/he will be entering and allocate an array that big. Good luck.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb