I am trying to open a file in loop and print the data which my teacher request. however, there is blank in the out put file. and , how to indent the body of loop?? there is my code, please someone tell me what i did wrong!! thanks~`

there is my code..

//program 3
//compute the sum of the fisrt n cubes, and sum of odd number
//send the out put to the file
# include<iostream>
#include<fstream>
using namespace std;
void labelit();
int sumcubes(int);
int sumodd(int);
ofstream file_out;
int main()
{
    int x,sumcube,checkcube,sumod,checkodd;

    labelit();//function, print name& as3
    
    cout<<"please enter n (n=0 for stop)  >>  ";
    cin>>x; 
    while (x!=0)
{
//    cout<<"please enter n(int) > ";//sum of cubes
//    cin>>x;
    sumcube = sumcubes(x);
    cout<<"cubes of sum of x is "<<sumcube<<endl;

    checkcube =(x*(x+1)/2)*(x*(x+1)/2);//check sum of number cubes
    if(checkcube == sumcube)
    cout<<"correct"<<endl;
    else
    cout<<"incorrect"<<endl;

    sumod = sumodd(x);//sum of odd number
    cout<<"sum of odd number is "<<sumod<<endl;
    
    checkodd = x*x;//check sum of odd
    if(checkodd == sumod)
    cout<<"correct"<<endl<<endl;
    else
    cout<<"incorrect"<<endl<<endl;
    
    file_out.open("p4.out");//file open
    //file out put
    labelit();
    cout<<"the sum of first "<<x<<" cubes is "<<sumcube<<endl;
    cout<<"the check is "<<checkcube<<"\t\tCORRECT"<<endl<<endl;
    cout<<"the sum of the first "<<x<<" odd numbers is "<<sumod<<endl;
    cout<<"the check is "<<checkodd<<"\t\tCORRECT"<<endl<<endl;
    file_out.close();//file close
    
    cout<<"please enter n (n=0 for stop)  >>  ";
    cin>>x;
}

    system("pause");
    return 0;  
}

//function, print name and as3
void labelit()
{
     
    cout<<"Selena, Assignment 3"<<endl;
    return;     
}

//function, find sum of cubes
int sumcubes(int x)
{
    int sum,totSum=0;
    
    while(x>0)    {                
    sum=x*x*x;      
    totSum=totSum+sum;
    x=x-1;     
    }
    return totSum;     
}

//funcion, find sum of odd number
//if x=4, the sum of odd number should be 1+3+5+7=16 but NOT 1+3=4...
int sumodd(int x)
{
    int sum=1,totSum=0;
    
    while(x>0)    {
    sum=sum;
    totSum=totSum+sum; 
    sum=sum+2;
    x=x-1;       
    }
    return totSum;
}

And where exactly are you outputing your data?
cout<< will output it to screen

file_out.open("p4.out");//file open
//file out put
labelit();
cout<<"the sum of first "<<x<<" cubes is "<<sumcube<<endl;
cout<<"the check is "<<checkcube<<"\t\tCORRECT"<<endl<<endl;
cout<<"the sum of the first "<<x<<" odd numbers is "<<sumod<<endl;
cout<<"the check is "<<checkodd<<"\t\tCORRECT"<<endl<<endl;
file_out.close();//file close

this is where i want to output...

Well, simply instead of using cout use file_out.

how about function labelit()???
how can i send it to the file??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.