Use of fstream, arrays, and strings

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
#include <iostream>
#include <cmath>
#include <string>
#include <fstream>

using namespace std;




int main()

{
    const int size = 30; // values for x,t
    const int Size = 100; // for the characters
    double t[size];  //array for t
    double x[size]; //arrays for x
    
    string a[Size];
    int Count = 0;
    string FName;   // variable file name for data file
    /////////
    
    cout << "Please enter the name of the data file (Example: Data.dat): ";
    cin >> FName;
    ifstream InData;
    InData.open(FName.c_str());   //Open file and convert name to C-style string
    while(InData.fail())
    {
       cout << "Can't open file. Bad file name? Please re-enter file name: ";
       cin >> FName;
       InData.clear();   // clear fail bit  
       InData.open(FName.c_str());   //Open file and convert name to C-style str
    }
    cout << "File opened successfully!" << endl;
   
   do
   { getline (InData, a[Count]);
   Count++;}
   while (!InData.eof() && Count < Size);
   Count --;
   for (int j = 0; j < Count; j++)
   cout << a[j] <<  endl;
   
     system("Pause");
    return 0;
}   


Test vehicle:  Car #1
Test date:     October 26, 2007
Driver:        D. Myers
0.0     0.0
0.2     5.5
0.4     15.9
0.6     30.9
0.8     49.7
1.0     71.8
1.2     97.1
1.4     125.4
1.6     156.6
1.8     190.4
2.0     226.6
2.2     265.3
2.4     306.1
2.6     348.9
2.8     393.5
3.0     439.9
3.2     488.0
3.4     537.7
3.6     588.8
3.8     641.2
4.0     694.6
4.2     749.2
4.4     804.7
4.6     861.2
4.8     918.4
5.0     976.2

^ Save as a data file. In this case as "CAR1.DAT"

I am still a beginner in C++.
My objective is I want to be able to store the numbers only in the data file to an array, so I can be able to use them in functions. I figured how to save them into an array but I know its just using the array for my set up for the top 3 lines.I guess it saved everything as a string.

0.0     0.0
0.2     5.5
0.4     15.9
0.6     30.9
0.8     49.7
1.0     71.8
1.2     97.1
1.4     125.4
1.6     156.6
1.8     190.4
2.0     226.6
2.2     265.3
2.4     306.1
2.6     348.9
2.8     393.5
3.0     439.9
3.2     488.0
3.4     537.7
3.6     588.8
3.8     641.2
4.0     694.6
4.2     749.2
4.4     804.7
4.6     861.2
4.8     918.4
5.0     976.2


^ I want to use these numbers in functions but I need to separate it from the top 3 lines.



Topic archived. No new replies allowed.