intro C++ operator error?

hello. im in an intro to C++ class and i need some help on my current assignment. im having problem with my exclusionary statement for the if/else loop controlling file output. the program executes but if i give a false output file path, no exclusion statement is printed. i do not want the program to create its own output file. im also open to any other suggestions. i am very new at this and appreciate all help. thanks - garrett

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
ofstream out;
ifstream in;
string ReadFile;
string OutFile;
cout<<"Welcome to Rose State Software Engineering lab."<<endl;
cout<<"We will calculate average carbon foot print and corresponding fine."<<endl;
cout<<"Please be ready to provide the data file name for carbon FP for US cities."<<endl;
cout<<"Please enter the full path of input data file name: ";
getline(cin,ReadFile);
cout<<"Please provide full path to output file: ";
getline(cin,OutFile);

out.open(OutFile.c_str());
in.open(ReadFile.c_str());

if(!out) //this doesnt work!!!! i need correct use of logical operator i think... '!!out' and 'if(!/!!out)' do not work either..
//but i need this next cout statement to come before any factual info is printed to console..
{
cout<<"Failed to open input file. Data will only be written to console."<<endl;
}
else
{

if(in)
{
string name1 = "";
int NameCounter = 0;
double FineTotal = 0;
in>>name1;

cout<<"****************************************************************************"<<endl;
cout<<"City"<<setw(30)<<"Rounded Average Carbon FP"<<setw(30)<<"Fine ($)"<<endl;
cout<<"****************************************************************************"<<endl;

out<<"****************************************************************************"<<endl;
out<<"City"<<setw(30)<<"Rounded Average Carbon FP"<<setw(30)<<"Fine ($)"<<endl;
out<<"****************************************************************************"<<endl;

while(in)
{
int CFP = 0;
int counter = 0;
int sum = 0;
int RDAVG;
double AVG;
NameCounter++;

in>>CFP;

if(CFP<0)
{
cout<<name1;
cout<<" No value available for carbon FP. Fine not calculated."<<endl;

out<<name1;
out<<" No value available for carbon FP. Fine not calculated."<<endl;
}
else
{
cout<<name1<<setw(10);
out<<name1<<setw(10);

while(CFP>=0)
{
sum = sum + CFP;
counter++;
in>>CFP;
}

AVG = (1.0*sum) / counter;
RDAVG = (int)(AVG + 0.5);
cout<<RDAVG<<setw(30);
out<<RDAVG<<setw(30);
double fine;
const double fine1 = 1000000.00;
const double fine2 = 2000000.00;
const double fine3 = 3000000.00;
const double fine4 = 4500000.00;
if(RDAVG>7)
{
fine = fine4;
}
else
if(RDAVG<=7&&RDAVG>5)
{
fine = fine3;
}
else
if(RDAVG<=5&&RDAVG>3)
{
fine = fine2;
}
else
if(RDAVG<=3&&RDAVG>1)
{
fine = fine1;
}
else
{
fine = 0.0;
}

cout<<fixed<<fine<<endl;
out<<fixed<<fine<<endl;
FineTotal = FineTotal + fine;
}
in>>name1;
}
cout<<"****************************************************************************"<<endl;
cout<<"Total number of cities in file = "<<NameCounter<<endl;
cout<<"Total fines collected = ($)"<<FineTotal<<endl;

out<<"****************************************************************************"<<endl;
out<<"Total number of cities in file = "<<NameCounter<<endl;
out<<"Total fines collected = ($)"<<FineTotal<<endl;
}
else
{
cout<<"Error opening input file. File either does not exist or has no read permission."<<endl;
}
}
cout<<"Thank you for using Garret C***** C++ software"<<endl;
out<<"Thank you for using Garret C***** C++ software"<<endl;
cout<<"****************************************************************************"<<endl;
out<<"****************************************************************************"<<endl;
system("pause");
in.close();
out.close();
return 0;

}
Last edited on
also looking for ideas on how to align my columns of output format
Last edited on
ofstreams are defined to create new files when you open files which do not exist yet. Sorry, nothing you can do about that. What you CAN do is create an ifstream with the filename as the operator to check whether the file already exists, like that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int ret = 0;
string outFile = "outfile.txt";
ifstream testStream;
testStream.open(outFile.c_str());
testStream.close();
if(testStream.fail())
{
    cout << "File does not exist!",
    ret = 1;
} else {
//proceed with whatever you wanted to do
}
return ret;


EDIT: For the second thing, see stream manipulators. You can find them all the way down here:
http://www.cplusplus.com/reference/iostream/ (see "Manipulators"- it says iostream, but they work with all other streams aswell)
Last edited on
Topic archived. No new replies allowed.