Sunday, July 12, 2009

Still more help with C++ (not much more questions...)?

2 questions this time.





1) how do I output the results of a calculation into a file?





2) What Library is needed for random number generation?

Still more help with C++ (not much more questions...)?
If you can avoid file io, avoid it. Obviously, the simplest thing to do is nothing at all.





So, if we have a program (say):





int main()


{


cout %26lt;%26lt; 10 + 5;


return 0;


}





the how do we get the result (15) to be stored in a file? Don't change the program! Use redirection...





myprg %26gt;afile





does just fine. Now, we gain by doing this. The output of myprg can be "piped" into another program (like grep, or less, or awk, or perl or another calculation etc.)
Reply:to output something to a file, you include %26lt;fstream%26gt; and create a file stream, it works basically like cout. Google fstream for more detail.





and you need %26lt;cstdlib%26gt; for random numbers
Reply:ofstream , use STL


No comments:

Post a Comment