I'm done with my assignment for a beginner C++ course, and the only annoying thing is I cannot get rid of the extra space that appears on the screen when I input data from a file and print to screen while outputting to another file.
I have looked into the input.ignore()
and putback()
functions, but I cannot get them to work while messing around.
I know this is cause the extra \n
character is at the end of the first line, or cause I skipped the comma.
Any tips would be great, in terms of formatting a .ignore()
or .putback()
, as the professor rushed through explaining it at the end of class as we were leaving without examples.
It outputs to the screen as such:
John 13333 .69 // one extra space after John for every number and item
Susie 12222 .75...
#include <iomanip> //include directives to use various keywords
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include <limits>
using std::cin; //various usings to avoid namespace std
using std::cout;
using std::endl;
using std::ifstream;
using std::ofstream;
using std::string;
using std:: left;
using std::setw;
using std::numeric_limits;
void getName(string & fileName);
void readandsend(ifstream &input,ofstream &output);
int main()
{
//my variables I will use in function calling
string fileN;
ifstream input;
ofstream output;
getName(fileN); //calls to functions
input.open(fileN); //opens file to be streamed for input
output.open("Assgn6-BB.txt");
if (!input)
{
cout << "The input file failed to open! Try again." << endl; //checks that the file opened correctly
return -1;
}
cout << "============================================================" << endl; //prints header
cout << "=" << " ";
cout << "FARMER'S MARKET INVENTORY" <<" " << "=" <<endl;
cout << "============================================================" << endl;
readandsend(input, output);
return 0;
}
void getName(string & fileName)
{
//pre-conditions: a corrcet filename is entered by the user that exists and has data
//post-conditions: a filename is entered and processed without error to be used later
cout << "Enter the name of the file: "; //takes in file name
cin >> fileName;
}
void readandsend(ifstream &input,ofstream &output)
{
//pre-conditions: filename is a correct file that exists and has data that is opened correctly
//post-conditions: prints out the data from the file until the end of the file is reached while outputting the data and calculations to another file
char farm[25];
string item;
int numItems;
double pricePer, subTotal, total = 0, totalItems = 0;
input.getline(farm,25,','); //reads in first set of data to make sure the standard input.eof reads correctly
input >> numItems;
input >> item;
input >> pricePer;
totalItems += numItems;
total +=(pricePer * numItems);
while(!(input.eof()))
{
output << left << setw(25) << farm //sends to output file
<< setw(10) << numItems << setw(15)
<< item << setw(8) << pricePer
<< setw(8) << (numItems * pricePer);
cout << left << setw(25) << farm //prints to screen to verify
<< setw(10) << numItems
<< setw(15) << item
<< setw(8) << pricePer
<< setw(8) << (numItems*pricePer) << endl;
input.getline(farm, 25, ',');
input >> numItems;
input >> item;
input >> pricePer;
totalItems += numItems;
total += (pricePer * numItems);
}
cout << left << setw(24) << "Grand Total: " << totalItems << " items's totaling $" << total << endl;
input.close(); //closes the files
output.close();
}