Output problem

I have a program which takes an input for a case, then makes the output and if the input is 0 it stops running. I want the program to output all results at the end of the program. So, first inputs, then 0 and then outputs.
My program is this one:
#include <iostream>

using namespace std;
void function1(int num){
do{
cin>>num;
for(int n=0;n<num;n++){
cout<<"1";
}
cout<<""<<endl;
}while(num);
}
int main()
{
int num1=1;
function1(num1);
return 0;
}
THANKS
You could store your output somewhere. As a class member, as a global variable, or passing the output with a return to the calling function.
Here an example of the latter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <vector>

std::vector<char> function(int num)
{
    std::vector<char> v_outputs;
    do {
        std::cin >> num;
        for( int n = 0; n < num; ++n) v_outputs.push_back( '1' );
    } while( num );
}

int main()
{
    int num1 = 1;
    std::vector<char> v_outputs = function( num1 );  
    for( char value : v_outputs ) std::cout << value;
} 
Last edited on
if the output is 'reasonably small' you can just spew it all into a single string.
string may begin to slow you down if you have too much text, though.

1
2
3
4
5
6
7
8
void foo()
{
   static string s;
   ..
   s+= "words\n"; //whatever... you can put in end of lines, spacing, variables etc. 
   if(something == 0)  
      cout << s;  
}


Last edited on
You could store your inputs somewhere, like in a vector

> first inputs, then 0 and then outputs.
¿are you sure you want that?
¿why?
if you want to see only the output you may use stream redirection
I want the program to output all results at the end of the program.
Why? It's far more efficient to generate the output for each case as you see it. If you don't want inputs and outputs to run together then just redirect the output to a file:
$ ./foo > output.txt
10
5
3
0

$ cat output.txt
1111111111
11111
111


Maybe I'm just showing my UNIX bias.

Thanks to all for your help. For those who asked, why I want to do such thing, it is because I want to check my program on a validator. Actually the whole problem is from a website. And this problem requires to have this type of in- and output.

Thanks
Topic archived. No new replies allowed.