-
November 21st, 2008, 02:57 AM
#1
lines of code not processing in sequential order
this is not really a big problemo, but its making my brain itch. I'm trying to figure out why the program displays the cout << "downloading source..." line after processing the CurlGetUrl() function. the program takes the cin input then waits while grabbing the url then just displays instantaneously all the text output at once
sorry if this is a dumb question, i'm just learning c++, and I did do a search of the forums already but didn't see any explanation for this
Code:
int main(void) {
CurlSetCookieFile("./cookie.jar");
double quote;
int returnvalue;
string sPage;
string ticker;
string urlcombo;
char *url1 = "http://quotes.nasdaq.com/asp/SummaryQuote.asp?symbol=";
char *url2 = "&selected=";
cout << endl << "QUOTE PARSER" << endl << "============" << endl << endl;
do {
cout << "Enter ticker (or quit):";
cin >> ticker;
if (ticker=="quit") break;
cout << endl << "downloading source...";
urlcombo = url1 + ticker + url2 + ticker;
returnvalue = CurlGetUrl(urlcombo.c_str(), &sPage);
if (returnvalue) {
cout << "done!" << endl << "parsing quote data...";
quote = parsefloat1(sPage, "lastsale");
cout << "done!" << endl;
cout << ticker << "=$" << quote << endl << endl; }
else cout << endl << "url error!" << endl; }
while (ticker!="quit");
FreeCurlLibrary();
return 0; }
-j
-
November 21st, 2008, 07:59 AM
#2
Re: lines of code not processing in sequential order
cout is a buffered stream. That means it won't display anything you write to it until the buffer is flushed (or it fills up). While cout << flush is available, whenever you do cout << endl it flushes the stream as well. (So if you don't need to flush the stream but you want to write a new line, use cout << '\n' instead.)
cerr is unbuffered, FYI.
-
November 21st, 2008, 09:15 AM
#3
Re: lines of code not processing in sequential order
Originally Posted by Lindley
cout is a buffered stream. That means it won't display anything you write to it until the buffer is flushed (or it fills up). While cout << flush is available, whenever you do cout << endl it flushes the stream as well. (So if you don't need to flush the stream but you want to write a new line, use cout << '\n' instead.)
cerr is unbuffered, FYI.
While that is true, and documented for:
Microsoft Visual C++ 1.0 Professional Edition
Microsoft Visual C++ 1.5 Professional Edition
Microsoft Visual C++ 1.51
Microsoft Visual C++ 1.52 Professional Edition
Microsoft Visual C++ 2.0 Professional Edition
Microsoft Visual C++ 2.1
Microsoft Visual C++ 4.0 Standard Edition
Microsoft Visual C++ 5.0 Enterprise Edition
Microsoft Visual C++ 6.0 Enterprise Edition
Microsoft Visual C++ 5.0 Professional Edition
Microsoft Visual C++ 6.0 Professional Edition
Microsoft Visual C++, 32-bit Learning Edition 6.0
I can NOT find a statement about this functionallity in the ISO Standard....can you please post a link showing this is true for C++ and not just for certain compilers [the above are all known to be non-compliant as they were written before the standard - and are also all obsolete....]
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
November 21st, 2008, 09:22 AM
#4
Re: lines of code not processing in sequential order
I don't have a copy of the standard. However I know it to be true for gcc 4.1.2. I am *not* able to produce obvious verification of this in Visual Studio 2005 with a quick test however, so it may not be true everywhere.
Last edited by Lindley; November 21st, 2008 at 09:26 AM.
-
November 21st, 2008, 09:24 AM
#5
Re: lines of code not processing in sequential order
I guess that there is no requirement for streams being buffered or not, however, it is clearly stated that endl flushes output stream at 27.6.2.7 Standard basic_ostream manipulators.
I just do not know which part you consider non-conformant
Cheers
B+!
'There is no cat' - A. Einstein
Use [code] [/code] tags!
Did YOU share your photo with us at CG Members photo gallery ?
-
November 21st, 2008, 09:36 AM
#6
Re: lines of code not processing in sequential order
Thanks for the refernce...it sure does....
Calls os.put(os.widen('\n')) then os.flush().
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
November 21st, 2008, 11:21 AM
#7
Re: lines of code not processing in sequential order
Thanks everyone, this is really helpful. In order to get this to display the way I intended i.e.
downloading source...done!
then I should add:
...since I don't want it to display on two lines like:
downloading source...
done!
I also saw this coded as:
Code:
flush(cout);
cout << "xxxxxx" << flush;
but not sure what compilier they referenced, I'm using g++ on a Fedora9_64 box
I did some searching and noticed some reference to this method but also some folks said that it hurts performace to flush the buffer. This seems strange to me since endl flushes the buffer as well and this is used quite often. Can anyone explain why flushing the buffer with flush() would hurt performance? Is the operation of endl and flush() different in terms of performace for cout? I noticed you also impled I should just use endl instead but wondering why.
thanks again for educating a new comer...
-jerry
-
November 21st, 2008, 11:25 AM
#8
Re: lines of code not processing in sequential order
Don't worry about the performance hit here. Basically it's telling you that constantly writing output to the screen will slow down compute-heavy tasks. But your program is asking for input on each iteration of the loop anyway, so you don't really lose anything by flushing here.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|