Easiest way?

What is the easiest way to read from this input (string and numbers) file and restrict an Id number with more than four numbers & read the full name
Miss Informed
125432 32560.0
Sweet Tooth
5432 9500
Bad Data
1255 -4500.0
John Smith
1225 3500.0
Nancy Brown
1555 154500.00

I am dealing with functions and I've already created bool function to read but I am lost with the rest.


I know (!fin.eof()) but I am having trouble with looping all this information, thank you in advance.
Last edited on
I know (!fin.eof())
- you mean you know that using this is a bad idea and should be avoided, right?


in the case of your data, it looks a bit like this to me.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
    ifstream fin("input1.txt");

    string name;
    int num1;
    double num2;
    
    while (getline(fin, name) && (fin >> num1 >> num2 >> ws))
    {
        cout << setw(20) << left  << name 
             << setw(8)  << right << num1 
             << setw(10) << num2  << endl;   
    }
}

Output:
Miss Informed         125432     32560
Sweet Tooth             5432      9500
Bad Data                1255     -4500
John Smith              1225      3500
Nancy Brown             1555    154500


If you need additional validation (e.g. num1 must be no greater than 9999), you could add code for that within the while loop.
thank you that helped

can you take a look at my whole code? Im still having plenty of issues with getting the table I'm supposed to get please

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;



int main()
{
    
    float CalcIncomeTax(float );
    float CalcNetSalary(float, float );
    void OpenFile(ifstream& fin);
    void OpenFile(ofstream& fout);
    void Instruct(void);
    void ReadData(ifstream & fin, string& Name , int &Id, float& grossIncome);
    void Print(ofstream&, string, int, float, float, float);
    
    
    ifstream    fin;
    ofstream    fout;
    string      Name;
    int         Id;
    float       grossIncome;
    float       netSalary;
    float       incomeTax;
 
    
    Instruct ();
    
    OpenFile(fin);
{
    while (getline(fin, Name) && (fin >> Id >> grossIncome))
    {
        cout << setw(20) << left  << Name
        << setw(8)  << right << Id
        << setw(10) << grossIncome  << endl;
    }
    
}
  OpenFile(fout);

ReadData(fin, Name, Id, grossIncome);
    while(!fin.eof())
    {
        netSalary = CalcNetSalary(grossIncome,incomeTax);
        incomeTax = CalcIncomeTax(grossIncome);
        Print(fout, Name, Id, grossIncome, incomeTax, netSalary);
        ReadData(fin, Name, Id, grossIncome);
    }
    fin.close();
}

    void OpenFile(ifstream&fin)
{
    cout <<"\nEnter the name and location of the input file:   ";
    string file_input;
    getline(cin, file_input);
    fin.open(file_input.c_str() ) ;
        while(fin.fail())
        {
            fin.clear();
            cout<<endl<<"\aError opening the inputfile! Please try again"<<endl<<endl;
            cout<<"Enter the name of the filename: ";
            getline (cin, file_input);
            fin.open(file_input.c_str());
        }
}
void OpenFile(ofstream &fout)
{
    cout <<"Enter the name and location of the output file:   ";
    string file_output;
    getline(cin, file_output);
    fout.open( file_output.c_str() );
    while(fout.fail())
    {
        fout.clear();
        cout<<endl<<"\aError opening the inputfile! Please try again"<<endl<<endl;
        cout<<"Enter the name of the filename: ";
        getline (cin, file_output);
        fout.open(file_output.c_str());
    }
    
}

void Instruct()
{
    cout << "Programmer:"<< setw(25) << "//" << endl;
    cout << "Programming Assignment" << setw(5) << "4" << endl;
    cout << "This program will calculate and report tax liability" << endl;
    }
    
    float CalcIncomeTax(float grossIncome)
    {
        float incomeTax = 0;
        
        if (grossIncome <= 3500)
        {
            incomeTax = 0.00;
        }
        else if (grossIncome >= 3500 && grossIncome <= 8000)
        {
            incomeTax = 0 + 0.06 * (grossIncome - 3500);
        }
        else if (grossIncome >= 8000 && grossIncome <= 20000)
        {
            incomeTax = 270.00 + 0.11 * (grossIncome - 8000);
        }
        else if (grossIncome >= 20000 && grossIncome <= 34000)
        {
            incomeTax = 1590.00 + 0.17 * (grossIncome - 20000);
        }
        else if (grossIncome >= 34000 && grossIncome <= 54000)
        {
            incomeTax = 3970.00 + 0.24 * ( grossIncome - 34000);
        }
        else if (grossIncome >= 54000)
        {
            incomeTax = 8770.00 + 0.32 * ( grossIncome - 52000);
        }
        else if (grossIncome < 0)
        {
            cout << "****Invalid Income";
        }
        return(incomeTax);
    }

    
    float CalcNetSalary( float grossIncome, float incomeTax)
    {
        float netSalary;
        netSalary = grossIncome - incomeTax;
        return (netSalary);
    }
    
    void Print(ofstream& fout, string Name, int Id, float grossIncome, float incomeTax, float netSalary)
    {
        cout << setfill(' ') << left << setw(18) << "\tName";
        cout << setfill(' ') << left << setw(12) << "ID";
        cout << setfill(' ') << left << setw(17) << "Gross Income";
        cout << setfill(' ') << left << setw(12) << "Taxes";
        cout << setfill(' ') << left << setw(16) << "Net Income";
        cout << endl;
        
        cout << setfill('=') << setw(70)<<"\t";
        cout<<endl;
        
        cout << setprecision(2) << showpoint << fixed;
        cout << setfill(' ') << "\t" << setw(17)<< Name;
        cout << setfill(' ') << setw(12) << Id;
        cout << '$' << setfill(' ') << setw(16) << grossIncome;
        cout << '$' << setfill(' ') << setw(11) << incomeTax;
        cout << '$' << setfill(' ') << setw(16) << netSalary;
        cout << endl;
        return;
    }




output should look like this:

1
2
3
4
5
6
Name                   ID           Gross Income        Taxes           Net Income 
Miss Informed          125432       $32560.00     **** Invalid ID
Sweet Tooth            5432         $9500.00           $435.00           $9065.00
Bad Data               1255         $-4500.00      **** Invalid Income
John Smith             1225         $3500.00           $0.00             $3500.00
Nancy Brown            1555         $154500.00         $40930.00         $113570.00
AFAIK you only have to set the fill once if you are using the same fill the entire time. Also you shouldn't even need to set the fill the setw should automatically put spaces for you just change where the text is outputted ( left/right/center) like chervil did earlier.
thank you I'll correct that. But my problem is my code isn't working and my output is coming out horribly like this :

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
Programmer:                       //
Programming Assignment    4
This program will calculate and report tax liability

Enter the name and location of the input file:   /Users/MJ/Desktop/incomes.txt
 ***Invalid Id.
Enter the name and location of the output file:   /Users/MJ/Desktop/incomefdfd.txt
125432 32560.0 Bad data type in the file.
 Record will be skipped.
Sweet Bad data type in the file.
 Record will be skipped.
	Name             ID          Gross Income     Taxes       Net Income      
	=====================================================================
	Tooth            5432        $9500.00         $435.00     $-28217813638685130752.00
Bad Bad data type in the file.
 Record will be skipped.
	Name             ID          Gross Income     Taxes       Net Income      
	=====================================================================
	Data             1255        $-4500.00        $0.00       $-4935.00        
John Bad data type in the file.
 Record will be skipped.
	Name             ID          Gross Income     Taxes       Net Income      
	=====================================================================
	Smith            1225        $3500.00         $0.00       $3500.00         
Nancy Bad data type in the file.
 Record will be skipped.



I'm trying everything to fix it into the output Im supposed to get. any advice on what I'm doing wrong.
anyone?
Topic archived. No new replies allowed.