Limit the amount of output

Mar 27, 2016 at 2:42am
Is there a way to make the program pause or stop at a certain line of output? I have a loan calculator which I want to stop at a certain point in the output without compiling the entire code. How could I do this?
Mar 27, 2016 at 3:10am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <thread>
#include <chrono>

struct pause
{
    constexpr explicit pause( int milliseconds = 1000 ) : milliseconds(milliseconds) {}
    const unsigned int milliseconds ;
};

template < typename OSTREAM > OSTREAM& operator<< ( OSTREAM& stm, pause p )
{
    std::this_thread::sleep_for( std::chrono::milliseconds(p.milliseconds) ) ;
    return stm ;
}

int main()
{
    std::cout << "hello " << pause(2000) << "world" << pause() << "!\n" ;
}
Last edited on Mar 27, 2016 at 3:12am
Mar 27, 2016 at 3:17am
Compiler doesnt compile this. Is there a simpler way to limit the output of code?
Mar 27, 2016 at 3:18am
More specifically, I have a loan program and I just want to limit the output to the first three months (usually output line 36).
Last edited on Mar 27, 2016 at 3:19am
Mar 27, 2016 at 3:28am
After output for the first three months, put std::cout into a failed state. std::cout.clear(std::ios::failbit) ;

To re-enable output, clear the failed state: std::cout.clear() ;
Mar 27, 2016 at 3:31am
$ ./program | head --lines 36
Mar 27, 2016 at 4:15am
Where would I put that $ ./program | head --lines 36 Line 42?
As for the JL's suggestion, specifically I'm trying to run the program and have it stop around a specific line. So I cant directly insert things into the output. Unless I'm misunderstanding. And i'm not using that kind of compiler.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 
int main()
{	  		
	cout << "Mortgage calculator"<<endl;
	
	double principalAmount, 
	annualRate,nYears,
	nMonths=12*nYears;
	{
		cout<<"Insert Mortgage amount:"<<endl; 
		cin >>principalAmount;
		
		cout <<"Insert Annual Interest rate (in decimals):"<<endl;
		cin >>annualRate;
		
		cout<<"Duration of years:"<<endl;
		cin >>nYears;		
	}
	
	double totalPaid=0;
	double monthlyPayment=0;
	double remaining=principalAmount;
	double interestAdd;
	double monthlyRate;
	double toAdd;
	double i;
	
	cout <<"Mortgage, Monthly paid, Montly Interest."<<endl;
	
	while (remaining > 0){
		nMonths++;
		monthlyRate=annualRate/12;
		monthlyPayment= (principalAmount * monthlyRate) / (1-(1/(pow((1+monthlyRate),(nYears*12))))); 
		interestAdd=remaining * monthlyRate;
		remaining+=interestAdd;
		i=remaining-interestAdd;
		
	if (remaining > monthlyPayment) {
	toAdd=monthlyPayment; }
	else {
	toAdd=remaining;
	}
	cout<<nMonths<< ". $" << i << "    . $" <<    toAdd << "    . %" <<    monthlyRate <<endl;
	i+=toAdd;                                                                              
	remaining-=toAdd;
	
	}
return 0;
}


Output as of current:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
A)

Mortgage calculator
Insert Mortgage amount:
250000
Insert Annual Interest rate (in decimals):
.06
Duration of years:
30
Mortgage, Monthly paid, Montly Interest.
1. $250000    . $1498.88    . %0.005
2. $249751    . $1498.88    . %0.005
3. $249501    . $1498.88    . %0.005
4. $249250    . $1498.88    . %0.005
5. $248997    . $1498.88    . %0.005
6. $248743    . $1498.88    . %0.005
7. $248488    . $1498.88    . %0.005
8. $248232    . $1498.88    . %0.005
9. $247974    . $1498.88    . %0.005
10. $247715    . $1498.88    . %0.005
11. $247454    . $1498.88    . %0.005
12. $247193    . $1498.88    . %0.005
13. $246930    . $1498.88    . %0.005
14. $246666    . $1498.88    . %0.005
15. $246400    . $1498.88    . %0.005
16. $246133    . $1498.88    . %0.005
17. $245865    . $1498.88    . %0.005
18. $245596    . $1498.88    . %0.005
19. $245325    . $1498.88    . %0.005
20. $245052    . $1498.88    . %0.005
21. $244779    . $1498.88    . %0.005
22. $244504    . $1498.88    . %0.005
23. $244228    . $1498.88    . %0.005
24. $243950    . $1498.88    . %0.005
25. $243671    . $1498.88    . %0.005
26. $243390    . $1498.88    . %0.005
27. $243108    . $1498.88    . %0.005
28. $242825    . $1498.88    . %0.005
29. $242540    . $1498.88    . %0.005
30. $242254    . $1498.88    . %0.005
31. $241966    . $1498.88    . %0.005
32. $241677    . $1498.88    . %0.005
33. $241387    . $1498.88    . %0.005
34. $241095    . $1498.88    . %0.005
35. $240801    . $1498.88    . %0.005
36. $240507    . $1498.88    . %0.005
... output would stop or pause here....

360. $1492.68    . $1498.88    . %0.005
361. $1.26587    . $1.2722    . %0.005

--------------------------------
Process exited after 16.59 seconds with return value 0
Press any key to continue . . .
*/
Last edited on Mar 27, 2016 at 5:05am
Mar 27, 2016 at 7:04am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>
#include <iomanip>

int main( int argc, char* argv[] )
{
    //////////////////////////////////////////////////////////////////
    // usage: program_name <month number after> <month number till> //
    //                                                              //
    // example: program_name 22 340                                 //
    //     skip output for months after month# 22 till month# 340   //
    //////////////////////////////////////////////////////////////////
    
    int silence_after_month = 36 ;
    int unsilence_after_month = 356 ;
    try
    {
        if( argc > 1 ) silence_after_month = std::stoi( argv[1] ) ;
        if( argc > 2 ) unsilence_after_month = std::stoi( argv[2] ) ;
    }
    catch( const std::exception& )
    {
        std::cerr << "invalid command line arguments were ignored\n" ;
    }
    
    std::cout << "\nnote: output for months " << silence_after_month+1 << " to " << unsilence_after_month << " is elided\n\n" ;

    for( int month_number = 1 ; month_number <= 360 ; ++month_number )
    {
        std::cout << std::setw(3) << month_number << ".  .... < output for month " << month_number << " > .... \n" ;
        if( month_number == silence_after_month ) std::cout.clear( std::cout.failbit ) ;
        if( month_number == unsilence_after_month ) std::cout.clear() ;
    }
    
    std::cout << "\n---------------------------\n\n" ;
}

http://coliru.stacked-crooked.com/a/9943390dd397808a
Topic archived. No new replies allowed.