Thread: Progress of program

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    12

    Progress of program

    I am working on the interface of the program. Cause it takes long time to run, I am thinking of having a progress bar, which it shows how many percentage it completed. For example, after

    cout<<"20% is completed"

    , may I ask how I can erase it and replace with

    cout<<"21% is completed" afterward?

    Many thanks and I appreciate any help.

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Here's a nice quick one:

    Code:
    while (progress <= 100)
      cout<< progress << "% is completed\r";

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    If you mean actually writing on the exact same line, I don't think that is possible in plain, vanilla C++ (using the standard library). You would need to write your own code in the specific-O/S manner required to get greater control over the console, or use a library which ends up doing that for you.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    using namespace std;
    
    int main()
    {
        string first, second;
        stringstream fs;
    
        first = "It's ";
        second = "&#37; done.";
    
    
        cout << first;
    
        for(int i=0; i<100; i++)
        {
            fs=""; //apparently this is incorrect... no operator= for that
            fs << i << second;
    
            cout<<fs.str;
            //do stuff
            for(int j=0; j < fs.length(); j++) cout <<'\b'; //nonexistent member function
        }
    
        cout<<"done     "<<endl;
        return 0;
    }
    something like this... can somebody help me to get this to compile with just using C++ constructs?
    Last edited by robwhit; 06-11-2007 at 09:32 PM.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Something like
    Code:
    for(int i=0; i<100; i++)
    {
            stringstream fs("");
            fs << i << second;
    
            cout<<fs.str;
            //do stuff
            for(int j=0; j < fs.str.length(); j++) cout <<'\b'; 
    }
    ?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    yes, except:
    Code:
    cout<<fs.str;
    some.cc:22: error: no match for 'operator<<' in 'std::cout << fs.std::basic_stri... etc... lots of ugly type-stuff
    
    and
    
    for(int j=0; j < fs.str.length(); j++) cout <<'\b';
    some.cc:24: error: insufficient contextual information to determine type

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    This compiles and works fine
    Code:
    #include <sstream>
    #include <iostream>
    //#include <windows.h> Sleep is used to emulate time consuming operation
    
    int main()
    {
    	for(int i=0; i<100; i++)
    	{
    		std::stringstream fs;
    		fs << i << "&#37; done.";
    
    		std::cout << fs.str();
    		//do stuff
    		//Sleep(100); time consuming emulation
    		for(int j=0; j < fs.str().length(); j++) std::cout <<'\b'; 
    	}
    	return 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Very nice. One problem is that it just goes to 99&#37; when of course it should go to 100%.

    I've never had much success at all with using '\b' to clear console output. Nice to see this work.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    arr.. dang parentheses!! thanks vart! I'm so used to C...

    thanks mac... if you mean me... haha... but when it's 100, you don't need it anymore.

    something I found out is that \b doesn't clear it, you have to overwrite it. Thus all the spaces in the last cout.
    Last edited by robwhit; 06-11-2007 at 10:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Displaying Progress.
    By samus250 in forum C Programming
    Replies: 15
    Last Post: 04-11-2008, 03:56 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM