//This program will take sets of numbers(in 3's) and call
//2 functions, one which will print out how many numbers
//are negative, and one that will find the sum and product
//of the 3 numbers. After it will count how many
//sets have been processed.
#include <iostream>
#include <fstream>
using namespace std;
ofstream myfile;
void introduction();
int howmanynegative(int a,int b,int c);
int findsumandprod(int x, int y);
int main()
{
int first, second, third, neg, num_of_sets=0;
char yes_or_no;
myfile.open("fileOutput.txt");
introduction();//calls the introduction function
cout<<"Please enter any integer then press enter."<<endl;
do{
cin>>first;
cin>>second;
cin>>third;
myfile<<endl<<"The three original integers are "<<first<<" "
<<second<<" "<<third<<"."<<endl;
neg=howmanynegative(first, second, third);//calls the howmanynegative function
myfile<<neg<<" of these numbers are negative."<<endl;
findsumandprod(first,second);//calls the findsumandprod function
findsumandprod(second,third);//calls the findsumandprod function
findsumandprod(third,first);//calls the findsumandprod function
num_of_sets++;
cout<<"Would you like to continue?(y/n)"<<endl;
cin>>yes_or_no;
}while(yes_or_no!='n');
myfile<<endl<<num_of_sets<<" sets have been processed";
myfile.close();
system("pause");
return 0;
}
//Simple function to just print an introduction at the top of the .txt file.
//Does not receive any parameters
void introduction(){
myfile<<"Cosimo Vilardo"<<endl;
myfile<<"This program will take sets of numbers(in 3's) and call "<<endl;
myfile<<"2 functions, one which will print out how many numbers"<<endl;
myfile<<"are negative, and one that will find the sum and product"<<endl;
myfile<<"of the 3 numbers. After it will count how many "<<endl;
myfile<<"sets have been processed."<<endl<<endl;
}
//Function that determines how many of the numbers are negative.
//Receives int first, second and third and returns "negative" which is the total
//amount of negative numbers out of the three received.
int howmanynegative(int a,int b,int c){
int negative;
//Function will calculate the sum and the product of the two values it receives.
//The main program calls this function 3 times and sends 2 values.
//First call- Receives: "first" and "second".
//Second call- Receives: "second" and "third".
//Third call- Receives: "third" and "first".
//This prints the sum and product from the function its self.
int findsumandprod(int x, int y){
int sum, prod;
sum=x+y;
prod=x*y;
myfile<<x<<" and "<<y<<":"<<" Sum="<<sum<<" "<<"Product="<<prod<<endl;
return 0;
}
Use [_code_][/_code] tags please (without the underscores). It makes the code easier to read.
There's three possible reasons why your problem is occurring (maybe more):
1) The file was in fact created but it's somewhere else (this happened to me before).
2) std::ofstream failed to create your file.
3) std::ofstream created your file but failed to open it.
For No.1, check your entire project folder structure and see if the file is there. For No.2 & 3, check if std::ofstream::is_open( ) returns false. If it does, then your file isn't ready to be written to.
When a file is made with std::fstream, it's created within the same directory as the program (known as the working directory) by default. If the file already exists, the existing file is used instead.