Holding multi values with one variable

This is a discussion on Holding multi values with one variable within the C++ Programming forums, part of the General Programming Boards category; Well I will probably end up using either the 2D array or the string[] but if someone doesnt mind just ...

  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,088
    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.

Page 2 of 2 FirstFirst 12
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, 09: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21