CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2008
    Posts
    1

    Question 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

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  3. #3
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: lines of code not processing in sequential order

    Quote Originally Posted by Lindley View Post
    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

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  5. #5
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    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 &#91;code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  6. #6
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    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

  7. #7
    Join Date
    Nov 2008
    Posts
    1

    Question 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:
    Code:
    cout.flush();
    ...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

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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
  •  





Click Here to Expand Forum to Full Width

Featured