2

I have to create a convoluted program to read 3 values from a file, calculate the average, and output it to a file, all using different functions, and the function that gets the input from a file needs to be called 3 times separately with no fancy stuff. I'm incrementing a variable by 1 each time and input is taken and skipping lines accordingly. It seems to get the first 2 inputs fine when using ignore() until the newline character, but when using it twice to skip the first 2 inputs and get the third, it seems to be getting the second input again and using that to calculate the average. For example I got the average of 5,7 and 10 as 6.3~ when it should be 7.3~. Am I doing something wrong by using ignore until the newline character twice? I've included the code from my main and the two relevant functions.

int main() {

    double input_1, input_2, input_3;
    double average;
    int count = 1;

    ifstream infile;
    ofstream outfile;
    infile.open("infile.txt");
    outfile.open("outfile.txt");

    input_1 = get_input(infile, count);
    count = count+1;

    input_2 = get_input(infile, count);
    count=count+1;

    input_3 = get_input(infile, count);

    average = calculate_avg(input_1, input_2, input_3);

    output_to_file(average, outfile);

    infile.close();
    outfile.close();

    return 0;
}

double get_input(ifstream& infile,  int number) {

    double input_value;

    if (number == 1) {
        infile >> input_value;
    }
    else if (number == 2)
    {
        infile.ignore (100, '\n');
        infile >> input_value;
    }
    else if (number == 3)
    {
        infile.ignore (100, '\n');
        infile.ignore (100, '\n');
        infile >> input_value;
    }
    return input_value;
}

double calculate_avg(double val_1, double val_2, double val_3){
    double avg;

    avg = (val_1 + val_2 + val_3)/3;

    return avg;
}
1
  • 1
    Please don't put TABs in your code samples here! Edited this time ... Oct 21, 2013 at 18:19

2 Answers 2

2

std::fstream::ignore will extract and discard characters from the input stream, so 100 won't be the best choice

Use following in get_input() for sake of "no fancy stuff"

infile.seekg(0); // rewind
if (number == 1) {
    infile >> input_value;
}
else if (number == 2)
{
    infile >> input_value;
    infile >> input_value;
}
else if (number == 3)
{
    infile >> input_value;
    infile >> input_value;
    infile >> input_value;
}
return input_value; //only the last read value
1
  • Thanks so much! I hadn't realized it would only return the last read value, I thought it would cause some kind of error. I guess I should test stuff out more. Oct 21, 2013 at 18:37
1

write function get_input as:

double get_input(ifstream& infile,  int number) {
    double input_value;
    infile >> input_value;
    infile.ignore (1, '\n');
    return input_value;
}

the will work because you opened file and then reads it consequentially, so after you read number - internal pointer is at \n symbol, and you do not have to skip several lines, you need just to skip \n symbol

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.