I'm trying to write a program that reads an input file and takes the values and takes their sum. One of the things I'm trying to do is check for negative numbers, but the if statement for it evaluates true every time, even without negative numbers in the file. The integers have to be all be floats. The specific if statement is:
inFileStream.ignore(20,'A');
inFileStream >> firstVal;
inFileStream.ignore(20, 'B');
inFileStream >> secondVal;
inFileStream.ignore(20, 'C');
inFileStream >> thirdVal;
if (firstVal || secondVal || thirdVal < 0){
cout << string(15, '*') << " Negative Number "
<< string(15, '*') << endl;
if (0 > firstVal){
cout << "==> First number is less than 0" << endl;
}
if (0 > secondVal){
cout << "==> Second number is less than 0" << endl;
}
if (0 > thirdVal){
cout << "==> Third Number is less than 0" << endl;
}
cout << "==> All numbers should be positive " << endl << "==> Terminating program!!!" << endl;
cout << string(47,'*') << endl << endl;
inFileStream.close();
outFileStream.close();
return 1;
}
The full program takes in two inputs as files when run:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <climits>
using namespace std;
int main(int argc, char *argv[])
{
ifstream inFileStream;
ofstream outFileStream;
string inFileName;
string outFileName;
inFileName = argv[1];
outFileName = argv[2];
float firstVal;
float secondVal;
float thirdVal;
float avgVal;
float avgValMeasure;
if (argc != 3) {
cout << endl;
cout << string(9,'*') << " Command Line Argument Error "
<< string(9,'*') << endl;
cout << "==> Incorrect number of Command Line Arguments!\n";
cout << "==> Command for running the program is:\n";
cout << "==> ./Project_05 inputFileName outputFileName\n";
cout << string(47,'*') << endl << endl;
return 1;
}
inFileStream.open(inFileName.c_str());
if (inFileStream.fail()) {
cout << string(15,'*') << " File Open Error "
<< string(15,'*') << endl;
cout << "==> Input file failed to open properly!!\n";
cout << "==> Attempted to open file: " << inFileName << endl;
cout << "==> Terminating program!!!\n";
cout << string(47,'*') << endl << endl;
return 1;
}
if (inFileStream) {
outFileStream.open(outFileName.c_str());
cout << "\nOpening Input File: " << inFileName << endl << "\n";
}
if (outFileStream.fail()) {
cout << string(15,'*') << " File Open Error "
<< string(15,'*') << endl;
cout << "==> Output file failed to open properly!!\n";
cout << "==> Attempted to open file: " << outFileName << endl;
cout << "==> Terminating program!!!\n";
cout << string(47,'*') << endl << endl;
return 1;
}
if (outFileStream) {
cout << "Opening Output File: " << outFileName << endl << "\n";
}
inFileStream.ignore(20,'A');
inFileStream >> firstVal;
inFileStream.ignore(20, 'B');
inFileStream >> secondVal;
inFileStream.ignore(20, 'C');
inFileStream >> thirdVal;
if (firstVal || secondVal || thirdVal < 0){
cout << string(15, '*') << " Negative Number "
<< string(15, '*') << endl;
if (0 > firstVal){
cout << "==> First number is less than 0" << endl;
}
if (0 > secondVal){
cout << "==> Second number is less than 0" << endl;
}
if (0 > thirdVal){
cout << "==> Third Number is less than 0" << endl;
}
cout << "==> All numbers should be positive " << endl << "==> Terminating program!!!" << endl;
cout << string(47,'*') << endl << endl;
inFileStream.close();
outFileStream.close();
return 1;
}
else {
avgVal = (firstVal + secondVal + thirdVal) / 3;
if (avgVal >= 75) {
avgValMeasure = 1;
}
else if (avgVal < 75 && avgVal >= 25){
avgValMeasure = 2;
}
else {
avgValMeasure = 3;
}
}
inFileStream.close();
outFileStream.close();
}
I apologize in advance, I'm very new to C++ programming.
Aucun commentaire:
Enregistrer un commentaire