Start New Discussion within our Software Development Community

iam trying to extract a individual character. for example, during run time the user enters 110111 as input. can any 1 help me with the code to extract these input and store them in an array as individual elements.

If it's an std::string, do mystr.c_str() to get a character array, to access an individual character, do mystr[[B]charIndex[/B]]. This is all over the web, try searching first.

You can just use [] operator to get desired char, converting to char table (c_str method) is pointless.

Fairly new at C++ but I believe something like this will work. Comments or improvements appreciated.

int main()
	cout << "Please enter some text below:\n";
	//string to hold user input
	string userInput;
	//user inputs info
	cin >> userInput;
	//create a char array of desired length.
        char chrArray[5];
	//loop through the string
	for(int index = 0; index < (int)userInput.length(); index++)
		//if the userinput equals something 
		if(userInput[index] == '1')
			 chrArray[index] = userInput[index];
			 //just pausing so console will stay up
	return 0;
This question has already been answered. Start a new discussion instead.