Thread: Holding multi values with one variable

  1. #16
    Registered User
    Join Date
    Sep 2004
    Posts
    39
    Well I will probably end up using either the 2D array or the string[] but if someone doesnt mind just typing of a very simple program with pointer that will do what yall say it can do then that would be great. Thanks for all the feedback and the quickness of them .

  2. #17
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Here is Lithorien's example except with strings:
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
    	std::string names[200];
    
    	for (int i = 0; i < 200; i++)
    	{
    		std::cout << "Enter your name: ";
    		std::getline(std::cin, name[i]);
    	}
    	
    	for (int i = 0; i < 200; i++)
    	{
    		std::cout << name[i] << "\n";
    	}
    
    	return(0);	
    }
    If you are doing this for a class, then I don't know whether you should be using strings if your instructor has not yet taught them. However, if you're teaching yourself, or your instructor doesn't mind, I'd recommend learning and using the string class instead of character arrays. They are easier for a new programmer to understand, in my opinion, and harder to screw up.

    I have to disagree with Lifedragn. You shouldn't have to worry about memory usage with such a small app, especially if you're a beginner and especially if it's only 200 strings. That's nothing. Chances are, if you are a beginner, you'd benefit from the string class much more than you'd ever have to worry about memory efficiency. If you are advanced enough to have to worry about memory efficiency, then you should be able to use a custom allocator or find some other solution that allows you to use string class and its benefits. I'm not saying that there is never a reason to use simple char arrays in C++, but generally strings are better.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 02-03-2005, 03:33 AM
  2. Problem with a char variable set as a letter
    By 7smurfs in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2004, 01:25 PM
  3. Beginner question
    By Tride in forum C Programming
    Replies: 30
    Last Post: 05-24-2003, 08:36 AM
  4. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM
  5. Parameter pass
    By Gades in forum C Programming
    Replies: 28
    Last Post: 11-20-2001, 02:08 PM