Monday, May 24, 2010

Sending output to a file and screen in c++?

this is my program so far: i am trying to send and output to a file and also to the screen. i do not want the prompts to appear on the screen, therefore i need to send the prompts to a file. Some how it is going to a file but on the screen i still have to input the information.


can someone tell me please what i am doing wrong.





{


int custnum;


double $orderplaced, $orderpaid, balancedue;





ofstream outfile;


outfile.open("g:A3.out");





cout.setf(ios::fixed,ios::floatfield);


cout.precision(2);





cout %26lt;%26lt; "\t\tHandy Hardware Company's Customer Database" %26lt;%26lt; endl;


cout %26lt;%26lt; "\t\t___________________________________... " %26lt;%26lt; endl;


cout %26lt;%26lt; endl;


cout %26lt;%26lt; "\tOrders Placed($)\tOrders Paid($)\tBalance Due\tStatus " %26lt;%26lt; endl;


outfile %26lt;%26lt; "Enter the Customer Number " %26lt;%26lt; endl;


cin %26gt;%26gt; custnum;


while (custnum %26gt; 0) { // read unitl a negative number is entered


outfile %26lt;%26lt; "Enter dollar amount of Orders placed

Sending output to a file and screen in c++?
First of all, you have the prompt in the outfile, then you cin the answer:


outfile %26lt;%26lt; "Enter the Customer Number " %26lt;%26lt; endl;


cin %26gt;%26gt; custnum;


That's not user friendly, because you would have to be looking at the file prompts, then entering the answers on screen.





Also, I don't think you should put the prompts in the file. If you can't have them on the screen, don't put them anywhere, because when you go to read the file, you'll have to overlook the prompts. So if the prompts can't go on the screen, don't put them anywhere. Just infile%26gt;%26gt; your answers.
Reply:Well,


1.$ is an illegal character for a declaration (I think).


2.I think you mean "G:\\A3.out".


Hold on a second, I need to see the details again...


Oh, that's it? Well, you're doing OK so far. Remeber


cout is tied to stdout (the screen).


outfile is tied to file G:a3.out (do you mean G:\\A3.out? The double backslash is because of the backslash being an escape character.)


and cin is tied to stdin (the keyboard).


No comments:

Post a Comment