Archived

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

Console output?

This topic is 5271 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

Hey there, thanks for looking at my post. I have a always compiled my code on a UNIX based system; however, I am trying to make a conversion over to "Visual C++ ver.6 SP5" and eventually to "Visual .NET" so I can program in Windows and write some games. I have been very surprised at the lack of portability of much of my old "UNIX" code. Therefore, I have a 2 part question: 1) I cannot get the following code to work in a console.
/* This is just psudeo-code */
#include <iostream>
#include <iomanip>

cout.width(80);
cout.fill('*');
2. Why does iostream.h and iomanip.h create so many errors as opposed to the same declarations w/o the ".h" "poor is the pupil who does not surpass his master" - leonardo da vinci

Share this post


Link to post
Share on other sites
Did you remember to include the namespace std?

Something like this:


using namespace std;


or try to add the binary res operator to your cout like so


std::cout.width(80);
std::cout.fill(''*'');






You fight like a dairy farmer.

Share this post


Link to post
Share on other sites
Thanks Greatwolf for your reply.

Yes, I included:

using namespace std;


No, I did not use:

std::cout.width(80);
std::cout.fill('*');


But I am about too.


Later that day in the batcave: Nope using std:: didn't work either.

[edited by - Dublin on July 8, 2003 1:43:58 AM]

Share this post


Link to post
Share on other sites
My bad!! Kinda?

ok the code works either way with or w/o the "std::".

But only in the main. It doesn''t work in a subfuction that I created. Here is the prototype:

void printHeading(bool aBool, int anInt);

Any Ideas?

Share this post


Link to post
Share on other sites
I can''t see any reason why it wouldn''t work. It works fine for me but then again I''m using Borland''s compiler, not VC++





You fight like a dairy farmer.

Share this post


Link to post
Share on other sites
You sould not really use ''using namespace std;''

it might fuck up things for you if you have many files..

However, using std::cout; should be better..

Put that in all files that uses the cout object..

OR:

skip the using line and put std::cout. Do NOT mix them!!

btw, please post the error msg you''re getting

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Perhaps this should be another post, but I''ve been wondering, and since I saw it mentioned in this post, I had to ask. In school and stuff, I was always taught to use "#include <iostream>" and "using namespace std". Once I started looking at game programming code I see a lot of people using std::cout. PhiberOptic, you say use the latter (std::cout), and your reason is because it would mess up things if there are too many files. Could you, or someone, elaborate? Because ever since i started seeing it, I wondered why it''s used. using namespace seems so much easier. I also don''t look forward to having to change my old code, and break my habits....

old coding habits die hard

Share this post


Link to post
Share on other sites
Sorry, I had to go to bed last night, to many beers and to much work; therefore, I wasn't thinking straight. So I thought... Then I get home and I come to find the solution to my post. Low and behold, I didn't find anything concret. So if any of you guys out there would be kind enough to pull yourselves away from your on projects to see if you can debug my code, I would GREATLY appreciate it.

The following code compiles error FREE using my compiler "Visual C++ ver. 6 ServicePack 5". However the "print function" does NOT work.

#include <windows.h>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <time.h>
#include <string>
#include <vector>
using namespace std;


void aPrintFunction();

int main(void)
{

std::cout.width(80);
std::cout.fill('*');
std::cout << "Before aPrintFunction" << endl;

aPrintFunction();

std::cout << "After aPrintFunction" << endl;
std::cout.width(80);
std::cout.fill('*');


return EXIT_SUCCESS;
}


Neither does this.



void aPrintFunction()
{
std::cout << endl;
std::cout.width(80);
std::cout.fill('*');
}


#include <windows.h>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <time.h>
#include <string>
#include <vector>
//using namespace std;



void aPrintFunction();
int main(void)
{

std::cout.width(80);
std::cout.fill('*');
std::cout << "Before aPrintFunction\n";

aPrintFunction();

std::cout << "After aPrintFunction\n";
std::cout.width(80);
std::cout.fill('*');


return EXIT_SUCCESS;
}

void aPrintFunction()
{
std::cout << "\n";
std::cout.width(80);
std::cout.fill('*');
}


[edited by - Dublin on July 8, 2003 9:29:24 PM]

[edited by - Dublin on July 8, 2003 9:34:45 PM]

[edited by - Dublin on July 8, 2003 9:35:53 PM]

[edited by - Dublin on July 8, 2003 9:38:11 PM]

Share this post


Link to post
Share on other sites
I''d be glad to help, but you should be a bit more specific when describing your errors. It''s way much easier to help out that way. Anyways, this code should do the trick. If it still doesn''t work, tell us what your complier has to say about the code.




#include <windows.h>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <time.h>
#include <string>
#include <vector>

void aPrintFunction();

int main(void)
{
std::cout.width(80);
std::cout.fill(''*'');
std::cout << "Before aPrintFunction" << std::endl;
aPrintFunction();
std::cout << "After aPrintFunction" << std::endl;
std::cout.width(80);
std::cout.fill(''*'');
return EXIT_SUCCESS;
}

void aPrintFunction()
{
std::cout << std::endl;
std::cout.width(80);
std::cout.fill(''*'');
}



Share this post


Link to post
Share on other sites
quote:
Original post by Dublin
The following code compiles error FREE using my compiler "Visual C++ ver. 6 ServicePack 5". However the "print function" does NOT work.

As Robin would say, "Holy unnecessary includes Batman!".

For using cout, all you really need is #include <iostream>. But, what kind of errors do you get from that code? The second one doesn''t work becuase the pre-directives (the includes) are not the top-most code.

Here''s a quick Hello World using a function for you to play with:

NOTE: I haven''t compiled it....

#include <iostream>

void printFunction();

int main()
{
std::cout << "Before printFunction()" << std::endl;
printFunction();
std::cout << "After printFunction()" << std::endl;
return 0;
}

void printFunction()
{
std::cout << "Hello, Momma! << std::endl;
}


I''ve also never used cout.fill and cout.width as I made my own console output engine to make my life easier.



Rob Loach
Current Project: Go Through Object-Oriented Programming in C++ by Robert Lafore

"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?"
- The Boondock Saints

Share this post


Link to post
Share on other sites
Thank you folks for your responses to my questions.

Exellion: Sorry there, In my minds eye I had explained what the error was. My bad. As far as the routine that you posted, It produced the exact same results as the 2 routines that posted.

The error I am having is that I cannot get the following lines of code to work in a subfunction, out of the main.

std::cout.width(80);
std::cout.fill(''*'');

Ouput to my console looks something like this:

********************************************Before aPrintFuction

*********************************************After aPrintFuction

Rob Loach: Sorry bout all the includes. I know they aren''t all involved in for those little routines I posted above. However, they are the includes that I am using in the program that contains a printFunction like the ones above. Instead of posting the "actual" printFuction from the console program I am trying to write, I made the above routines to mimic them.


I hope we can figure this out.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Based on the code that you have provided, that is exactly what the output should be expected to be.
Let''s look at this for a moment.

std::cout.width(80);
std::cout.fill(''*'');
std::cout << "Before aPrintFunction" << std::endl;

and as output this shows
********************************************Before aPrintFuction


followed by :

aPrintFunction();
std::cout << "After aPrintFunction" << std::endl;

which you show the output to be
*********************************************After aPrintFuction

then the remainder
std::cout.width(80);
std::cout.fill(''*'');
return

you are requesting an explaination as to why the text is not shown from the aPrintFunction(); but if you look at your code in the main loop, and the output, you should notice that the code is in fact executing and you are receiving the *''s in the output before you print "After aPrintFuction".

Therefore your code is executing and your inner statement is performing the requred task, which is to buffer 80 *''s into cout.

Now I beleive your confusion is to when the *''s are put on the screen. They are not put on the screen immidiately follinging the call to fill. No, they are printed following the call to ''std:cout << "" << std:endl;''

what you are not printing is in fact not what is behind the code, but instead it is what is after the print of "After aPrintFuction", where you call to fill again, and then return. these *''s are the *''s that will not print to the screen.
Adding ''std::cout << "" <<std::endl;'' after that last fill will show the *''s to the screen.

May not be the only way, but it is a way that works.

Kyle Prestenback
''Episode

Share this post


Link to post
Share on other sites
Problem SOLVED; thanks to everyone who posted in this thread, I learned several new facts that I didn''t fully quite understand. And, I got the problem SOLVED, CASE CLOSED.

Thanks Kyle for your response. I think I understood what your were trying to say. So I tried the following routine and I got the following results:


#include <iomanip>
#include <iostream>

void aPrintFunction();
int main()
{
aPrintFunction();

return EXIT_SUCCESS:
}
void aPrintFunction()
{
std::cout.width(80);
std::cout.fill(''*'');
std::cout << "I hope this works" << std::endl;
/*std::cout << std::endl; this line instead of the one above had no OUTPUT*/

}



OUTPUT:

***********************************************I hope this works


Share this post


Link to post
Share on other sites