Thread: A few questions in C++

  1. #16
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by ThWolf
    Edit:
    BTW, in the book I learned that in order to input a string, you need a char array. Never thought that you can do "string name", for example.
    Ick. Just ick. You can do character arrays (which are the C-style way of doing things) but I'd never use an array when a std::string would do the job better. And it does the job better most of the time. Yes, you should learn how to use character arrays. And learn that most of the time there are better options

    Arrays have a lot of pitfalls. Say you make a 20 character array for a person to enter their name. Joe User enters 30 characters. Best case scenario, he just crapped all over your stack. Worst case, he's able to execute arbitrary code by very carefully crafting his string.

    If you're serious about learning C++, I strongly encourage you to pay special attention to learning about a few areas, once you've got the basics down:

    * Classes. I know you don't know them now. Once you learn to use them you'll wonder how on earth you ever coded without them.

    *Standard Template Library (STL). std::string is one of very many powerful tools that you'll find here. You'll find vectors (like arrays, but can grow or shrink in size), lists (optimized for insertion/deletion speed, but you can't easily skip around within them), maps (optimized for very fast insertion/deletion/access of very large amounts of data), and many others.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  2. #17
    Registered User
    Join Date
    Aug 2006
    Location
    Israel
    Posts
    33
    Thanks for enlightening me a bit, Cat
    But your message raises two questions:
    1. I can declare a string by writing:
    Code:
    string <string variable name>
    instead of using an array to declare the string?
    2. You mentioned Vectors. I know that there are vectors in math. Is it the same in C++ and other programming languages?

  3. #18
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    1. Yes.
    2. A vector is the C++ class that is used for arrays. It is better if you just think of a C++ vector in relation to arrays and not in math terms.

    The string and vector classes are just the start of the very powerful toolset that C++ provides. I'd suggest getting a newer book that teaches that toolset (e.g. Accelerated C++), or finding online resources that you are comfortable with that do the same.

  4. #19
    Registered User
    Join Date
    Aug 2006
    Location
    Israel
    Posts
    33
    The book I read from is "C++ Primer plus (fourth edition)"
    it is from 2001.
    Do you think it ain't good enough or shall I really get another book?

  5. #20
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Sadly C++ lacks classes for vector/matrix arithmatic. std::vector is like an array except its size is not constant; you can increase or decrease it as the program runs.

    For example:

    Code:
    #include <vector>
    #incude <iostream>
    
    int main(int, char**){
    	// Make a vector which can store 5 ints, then fill it with 0 2 4 6 8
    	std::vector<int> myVector(5);
    	for (int i = 0; i < myVector.size(); i++){
    		myVector[i] = 2*i;
    	}
    
    	// Print it
    	std::cout << "Initial vector: " << std::endl;
    	for (int i = 0; i < myVector.size(); i++){
    		std::cout << "myVector[" << i << "] = " << myVector[i] << std::endl;
    	}
    
    	// This adds the number 10 to the end of the vector.  It increases the size of the vector
    	// from 5 to 6 to accomplish this task.
    	myVector.push_back(10);
    
    	// Print it again
    	std::cout << "Final vector: " << std::endl;
    	for (int i = 0; i < myVector.size(); i++){
    		std::cout << "myVector[" << i << "] = " << myVector[i] << std::endl;
    	}
    }
    In that example, it starts out as an array of 5 integers, but the size grew automatically to 6 when I added a new element using push_back.

    Oh, and I forgot to mention that is true about string as well -- it can grow or shrink to accomodate the data you put in it.

    All STL (standard template library) containers are nice like this. They also keep track of their size, so when you pass them to other functions you don't need to pass a pointer and the length separately; the vector knows its own length and will tell you if you ask it nicely
    Last edited by Cat; 08-21-2006 at 01:52 AM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #21
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    As to the book: Haven't read it. But the author is good, and the 4th ed. is supposed to be a really good improvement over the priors. Should be a very good one to learn from.

    Your book will definately teach you about C++ style std::string if you give it time They do need to teach you how to do things with char arrays, even if they're less useful later, as they still have their uses. There do come times when a char array is better than a string, so it's very important to know both and choose between them wisely. I think the book will do a good job on that.
    Last edited by Cat; 08-21-2006 at 02:17 AM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  7. #22
    Registered User
    Join Date
    Aug 2006
    Location
    Israel
    Posts
    33
    Wow, thanks for that Cat.
    I don't know anything about Vectors but I'll be sure to learn when I get to it (same with classes).

  8. #23
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> As to the book: Haven't read it. But the author is good, and the 4th ed. is supposed to be a really good improvement over the priors. Should be a very good one to learn from.

    I think you're thinking of C++ Primer by Lippman, Lajoie and Moo. ThWolf mentioned C++ Primer Plus, which is unrelated and doesn't have nearly as good of a reputation.

    >> Your book will definately teach you about C++ style std::string if you give it time.
    Switch to C++ Primer (without the Plus) or Accelerated C++.
    Last edited by Daved; 08-21-2006 at 10:56 AM.

  9. #24
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    I have C++ Primer, Wiley's Teach Yourself C++, and Accelerated C++, and they are all good books in my opinion, but unless you buy used that is 150$ worth of books. Course used you probably wont get the source code CD's

  10. #25
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466

    Read the FAQ

    Quote Originally Posted by ThWolf
    1. How can I tell the compiler to go to certain x,y coordinates? (I want to "print" the spacecraft in a simple letter, and it has to be in the bottom of the window, in the center).
    Unfortunately, this isn't as simple as it once was (i.e, Turbo C++ days)

    For Windows, check the FAQ (level 2) for the "How do I...gotoxy in a Windows console?"
    For Linux, I have no idea.

    Depending on your compiler, you may or may not have the "windows.h" header and the Windows SDK (Software Development Kit) installed on your computer. You NEED this to use the win32 API console functions that will allow you to change colors, console size, ect.

    Also note that if you choose to use the Windows API console functions you will lose ANY and ALL portability between different OS's.

    However, if you want to learn about using the windows API this is an EXCELLENT starting point.

    For questions 2 and 3 check the FAQ also, the FAQ is there for a reason.

    Also for questions 2 and 3, check the MSDN website, tons of good info there.

    Quote Originally Posted by ThWolf
    4. Most important, I want to make the screen size constant, becuase I don't want the user to change it. How do I do that?
    In order to do this, you first need to install the Windows SDK and compile some simple console programs in which you clear the screen, move the cursor around, and change colors. Once you can do this by learning from the FAQ, then you can easily check on the MSDN online library of console functions to find out how to change the console windows size.

    Basically, it involves a call to the SetConsoleScreenBufferSize() function which requires a couple structures you will need to learn about...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM