-2

I need to read the marks.dat file and output that file onto the screen.

marks.dat

23468555 88 55 67
55223344 45 90 78
32321111 67 89 73
77001234 79 59 99
21324354 62 84 88
40450765 34 45 56
26726826 93 69 71
30917823 57 65 80
54277888 87 77 55

The 8-digit number represents the student number and the following 3 2-digit numbers represent the assignment marks, calculate the average of the 3 that contributes 40% of the year mark, find the highest mark that contributes 60% of the year mark, then output the student number, followed by the average, followed by the year mark, i.e

yearmark.dat

23468555 70.00 80.80

The average and year mark should be of type double and display only 2 digits after the decimal, then output to the file and display the contents on the screen.

The problem I have is that trying to output the double of the 8-digit number outputs 234686e+007, however if its int, it then outputs correctly.

Would also like to see how you would tackle this problem. Would you try to create an array to insert the contents of the file, was thinking 2-d, first student number then the marks in the second dimension?

Community
  • 1
  • 1
Spaz
  • 9
  • 1
    There is *lots* of text that is not really related to the problem at all -- which is, the printing of `23468555` gives unexpected results. **The actual question is almost hidden.** There is *too little* information on *how* you are printing that value; something like `int main() { double x = 23468555; std::cout << x << std::endl; }` would at least tell us you're using `cout` (as opposed to, say, `printf()`). The last paragraph would be off-topic, as SO is about actual programming problems that could be answered "correctly", not discussions about "how to approach this problem". – DevSolar Feb 22 '16 at 10:34
  • 1
    @DevSolar that question and its answers was about outputting a double with **full** precision. The OP wants only 2 digits and that's not clearly stated there. – Bob__ Feb 22 '16 at 10:58
  • @Bob__: Correct. Reopened the question so you can post your more correct answer. (Sorry, didn't bother to dig through the prose and skipped to "The problem is...", thus missing the formatting requirement in the previous paragraph.) – DevSolar Feb 22 '16 at 11:00

1 Answers1

1

To print only 2 digits of a double you can take advantage of the input/output manipulators of the standard library:

#include <iostream>
#include <iomanip>  // for fixed and setprecision

// ... 
double x = 89.12345678;
double y = 67.77777777;

// to print out "89.12 67.78":
std::cout << std::fixed << std::setprecision(2) << x << ' ' << y << '\n';

The student number should be an int (or a string in some cases) more then a double, but if you want to print a double without any digit after the decimal point:

double n = 23468555;
std::cout << std::fixed << std::setprecision(0) << n << '\n';

As an advise for your task, I'd avoid a 2D array (and arrays in general).

Create a class (or a struct, it is the same) to collect the student number and the averages, then store all the data you read in a std::vector of those structs.

If you don't already know anything of what I'm talking about... Define 3 arrays to store the numbers.

Bob__
  • 12,361
  • 3
  • 28
  • 42