Only thing I can think of is that my program currently reports one more decimal than in your sample output. This revised version clips the resultant output to two decimal places.
#include <iostream> //provides support for fstream. Also used for printing errors to console.
#include <fstream> //required for file i/o
#include <cstdlib> //required for EXIT_SUCCESS macro, atoi(), atof(), endl, pow()
#include <cmath> //required for sqrt()
using namespace std;
void readToContent(ifstream *input);
double fix(double inputnumber, int decimals);
int main(int argc, char * argv[]) {
//For the sake of brevity, I have left off exception handling for now. This function assumes input is formatted correctly.
if (argc != 3) {
cout << "Syntax:\nprogram.exe NameOfInputFile NameOfOutputFile\n\nTerminating.";
return 1;
}
ifstream input;
ofstream output;
//open the first argument for reading
input.open(argv[1],ios::in);
//open the second arg for writing
output.open(argv[2],ios::out);
//The stream will evaluate to false end of file when the end of file is reached.
while(input) {
int numcases;
input >> numcases;
for(int curcase = 0; curcase < numcases; curcase++){
double curnum;
input >> curnum;
output << "Velocity needed is: " << floor( (100*sqrt(9.8*curnum)) + .5)/100 << endl;
}
//Read to content to force loop exit if there is whitespace ending the file.
readToContent(&input);
//Sets of test cases delmited with a blank line
if (input) {output << endl;};
}
input.close();
output.close();
}
void readToContent(ifstream *input) {
//Skip past whitespace
char temp;
do {
temp = (char) input->get();
} while ((temp == '\n' || temp == ' ') && input);
//once we have read non whitespace, decrement so that is read again for >> later
if (input) {
input->unget();
}
}Compiled version here:
http://billy-oneal.com/BleepingComputer/Gr...922Attempt4.exe
Thanks!
Billy3




Back to top







