dynamic char array size

Hi,

I want to make char mystring the size of input. Compiler is claiming it can't do this without an explicit number. I don't understand my the formatting is the way it is.

1
2
3
4
char mystring[]; tried as well

cout<<endl<<"Take a card ?";
	char mystring[10];
	cin>>mystring;
	cout<<mystring;
	if (mystring[0] == 'y') {cout<<"-y-";}


output:
..\src\main.cpp:183:16: error: storage size of 'mystring' isn't known
Last edited on
If I'm correct you can only std::cin with strings.
Why don't you use a std::string instead? Then you would not have to know the size of the string when you create it.

http://www.cplusplus.com/reference/string/string/

Your output is messed up because when you put --- inside a code block it's split up into code and output sections.
Last edited on
...or you can scratch through the array

1
2
3
4
5
6
cout<<endl<<"Draw or Hold ?";
	char io [256];
	cin>>io;
	char * n_ptr = &io[0];
		cout<<"1st character<<"<<*n_ptr;
		cout<<*(n_ptr+1);


output: Dr if you put in Draw

Neat to manipulate the string as pointer array
Last edited on
Or as Peter said use std::string instead:
1
2
3
4
5
6
7
8
9
#include<iostream>
#include<string>

int main ()
{
	std::string str_input;
	std::cin>>str_input;
	std::cout<<str_input[0];
}
Last edited on
Topic archived. No new replies allowed.