Opening files

So I' am trying to read from a file and the end of this code, but nothing is really coming up. I have pretty muh stumped myself and im pretty much second guessomg everything. Can anyone point me in the right direction.

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>


using namespace std;

int main ()

{
    float dpayAmount = 1700.00;
    float dannualPay = 0.0;
    int payPeriods = 26;
    
    cout<<   "Annual Pay Calculator";
    cout<< "\n";
    cout<< "\n";
    
    dannualPay = (dpayAmount * payPeriods);
    

    cout<< setprecision (2) << setiosflags (ios :: fixed) << setiosflags(ios::showpoint)<<
      "You were payed $" << dannualPay;
    
    
    cin.get ();
    system ("cls");
    


    float duserAmount = 0.0;
    float duserAnnual = 0.0;
    int duserPayPeriods = 0;
    
    cout<< " Welcome to the Annual Pay Calculator ";
    cout<< "\n";
    cout<< "\n";
    cout<< "How much do you make each pay period?____________$";
    cin>> duserAmount;
    cout<< "\n";
    cout<< "\n";
    cout<< "How many pay periods do you have annually?________";
    cin>> duserPayPeriods;
    cout<< "\n";
    cout<< "\n";
    
    duserAnnual = (duserAmount * duserPayPeriods);
    
    cout<< "This year you have made._________________________$"<< duserAnnual;
    
    cin.get();
    cin.ignore ();
    system ("cls");
    



    float dfileAmount = 1700.0;
    float dfileAnnual = 0.0;
    int filePayPeriods = 26;
     string line;
    
    ofstream myfile;
    myfile.open ("output.txt");
    myfile << "Welcome to the Annual Pay Calculator";
    myfile << "\n";
    myfile << "\n";
    dfileAnnual = (dfileAmount * filePayPeriods);
    myfile << "You were payed $" << dfileAmount<<
    " every pay period";
    myfile << "\n";
    myfile << "\n";
    myfile << " You had " << filePayPeriods << " pay periods this year ";
    myfile << "\n";
    myfile << "\n";
    myfile << " So this year you were payed: $" << dfileAnnual;
    
    
    cin.ignore ();
    cin.get ();
    
    
  
    {
    ifstream myfile ("input.txt");
    myfile.open ("input.txt");
    
   
    
    if (myfile.is_open())
    
    {
                         getline (myfile,line);
                         cout << line << endl;
                         }
    
}
}

    
    
Just so I understand what you're trying to accomplish:

1) You gather information from the user, and write it to a file named "output.txt"

2) You then attempt to open a file named "input.txt" and display 1 line of it.

Are my assumptions correct?
Last edited on
ifstream myfile;
myfile.open ("input.txt");

Isn't it the right way of opening a file?
http://recurseit.blogspot.com/2011/09/how-to-read-input-from-txt-file.html
ifstream is for when you want to read from a file.

The problem is this:

1
2
ifstream myfile ("input.txt"); //create new ifstream, open file input.txt
myfile.open ("input.txt"); //open file input.txt again - I am pretty sure that sets myfile in fail state 
hi I think i found yor problem you forgot to declair (system)

#include <stdio.h>
#include <stdlib.h>

add these 2 line's
1
2
#include <stdio.h>
#include <stdlib.h> 


Those are c headers. The C++ versions look like this:

1
2
#include <cstdio>
#include <cstdlib> 


And chances are that one of the headers he includes already includes cstdlib, that's why he probably didn't get any compile errors.
Sorry, what I'm trying to do is just display whatever is written in the input.txt file. I have gotten only the first line to appear now.

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdio>
#include <cstdlib>

using namespace std;

int main ()

{
    float dpayAmount = 1700.00;
    float dannualPay = 0.0;
    int payPeriods = 26;
    
    cout<<   "Annual Pay Calculator";
    cout<< "\n";
    cout<< "\n";
    
    dannualPay = (dpayAmount * payPeriods);
    

    cout<< setprecision (2) << setiosflags (ios :: fixed) << setiosflags(ios::showpoint)<<
      "You were payed $" << dannualPay;
    
    
    cin.get ();
    system ("cls");
    
    float duserAmount = 0.0;
    float duserAnnual = 0.0;
    int duserPayPeriods = 0;
    
    cout<< " Welcome to the Annual Pay Calculator ";
    cout<< "\n";
    cout<< "\n";
    cout<< "How much do you make each pay period?____________$";
    cin>> duserAmount;
    cout<< "\n";
    cout<< "\n";
    cout<< "How many pay periods do you have annually?________";
    cin>> duserPayPeriods;
    cout<< "\n";
    cout<< "\n";
    
    duserAnnual = (duserAmount * duserPayPeriods);
    
    cout<< "This year you have made._________________________$"<< duserAnnual;
    
    cin.get();
    cin.ignore ();
    system ("cls");
    

    float dfileAmount = 1700.0;
    float dfileAnnual = 0.0;
    int filePayPeriods = 26;
     string line;
    
    ofstream myfile;
    myfile.open ("output.txt");
    myfile << "Welcome to the Annual Pay Calculator";
    myfile << "\n";
    myfile << "\n";
    dfileAnnual = (dfileAmount * filePayPeriods);
    myfile << "You were payed $" << dfileAmount<<
    " every pay period";
    myfile << "\n";
    myfile << "\n";
    myfile << " You had " << filePayPeriods << " pay periods this year ";
    myfile << "\n";
    myfile << "\n";
    myfile << " So this year you were payed: $" << dfileAnnual;
    
    
    cin.ignore ();
    cin.get ();
    
    {
    ifstream myfile;
    string line1;
    string line2;
    string line3;
    string line4;
    string line5;
    string line6;
    string line7;
    string line8;
    
    myfile.open("input.txt");
    
    myfile >> line1;
    myfile >> line2;
    myfile >> line3;
    myfile >> line4;
    
    cout << line1 << " " <<
   
    cout << line2 << " " <<
    
    cout << line3 << " " <<
    
    cout << line4 << " " <<      
    
    cin.ignore();
    cin.ignore();
    
    cin.ignore();
    cin.get();
    
}
}
 
Use istream::getline if you want to read lines - the >> operator is "stupid" in that it just blocks at a newline character.
Also, you are writing to a file named "output.txt" but reading a file named "input.txt" is that really what you want?
Topic archived. No new replies allowed.