A Question on C++ DOS Console Programming

Started by
17 comments, last by Radagar 21 years, 10 months ago
"Finally, as I''m used to Strings in BASIC, I''m having a LOT of trouble getting them to work right in C++. I can''t get a function to return a string right, and I''m a little confused about how the name of a string is simply a pointer to the first element of that string. Pointers and Linked Lists are still a little fuzzy too. Does anyone have any easy-to-follow code snippets that show good string use?"

That''s because you aren''t using string, you are using char*. Here''s what you are looking for

  #include <string> //note no .husing namespace std; //just always do this if you include//a header that doesn''t have a .hstring first; //declare a string, just like declaring and intfirst = "whoo"; //assign, no using strcpy for me!string second("hoo!"); //make a variable and initializestring third = "yipee!"; //traditional syntax for the same thingfirst+=second; //now first is "whoohoo!"second = first+" "+third; //now second is "whoohoo! yipee!"if(first==second) //no need to use strcmp   do_something();if(first<second) //no need to use strcmpoh and here''s a nice one:string string_array[10];#include <algorithm>sort(string_array,string_array+10);//sorts the array//note you should be using a "vector" which is a//superior array that can resize and so much more//and the same sort function can sort both arrays and vectors//now there is a problem with string, some functions//that you will want to use take a char*, not a string.//to remedy that string has a member function c_str()string filename("data.txt");ofstream(filename.c_str());  

to learn more about strings, vectors, sort, ofstream and so much more read The C++ Standard Library: A Tutorial and Reference by Nicolai M. Josuttis.


Advertisement
A problem....


  #include <string>using namespace std;string first;first = "whoo";cout << first;  


This doesn''t work! I get the following Compiler Errors. Again, I''m using MSVC++ 6.0 with a Win32 Console App. Project.

c:\my documents\string test\st.cpp(6) : error C2501: ''first'' : missing storage-class or type specifiers
c:\my documents\string test\st.cpp(6) : error C2371: ''first'' : redefinition; different basic types
c:\my documents\string test\st.cpp(4) : see declaration of ''first''
c:\my documents\string test\st.cpp(6) : error C2440: ''initializing'' : cannot convert from ''char [5]'' to ''int''
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
c:\my documents\string test\st.cpp(8) : error C2143: syntax error : missing '';'' before ''<<''
c:\my documents\string test\st.cpp(8) : error C2501: ''cout'' : missing storage-class or type specifiers
c:\my documents\string test\st.cpp(8) : error C2143: syntax error : missing '';'' before ''<<''
Error executing cl.exe.



~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~
WyrmSlayer RPG - In Early Development
If you''re using MSVC and you have the MSDN library, look under the Contents in Platform SDK->Windows Base Services->Files and I/O->Consoles and Character Mode Support for all the information you need to know about writing to a Win32 DOS console.

MSDN library is your friend, though it''s a pain to find what you want in it.
the #include and using namespace std; should be on different lines. I just messed up when formating. The reason is that preprocessor directives (things that start with #) aren''t really C++ code and so they use different rules. So the fact that they are on the same line matters.
quote:Original post by Anonymous Poster
the #include and using namespace std; should be on different lines. I just messed up when formating. The reason is that preprocessor directives (things that start with #) aren''t really C++ code and so they use different rules. So the fact that they are on the same line matters.


The formatting messed up on my machine as well. They are on seperate Lines..

and to Cgoat... I''m looking there now. MSDN Libary is a pain, but I''ll try it =)





~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~
WyrmSlayer RPG - In Early Development
So, anyone know why my string code above didn''t work? seems simple enough... I''m thinking the ''#include <string>'' doesn''t actually have a ''string'' type variable... But I''m not sure.

any suggestions?

~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~
WyrmSlayer RPG - In Early Development
correct, it doesn''t, unless you write:
using namespace std;

Make sure that the include and namespace lines are outside of your functions or whatever and that you code is in a function. Also remember that in MSVC++ and most other compilers C++ files end in .cpp, not .c


  #include <string>using namespace std;int main(){    string s;}  
''first'' has already been defined in std::. Pick a different name.
---visit #directxdev on afternet <- not just for directx, despite the name
You could also do:

char *peh = "Foobar";

or

char peh[255] = "La la la la";

It''s all you need

This topic is closed to new replies.

Advertisement