File I/O Problem

I'm trying to write a program that has to read 10 names from an input file and each name has to have 5 grades. The 5 grades will be in another input file. They have to show up in the output file.

I have done the input file and the output file...I want to display one column with 6 rows for the output file. How can I do that?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  // Headers and Other Technical Items

#include <iostream> 
#include <fstream>
#include <iomanip> 

using namespace std;

// Function Prototypes

void pause(void);

// Variables

ifstream inData; 
ofstream outData; 

char Carlos
char Jonathan
char Carmen
char Kathy
char Natalie
char John 
char Sean
char Doug
char Yasmin
char Brad

int  test_1, test_2, test_3, test_4, test_5; 
int  test_6, test_7, test_8, test_9, test_10; 
int  test_11, test_12, test_13, test_14, test_15;
int  test_16, test_17, test_18, test_19, test_20; 
int  test_21, test_22, test_23, test_24, test_25; 
int  test_26, test_27, test_28, test_29, test_30;
int  test_31, test_32, test_33, test_34, test_35; 
int  test_36, test_37, test_38, test_39, test_40;
int  test_41, test_42, test_43, test_44, test_45;
int  test_46, test_47, test_48, test_49, test_50; 


//******************************************************
// main
//******************************************************

int main()
{

//Open files

inData.open ("Names.txt"); //open input file
outData.open ("Grades.txt"); //open output file

//code for data manipulation

cout << setw (1) << Carlos << end1; 
cout << setw (1) << Jonathan << end1; 
cout << setw (1) << Carmen << end1; 
cout << setw (1) << Kathy << end1; 
cout << setw (1) << Natalie << end1; 
cout << setw (1) << John << end1; 
cout << setw (1) << Sean << end1; 
cout << setw (1) << Doug << end1;  
cout << setw (1) << Yasmin << end1; 
cout << setw (1) << Brad << end1; 



//close files

inData.close();
outData.close(); 

 
  
 
  return 0;
}






Input file looks like:

Carlos

Jonathan

Carmen

Kathy

Natalie

John

Sean

Doug

Yasmin

Brad

Output file:

86, 98, 100, 89, 90

80, 81, 100, 78, 100

86, 88, 90, 100, 76

87, 99, 100, 87, 75

98, 100, 98, 99, 100

80, 83, 98, 78, 100

87, 90, 87, 90, 100

89, 90, 100, 87, 100

78, 76, 79, 80, 86

100, 78, 98, 84, 83
Last edited on
I received a layout for the program, but I don't understand some things...
Why have a file called "Gradesandnames"? What is the loop trying to do exactly there? How can arrays be arranged to get the columns and rows I want?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using  namespace std;
// Function Prototypes
void
 
 
 pause(void);
 
// Variables
ifstream inData;
ofstream outData; 
 
//******************************************************
// main
//******************************************************
int
 
 
 main()
{
string arraynames[10];
//Open files
inData.open (
 
"Names.txt"); //open input file for names
indatagrades(
 
"grades.txt")  //for grades like 10 20 30 40 50  without comma, just a space between them
outData.open (
 
"Gradesandnames.txt"); //open output file
// Here you need to write a loop to read names from names.txt
for
 
 
 (int i= 0; i < 10; col++)
getline(indata , arrayname[i]);
//to get grades
for
 
 
 (int i= 0; i < 10; i++)
   
 
for (int j= 0; j<5; j++)
       indatagrades>>arraygrades[i][i];
 
//now two loops again to write in into your data file
 
 
 
 
 
//close files
inData.close();
outData.close();
 
 
 
 
 
return 0;
}




Last edited on
I know this dosent answer your question but some advice insted of writing test1 test2 test3 etc use an array!
I understand how that I need to write 2D arrays...But why do I need an output file called gradesandnames?

The input file I made has the name and the output the grades.

How do I have to connect those two files to get a gradesandnames file? I mean, if I'm suppose to have that file, what am I suppose to write on it?

I don't understand what the code is trying to do.

I'm using Visual Studio 2010 Express version.
Last edited on
Topic archived. No new replies allowed.