Mulalo
Mulalo

Reputation: 11

C++ FUNCTIONS dealing with input and output

I was given a task to code in c++,i had to develop an algoriths that checks 3 numbers in the following criteria , If firts number exponent second number is equal to third number then i it shoul return true or false. I made use of power function to find first number raised to second number and another funtions called comapare(num1,num2,num3).My pogramme works fine but the problem is it shows the results in a console and i want to output to an output file which i named outfile,when i do that i get an error which says that outfile was not declared, I will appreciate any help to be be able to output to the output file. below is my code

#include <iostream>
#include<fstream>

using namespace std;

double powerfunc(double num1,double num2){ // power function  

    double base=num1,exponent=num2,result;//decalsring base and exponent

    for(int i=1;i<exponent;i++){
        base=base*base;
        result=base;
    }
    return result;
}

double compare(double x, double y,double z){//Comapares if x exponent y gives z and return 
    //Return true or false

    int storersults;
    if( (powerfunc(x,y)== z)){
        cout<<"true"<<endl;
        //outfile<<"true"<<endl;
        storersults=powerfunc(x,y);
        cout<<"results is "<<storersults;
        //outfile<<"results is "<<storersults;
    }
    else{
        cout<<"false"<<endl;
        //outfile<<"false"<<endl;
        storersults=powerfunc(x,y);
        cout<< " results is "<<storersults;
        //outfile<<"results is "<<storersults;
    }
    return storersults;
}

int main(){
    ifstream infile;
    ofstream outfile;

    infile.open(" input.txt");
    outfile.open("output.txt");// I wanna link output to functions above and get results // I wann get results on outputfile not on console
    // double a,b,c;
    //infile>>a,b,c;
    powerfunc(3,2);//This functions finds power of 3 and 2
    compare(3,2,9);//This fucnions compare if 3^2=9 then retuens true

    }
    infile.close();
    outfile.close();
    return 0;
}

Upvotes: 1

Views: 112

Answers (2)

김선달
김선달

Reputation: 1562

i get an error which says that outfile was not declared,

Because outfile is used outside main()

You've made a syntax error. Remove that brace

int main(){
    ifstream infile;
    ofstream outfile;

    infile.open(" input.txt");
    outfile.open("output.txt");// I wanna link output to functions above and get results // I wann get results on outputfile not on console
    // double a,b,c;
    //infile>>a,b,c;
    powerfunc(3,2);//This functions finds power of 3 and 2
    compare(3,2,9);//This fucnions compare if 3^2=9 then retuens true

} // <- main() ends here
    infile.close();
    outfile.close();
    return 0;
}

Upvotes: 0

cigien
cigien

Reputation: 60258

You can accept the output stream in the function that wants to do output, like this:

double compare(double x, double y,double z, std::ostream &out) {
                                          // ^^^ accept some stream
  out << "hello"; // print whatever output to the stream
}

and then in main, you can call it like this:

outfile.open("output.txt");
compare(3,2,9, std::cout);   // to print to console
compare(3,2,9, outfile);     // to print to file

Upvotes: 1

Related Questions