c++ - Creating Structures via fstream -
One of the exercises in C ++ primer plus is using a ftream to open a txt file and input data in a composition, then output it to it. The first line of the TCT file is the number of "donors" I think the problem (I think) when I use "infile >> value"; To retrieve the number and then to allocate the structure through the new, is it expected to be an int and its string? is this correct? What should I do differently?
// ch6 p278 exercise # 9 #include & lt; Iostream & gt; # Include & lt; Cstring & gt; # Include & lt; Fstream & gt; # Include & lt; Cstdlib & gt; using namespace std; Const int size = 60; Structure contribution {name's name [20]; Double dollar; }; Four filenames [20]; String donors; Bull donation; Int main () {Four filenames [SIZE]; Cout & lt; & Lt; "Enter the name of the file to scan:"; Cin & gt; & Gt; file name; Fstream inFile; InFile.open (filename); If (! InFile.is_open ()) {cout & lt; & Lt; "Could not open file" & lt; & Lt; File name & lt; & Lt; Endl; Cout & lt; & Lt; "The program is ending. \ N"; Exhaust (EXIT_FAILURE); } Infile & gt; & Gt; Donors; Contribution * ptr = new contribution [donors]; For (int h = 0; h & gt; donor; h ++) {inFile & gt; & Gt; Ptr [bell] .name); InFile & gt; & Gt; Ptr [bell] .dollars); } // Use early seats & lt; & Lt; "Grand Patron: \ n"; For (Int i = 0; I & lt; Donor; I ++) {if (PTR [i]. Dollars & gt; = 10000) {cout & lt; & Lt; PTR [ii] .name & lt; & Lt; "Donation" & lt; & Lt; PTR [I] Darters & lt; & Lt; Endl; Donated = true; }} If (! Donation) {cout & lt; & Lt; "None. \ N";} Donated = Wrong; Cout & lt; & Lt; "Protector: \ n"; For (Int i = 0; I & lt; Data; I ++) {if (PTR [i]. Dollars & lt; 10000) {cout & lt; & Lt; PTR [ii] .name & lt; & Lt; "Donation" & lt; & Lt; PTR [I] Darters & lt; & Lt; Endl; Donated = true; }} If (! Donation) {cout & lt; & Lt; "None. \ N";} Remove PTR; Return 0; }
What should I do differently?
OK, if the code is based on the C ++ prelimer Plus you've taught - use a different text book. I do not want to insult you, but this code is really poor. For example, is the Study :: String Class book covered?
I advise doing junking it, and it is obtained by Koenig; Moo (two of the original C ++ team) who will teach you modern, good, idiomatic C ++