Progress of program

This is a discussion on Progress of program within the C++ Programming forums, part of the General Programming Boards category; I am working on the interface of the program. Cause it takes long time to run, I am thinking of ...

  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
    University of Waterloo
    Posts
    1,903
    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,211
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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'; 
    }
    ?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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;
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21