Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++ question

C++ question

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++algorithmsdata-structureshelp
20 Posts 11 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Sveta80

    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

    M Offline
    M Offline
    Marc Clifton
    wrote on last edited by
    #5

    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

    N 1 Reply Last reply
    0
    • M Marc Clifton

      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

      D Offline
      D Offline
      DavidNohejl
      wrote on last edited by
      #6

      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

      M 1 Reply Last reply
      0
      • D DavidNohejl

        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

        M Offline
        M Offline
        Marc Clifton
        wrote on last edited by
        #7

        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

        D 1 Reply Last reply
        0
        • M Marc Clifton

          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

          D Offline
          D Offline
          DavidNohejl
          wrote on last edited by
          #8

          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

          1 Reply Last reply
          0
          • S Sveta80

            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

            J Offline
            J Offline
            Jim Crafton
            wrote on last edited by
            #9

            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

            1 Reply Last reply
            0
            • M Marc Clifton

              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

              N Offline
              N Offline
              nde_plume
              wrote on last edited by
              #10

              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?

              M 1 Reply Last reply
              0
              • N nde_plume

                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?

                M Offline
                M Offline
                Marc Clifton
                wrote on last edited by
                #11

                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

                N 1 Reply Last reply
                0
                • S Sveta80

                  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

                  C Offline
                  C Offline
                  Christian Graus
                  wrote on last edited by
                  #12

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

                  1 Reply Last reply
                  0
                  • E El Corazon

                    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)

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #13

                    Sorry, too busy playing with my rubber troll :) The tigress is here :-D

                    B 1 Reply Last reply
                    0
                    • M Marc Clifton

                      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

                      N Offline
                      N Offline
                      nde_plume
                      wrote on last edited by
                      #14

                      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.

                      M 1 Reply Last reply
                      0
                      • N nde_plume

                        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.

                        M Offline
                        M Offline
                        Marc Clifton
                        wrote on last edited by
                        #15

                        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

                        1 Reply Last reply
                        0
                        • S Sveta80

                          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

                          D Offline
                          D Offline
                          David Crow
                          wrote on last edited by
                          #16

                          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 to size 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

                          A S 2 Replies Last reply
                          0
                          • L Lost User

                            Sorry, too busy playing with my rubber troll :) The tigress is here :-D

                            B Offline
                            B Offline
                            Blake Miller
                            wrote on last edited by
                            #17

                            Ummmmmmm. I am not even going to hazard a guess what that means ... :~

                            1 Reply Last reply
                            0
                            • D David Crow

                              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 to size 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

                              A Offline
                              A Offline
                              Anonymous
                              wrote on last edited by
                              #18

                              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.

                              1 Reply Last reply
                              0
                              • D David Crow

                                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 to size 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

                                S Offline
                                S Offline
                                Sveta80
                                wrote on last edited by
                                #19

                                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.

                                D 1 Reply Last reply
                                0
                                • S Sveta80

                                  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.

                                  D Offline
                                  D Offline
                                  David Crow
                                  wrote on last edited by
                                  #20

                                  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

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups