| | |
loop that creates strings?
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Is there a way to make a loop that creates strings? I ask the user how many players there are, and then the loop makes that many strings in order to get all of their names... Something like this, (but not in normal english...):
And then, if that's possible, how could I access each one individually?
C++ Syntax (Toggle Plain Text)
cout << "How many players? " << endl; cin >> numOfPlayers; for(unsigned int i=0 ; i<numOfPlayers ; i++) { /* create new string */ }
you will want to create an array, or vector, of strings.
>>how could I access each one individually?
From the above vector: if n is an integer between 0 and the number of names in the array
There is another way to access individual members of a vector - using an interator -- but its a little more complex.
C++ Syntax (Toggle Plain Text)
vector<string> names; <snip> for(unsigned int i=0 ; i<numOfPlayers ; i++) { string name; cout << "Enter name #" << i << "\n"; getline(name, cin); names.push_back(name); }
>>how could I access each one individually?
From the above vector: if n is an integer between 0 and the number of names in the array
C++ Syntax (Toggle Plain Text)
string thisName = names[n];
There is another way to access individual members of a vector - using an interator -- but its a little more complex.
Last edited by Ancient Dragon; Jun 6th, 2007 at 11:27 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- Dimensioning Strings (C#)
- problem with MATLAB loop (Legacy and Other Languages)
- judtifiying text (C)
- How to improve program with string compairing? (C++)
- can't proceed from here. (C++)
- C++ Address Book (C++)
Other Threads in the C++ Forum
- Previous Thread: problem dealing with constructor
- Next Thread: noob question, answer probably pretty easy, i just don't know where to start
Views: 719 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






