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; Hello all, I am a newb. I am wondering how I could hold multiple values in one variable. Lets say ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    39

    Cool Holding multi values with one variable

    Hello all, I am a newb. I am wondering how I could hold multiple values in one variable. Lets say I have a program that asks for 200 peoples names. I do not want to create 200 variable for each person. This is what I was thinking but it over writes the previous entree of course.

    Code:
    char name[30];
    
    for (int i = 0; i < 256; i++)
    {
    	cout << "Enter your name\n";
    	cin >> name;
    } //End For Loop
    Then I was thinking of outputing each name to a file and reading all the information back in but want to know if there is another way to do this. I am missing something here I know it. If you have a better way to do this please let me know. Thanks

    Bitphire

    EDIT:
    In a class at the moment so cant test this but would a multi-array thing work??
    ex:
    name[30][30];
    Last edited by Bitphire; 09-27-2004 at 06:18 PM.

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    yes a 2D array waould do it; but it would not be [30][30] - think about it, you want to store 200 names of size 30....

    edit:: after readin lithlorien's reply, of course it wont work in your code...you need to figure out your for loop
    Last edited by axon; 09-27-2004 at 06:36 PM.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    39
    The 30 by 30 was just an example. Will the for loop work? I might not want it to loop 256 time and output the questions every time but I think it i will work. Am I correct? Also is there a better way than an 2D array

  4. #4
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Ok. I deleted my previous post and tried this again.

    Code:
    #include <iostream>
    #include <conio.h>
    
    int main()
    {
    	char name[200][30];
    
    	for (int i = 0; i < 10; i++)
    	{
    		std::cout << "Enter your name: ";
    		std::cin.getline(name[i], 30, '\n');
    	}
    	
    	for (int i = 0; i < 10; i++)
    	{
    		std::cout << name[i] << "\n";
    	}
    
    	getch();
    	
    	return(0);	
    }
    That'll do what you want. Feel free to remove the getch(), and the second for loop, as well as updating the "i < 10" to be what you want them to be. I tested it with 10 because I REALLY didn't want to type 200 names.. it works with 10.
    Last edited by Lithorien; 09-27-2004 at 07:28 PM.

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Not quite... It should be:
    Code:
    std::cin.getline(name[i], 30, '\n');

  6. #6
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    This is a C++ forum right??
    Why not using stl vector?? Like you said you're a newbie but, using it it's extremely easy...
    Programing your self a template class requires more experience...

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Heck, using the string class would be an even better start. It's too bad more books/instructors/tutorials don't start with more user friendly and less error-prone C++.

  8. #8
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by jlou
    Heck, using the string class would be an even better start. It's too bad more books/instructors/tutorials don't start with more user friendly and less error-prone C++.
    I think he's still learning C-basis...

  9. #9
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    Quote Originally Posted by jlou
    Heck, using the string class would be an even better start. It's too bad more books/instructors/tutorials don't start with more user friendly and less error-prone language.
    yes they are; most are converting to java

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    39
    Are yall saying use string or a string array? I have not messed a lot with strings but i am assuming that there is a string array. A regular string already is an array correct? As in you can do:

    string name;

    cout << name[3];

    this would output the 4th char in the string correct? So it would be better to use a string?

    string name[200]; ? is this right? This will give me 200 seperate strings? I think I am wrong but we will see . Let me know if i am totally off or somewhat close.

  11. #11
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,805
    If you just want an array of string do something like this
    Code:
    #include <iostream>
    #include <string>
    
    int main(void)
    {
      std::string name[4];
      for(int i = 0; i < 4; i++)
      {
        std::cout<<"Please Enter A Name"<<std::endl;
        std::cin>>name[i];    
      }
      for(int i = 0; i < 4; i++)
      {
        std::cout<<name[i]<<std::endl;
      }
      std::cin.ignore();
      std::cin.get();
      return 0;
    }
    Last edited by prog-bman; 09-27-2004 at 08:18 PM.
    Woop?

  12. #12
    Registered User
    Join Date
    Sep 2004
    Posts
    27
    string name[200] will give you an array of 200 strings. But I would really advise against that, unless you aren't too concerned of your memory usages. STL is a great tool, but it is made extremely flexible which does give the slightest of performance hits. I don't know what level you're at, so I wouldn't suggest pointers, but the 2D character array should work fine for what you need to do. Also, for your loops...

    Code:
    char[8][30] // Will give you 8, 30 character strings.**
    
    for (int i = 0; i < 8; i++)
    {
    	cout << "Enter your name\n";
    	cin >> name[i]; // Places it into the proper array**
                    cin.get() // Takes the newline away that dangles at the end of your input. Newlines are very troublesome.
    } //End For Loop
    If you want to get a little better, or are just paranoid about them newlines like myself, this works a bit better...

    while(cin.get() != \n) {} // <-- will loop through until no newlines are left.

    to print the strings out, just use cout << name[#]; where # is a value between 0 and 7.

    As for using vectors... I would definitely wait until you have a bit more coding experience. While they are easy to use... debugging them is a huge pain. Trust me.
    Last edited by Lifedragn; 09-27-2004 at 08:39 PM.

  13. #13
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,805
    >string name[200] will give you an array of 200 string pointers. But I would really advise against that. I don't know what level you're at, so I wouldn't suggest pointers, but the 2D character array should work fine for what you need to do.
    What are you talking about lifedragon? string is a class your just making an array of classes. You don't have to deal with pointers at all
    Woop?

  14. #14
    Registered User
    Join Date
    Sep 2004
    Posts
    27
    Ack, you're right. Pointers on the brain, hehe. Not pointers, but yes, it is a class. The thing is that the class does use dynamic allocation within. It seems a bit overpowered for what he needs. It is great if you need to do a lot of string manipulation though. I just wouldn't use an array of 200 of them for this instance. They are great things to look into though, so don't let my ramblings break you from them if you want to use them.

    Afterall... there are about 100 ways to skin a cat.

  15. #15
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,805
    Ya i know but IMO I think unless you got a pretty good foundation in arrays c-style strings are a pain to do(They were for me ).
    Woop?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 02-03-2005, 02: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, 12: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, 01:26 PM
  5. Parameter pass
    By Gades in forum C Programming
    Replies: 28
    Last Post: 11-20-2001, 01:08 PM

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