Im trying to cin >> a string consisted of numbers and letters such as 3j4583 and storing each separate char to a separate index of an array
can u help provide the proper syntax?
Technically a string is an array? Would this be ok:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[]) {
string std;
cout << "Please enter your string: ";
cin >> std;
for(int i=0; (i < std.length()); i++)
{
cout << std[i] << endl;
}
return 0;
}
- What do you plan to do with it?
=)
If it's only for read only purpose, you can use your original string itself as an array of characters terminated with '\0'.
string myString = "hello world";
const char *myArray;
myArray = myString.c_str();
Otherwise create a copy and do read/write
string myString = "hello world";
const char *myArray;
myArray = myString.c_str();
char *copy = new char[strlen(myArray)];
strcpy(copy, myArray);
//use your copy
delete copy;
possibley im trying to read in 2-15chars in a string(ex: "2ta3" or "4528tj" and store them in seperare index's of an array
#include <iostream>
#include <string>
using namespace std;
int main()
{
int score = 0;
int lastcard = 0;
int numCard = 0;
string hand = "";
char cards = new char[15];
cout << "How many cards do you have?: " << endl;
cin >> numCard;
cout << "What are you cards?: " << endl;
cin >> hand;
for(int i = 0;i<numCard;i++)
{
cards[i].assign(hand, i, (i+1);
}
cout << cards[2];
return 0;
}
there no files, its all interactive
possibley im trying to read in 2-15chars in a string(ex: "2ta3" or "4528tj" and store them in seperare index's of an array
In my previous post, my second suggestion exactly gives the solution.
where do i set the parameter to read in 1 char for each index?
where do i set the parameter to read in 1 char for each index?
Can you be a bit more clear? I believe I gave the solution to what you have asked "Trying to store inputted string as individual chars to individual index's of Array"
What is blocking you?
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.