is there anyway to store a string (which is imputted from the user)
into an array?
is there anyway to store a string (which is imputted from the user)
into an array?
For next time, please read a tutorial on the C/C++ language before posting questions like this (this is pretty basic stuff).Code:char* cstyle = new char[50]; cin.getline(cstyle, 50);
Last edited by homeyg; 03-10-2006 at 08:43 PM.
This is c++ anyways, so why not use string?
If for whatever reason you needed a c array you can use the c_str() method
He said put a string into an array, not put a string into a string..
Plus the string class is for morons who can't properly handle real C style strings.
Maybe he wanted something like
Not sure if he wanted the array dynamical though.Code:string array[5]; for (int i = 0; i < 5; i++) getline(cin, array[i], '\n');
This sounds like "how do I get a string from the user?". FAQ: FAQ > How do I... (Level 1) > Obtain a string from the user (C++)is there anyway to store a string (which is imputted from the user)
into an array?
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
I really hope you're joking. I'm not even gonna go into why that's stupid.Originally Posted by homeyg
He also said store a string in C dynamically not C++, so no std::string and no new.
man that takes me back! That's the C way to do it.Code:#incude <stdio.h> int main() { int len = 0; char* pString = NULL; printf("How long is a piece of string?"); scanf("%d", &len); pString = malloc(len); printf("enter the string"); scanf("%s", pString); }
if you ARE using C++, that's about the worst way to do it.
Code:#include <iostream> #include <string> using namespace std; int main() { string str; cout << "enter a string: "; cin >> str }
Last edited by ChaosEngine; 03-11-2006 at 05:57 PM. Reason: bloody C variable declaration rules!!
"I saw a sign that said 'Drink Canada Dry', so I started"
-- Brendan Behan
Free Compiler: Visual C++ 2005 Express
If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?
He said put a string into an array, not put a string into a string..Well, looky there: a string is an array. Imagine that.Code:#include <iostream> #include <string> using namespace std; int main() { string myStr; cout<<"Enter a string of any length: "; getline(cin, myStr); cout<<"The characters you entered were:\n"; for(int i = 0; i < myStr.length(); i++) { cout<<myStr[i]<<endl; } return 0; }
...but it was posted in a C++ forum. std::string and new until the sun don't shine!He also said store a string in C dynamically not C++, so no std::string and no new.
If you're using C, that's STILL the worst way to do it. What happens if I claim my string is shorter than what I actually enter?Originally Posted by ChaosEngine
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Originally Posted by 7stud
strings are for the lazy >.>
Originally Posted by homeyg*........es himself*Originally Posted by JeremyCAFE
Rebels without a cause... I suppose you shouldn't use the ANSI strlen(), strcpy(), strcmp(), or any other predefined functions for strings either. They're just for lazy morons.
Last edited by SlyMaelstrom; 03-11-2006 at 09:42 PM.
Sent from my iPadŽ
true. to be honest it's been a long time since I've done any C (actually had to look up scanf and malloc ),Originally Posted by CornedBee
"I saw a sign that said 'Drink Canada Dry', so I started"
-- Brendan Behan
Free Compiler: Visual C++ 2005 Express
If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?
Crazy lazy:strings are for the lazy >.>
Code:#include <iostream> #include <string> #include <algorithm> using namespace std; int main() { string myStr; cout<<"Enter a string of any length: "; getline(cin, myStr); cout<<"The characters you entered were:\n"; copy(myStr.begin(), myStr.end(), ostream_iterator<char>(cout, "\n")); return 0; }
Laziness is a virtue.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law