0

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();
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
unit5016
  • 29
  • 5
  • See [Why !.eof() inside a loop condition is always wrong.](https://stackoverflow.com/q/5605125/9254539) – David C. Rankin Mar 15 '22 at 03:30
  • Does this answer your question? [Why is iostream::eof inside a loop condition (i.e. \`while (!stream.eof())\`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – David C. Rankin Mar 15 '22 at 03:31
  • I suggest you use *only* `std::getline` for actually reading user input, and then use for example `std::stringstream` for parsing the line. Also, for the love any anything that is good, do not use non-constant C strings (`char*` and `char[]`) in C++. Use `std::string`. – hyde Mar 15 '22 at 08:19
  • Or, if you read directly from a stream (`input >> numItems;` etc), don't use `std::getline`. Mixing these will be a head-ache, because then sometimes newlines matter, sometimes they don't, and keeping track of which is which and when, that's going to be messy. – hyde Mar 15 '22 at 08:22

0 Answers0