In C++ how may I ask can I output a string as with fgets in C. What is its equivalent.
In C++ how may I ask can I output a string as with fgets in C. What is its equivalent.
I may be wrong, as I started with C++, but I believe printf and cout (iostream.h, but only in console applications) will suit what you're looking for.
I'm not sure if a string (if you're talking about the STL class) will work with printf();. Use cout<< anyway - it's loads easier.
Away.
cin.getline(buffer_to_hold_input, num_of_chars, char_deliminator)
If you are using the std::string, the function differs a bit.
Use cout<<" "; as has been said before.
Example of <<endl;
cout<<"this is a string."<<endl; //this creates a new line.
Escape sequences are also allowed as shown.
cout<<"\tthis is a string.\n";
Im sorry, I should have said. (string variable)
the only way I can print out the charectors in a string at the moment is.......
for(x=0;x<sizeofstring;x++){
cout << string[x];
}
this becomes, tedious, and Im sure that there is a command that will allow me to print the string en masse. I appologize for my lack of clarity, I do know the basics of C, and, what little I derived from a high school class, C++.
Last edited by parrellel; 07-06-2003 at 10:09 AM.
If you're using the string class from <string> then cout will work. If you're using a nul terminated array of char, then cout will still work. If you're using some user defined class that doesn't overload operator<< then cout won't work. Can you show us the string variable declaration?
Why are you iterating through the string? You're not changing variables. As has been said before you don't need to do it that way, just do this cout<<string; and the whole string will be outputed to the console window.
output is not the string by writing
cout << array;
output is a memory address. This is what you instruct me to do? Accessing the memory address, expectedly, offers me the first peice'o'array not the area en tota.
Or am I being horribly stupid.
Can you post your code already? You've given us useless little snippets so far, we need more to figure out what your exact problem is.
Yes, we need code. At this point I'm assuming your using std::string from when you said string variable. But it could be a char array. Post the code, and we'll be able to help you more.