Undefined String Length

Hey guys, I have a question about string lengths. Is it possible to set a string length such as "1 to 100?" To allow any amount of characters to be set as the variable?

1
2
3
4
5
6
7
int randomint;
randomint=rand()%20+1;
char string[5];
if(randomstringint==1-10){
string="This";}
else{
string="These";}


Whenever I run this, I'll get an error at "This" saying that it doesn't use all the characters that string has assigned to it. Is there a simple way to fix this other than using just a space as a place holder?

I'm a bit new here, so if you need any other info, I won't hesitate to provide it.
You might want to look into vectors depending on what you are trying to achieve. A vector can be any length. I'm new as well though.
Last edited on
You could use a string instead of a character array.

1
2
3
4
5
6
7
int randomint;
randomint = rand() % 20 + 1;
std::string str;
if(randomstringint == 1 -10) 
     str = "This";
else
     str = "These";


http://www.cplusplus.com/reference/string/string/string/
Last edited on
Awesome, the string worked. Thanks! Is there a way that I could plug that into a printf statement though? Such as:

 
printf("%s", str); 


Obviously %s would be different if it's different from inserting an array. Or if that's wrong, I'm sorely mistaken on how to put arrays into printfs.
There is no need to use C functions when you are writing a C++ application. I suggest you use the standard output stream object std::cout to output the string object.

 
std::cout << str << '\n';

http://www.cplusplus.com/reference/iostream/ostream/
Last edited on
Well, you've declared a buffer of 5 chars

char string[5];

and you can't assign the address of a string to a buffer variable.

string = "Hello"; // :( does not compile

only point at it

const char* msg = "Hello"; // literals should always be const

(the char* pointer variable points at the memory where Hello is stored)

What you can do is copy into or print into, as you aready kind of realised. You use sprintf rather than printf

1
2
char buffer[256] = ""; // init buffer to zeros
sprintf(buffer, ""%s", str);  


With strcpy, you're orig code becomes

1
2
3
4
5
6
7
int randomint;
randomint=rand()%20+1;
char string[6]; // added an extra elem so long enough for These + null term!
if(randomstringint==1-10){ // <= this doesn't look right
    strcpy(string, "This";} // string is prob a name to avoid, due to std::string
else{
    strcpy(string, "These";}


Note that "These" needs 6 chars of buffer space. The extra one is for the null terminator.

Andy

PS If by this

if(randomstringint==1-10)

you mean the value is between 1 and 10, then you need

if(randomstringint >= 1 && randomstringint <= 10)

PPS Also see ostringstream
http://www.cplusplus.com/reference/iostream/ostringstream/

char buffer[256] = "";
sprintf(buffer, "%s = %d", name, age);

maps to this

1
2
3
4
string str;
ostringstream oss;
oss << name << " =" << age;
str = oss.str();


Last edited on
Topic archived. No new replies allowed.