Login [Register]
Don't have an account? Register now to chat, post, use our tools, and much more.
Ok, first I was wondering whether in c++, using c++ strings can I create a list of these strings?

So something like


Code:
string words[10];


to create a list that can hold 10 different strings?

Or do I have to use a struct


Code:
struct word {
string strings;
} the_string[10];


(I can't seem to remember the syntax, but you get what I mean Wink )


And now the basic question
----
Is there a way I can take a value from, lets say, x and get it into a string?

I can get it from a string into a normal variable (although I think there is a better way than this)


Code:
sub(str1,length(str1)-1,2)->Y1
Y1->X

but I have tried everything + expr but nothing works >.<
For a list of strings, use the vector class (google for c++ vector) - vectors are a dynamic list that allows you to add/remove with minimal hassle

it is something like this


Code:
#include <vector>

vector<string> my_string_list;

my_string_list.append("some new string");


of course, you can access it safely using either the .at() OR you can just access it using [] (but no range checking)
thanks for the answer, could someone please answer the basic question?

Quote:
And now the basic question
----
Is there a way I can take a value from, lets say, x and get it into a string?

I can get it from a string into a normal variable (although I think there is a better way than this)


Code:
sub(str1,length(str1)-1,2)->Y1
Y1->X



but I have tried everything + expr but nothing works >.<
to answer your basic question to get a number into a string just do

"5->str1
I mean like this

lets say I generate a random number 1-100 and store it to x
how can I add it to str1?

I have tried stuff like expr("x") to get it into a string but nothing works
if X = 1
"1->Str1
if X = 2
"2 ->Str2

so on and so forth.
I can't do that 10-99 without making the program go way out of proportion. I could rewrite it to get the string content at beginning and use it later for the width... oh well

Anyone have any ideas?

there must be some way, I mean you can get them out of strings easily.
the only other thing i can think of is instead of using a variable use a list and do something like:


Code:

"{"+ans+"0->str1
You could do something like


Code:
"" -> str1
repeat ipart(x
x/10->x
fpart(x
if (ans = 1
"1" + str1 -> str1
if (ans = 2
"2" + str1 -> str1
...etc...
end
Kllrnohj wrote:
You could do something like


Code:
"" -> str1
repeat ipart(x
x/10->x
fpart(x
if (ans = 1
"1" + str1 -> str1
if (ans = 2
"2" + str1 -> str1
...etc...
end
I know I'm necroposting here; I just wanted to point out that the x/10->x line can be optimized as either one of the following, both of which are faster (marginally) than division:

Code:
0.1X-->X
XE-1-->X
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are GMT - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement