Thread: Holding multi values with one variable

1. 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];

2. 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

3. 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. 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.

5. Not quite... It should be:
Code:
`std::cin.getline(name[i], 30, '\n');`

6. 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. 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. 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. 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

10. 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. 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;
}```

12. 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.

13. >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

14. 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. 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 ).

Popular pages Recent additions