Archived

This topic is now archived and is closed to further replies.

string creation

This topic is 5401 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok iam making a simple hamgman game in a win32 API. but what iam trying to do right now is make some kind of loop that will make a string of "X"''s in the same length of the string that the word chosen for the game to display in the box that shows the encrypted word. any help would be great. thanks

Share this post


Link to post
Share on other sites

  
#include <string>

using namespace std;

string word( "RanBlade" );
string x( word.size(), ''X'' );



[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
ok a lil revision on that...

I need to make a loop that will take lets say the game chose "online" as the word for the game and use its length of 6 to created a string of "X"''s in the length of 6 to act as a encrypted word....

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If you use C++:

string str(3, ''X'');

creates a string of 3 X.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
So what''s the problem with the solutions above?

Share this post


Link to post
Share on other sites
Some possibly useful functions :

string::find
string::find_first_of -
string::find_first_not_of

Look them up.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
Ok iam dumb. But in my test function when i set this up it compiles just fine... this is a console app to test my fucntions as i create them... but the code goes as follows


char* pszString;
pszString = Word();

//using namespace std;string word( "RanBlade" );string x( word.size(), ''X'' );

using namespace std;

string word1(pszString);
string x(word1.size(), ''X'');

now when i try to include that into my Win32 API i get a lot of errors... if you need anymore code segments just tell me thanks..

Share this post


Link to post
Share on other sites
well first, comment out the code you posted, and just try an int main(void) function with the new Win32 API stuff you are using ... which will tell you once and for all if the errors are in your string code, or your Win32 code ... cause I''m guessing it isn''t the string code ... and BTW ..always post at least the first error message when you say you have errors, cause many of us might know the exact error cause if you did

Share this post


Link to post
Share on other sites
sorry for my ignorance.. lol iam new to C++ Windows programming. but here are the error msgs in my win32APP

:\Source Code\hangmangame\gamewindow.cpp(31) : error C2872: ''string'' : ambiguous symbol
C:\Source Code\hangmangame\gamewindow.cpp(31) : error C2872: ''string'' : ambiguous symbol
C:\Source Code\hangmangame\gamewindow.cpp(31) : error C2146: syntax error : missing '';'' before identifier ''word1''
C:\Source Code\hangmangame\gamewindow.cpp(31) : error C2065: ''word1'' : undeclared identifier
C:\Source Code\hangmangame\gamewindow.cpp(32) : error C2872: ''string'' : ambiguous symbol
C:\Source Code\hangmangame\gamewindow.cpp(32) : error C2872: ''string'' : ambiguous symbol
C:\Source Code\hangmangame\gamewindow.cpp(32) : error C2146: syntax error : missing '';'' before identifier ''x''
C:\Source Code\hangmangame\gamewindow.cpp(32) : error C2065: ''x'' : undeclared identifier
C:\Source Code\hangmangame\gamewindow.cpp(32) : error C2228: left of ''.size'' must have class/struct/union type
C:\Source Code\hangmangame\gamewindow.cpp(36) : error C2872: ''string'' : ambiguous symbol
Error executing cl.exe

Share this post


Link to post
Share on other sites
Did you include the <string> header ?

If you have something else named ''string'', remove the using namespace std; bit, and replace all occurences of string in the code I gave you with std::string.

That bit also looks very suspicious to me.

char* pszString;
pszString = Word();

Although it is not the source of the errors you posted, be careful with your memory allocations.



[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
ok well i think iam gonna leave this alone while i test it more and more... i got rid of those errors and am on to some new ones... thanks for all your help guys..

Share this post


Link to post
Share on other sites
ok i got all the errors to go away. now how would i assign the value to a text box or listbox in a window... heres my error... I tried looking online but no luck...

SetDlgItemTextA'' : cannot convert parameter 3 from ''class std::basic_string,class std::allocator >'' to ''const char *''
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

I understand what the error says i just dont know how to fix this so I can set it to display in my window.... thanks in advance

Share this post


Link to post
Share on other sites
string::c_str returns a const char*.
You can write MyApiFunction( word.c_str() );.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites