help with files

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
Description:   You are shopping at your favorite store. You select one item 
               of merchandise marked at a listed price. At the checkout, you 
               are asked whether you have a coupon. If  not, you will be 
               charged the regular price. If you have a coupon, the cashier 
               will determine whether it is a cash-off coupon, or a percent-off 
               coupon. A cash-off coupon will reduce the marked price by a 
               fixed amount; a percent off-off coupon will reduce the price 
               by a percentage.

================ 

INPUT FILE:   (shoppers.dat)  NOTE: not every line has 4 items!!!
                              coupontype and couponvalue given ONLY when 
                              couponused is Y.
 
               CONTENT: 
                 itemcost couponused [coupontype couponvalue]

               FORMAT:  
                 xxx.xx  x  x  xxx.xx 

               EXAMPLE:  
                  49.99  Y  C    2.49 
                 80.00  Y  P   25.00
                 280.00  N  
                  87.33  Y  P  100.00 
                   2.99  Y  C    2.00 

SCREEN OUTPUTS: (same as PROG6a) PLUS ...

               Error Message: FATAL ERROR - CAN NOT OPEN FILE shoppers.dat
                     when input file can not be found/opened.


============================================================
ALGORITHM:
============================================================

...
int main()
{
   // Declare variables.
   ifstream shopperF("shoppers.dat");

   //-| PROCESS ONE SHOPPER
   1. Open input file.
      1.1 Display following message and exit if file can not be opened.

            "FATAL ERROR - CAN NOT OPEN FILE shoppers.dat"

   2. Read regularPrice, couponUsed.
      **************************************
      shopperF >> regularPrice >> couponUsed;
      **************************************

   3. If coupon is used
      3.1 Read couponType, couponValue;
          **************************************
          shopperF >> couponType >> couponValue;

      3.2 if type is C
          3.2.1 Compute Savings = couponValue
      3.3 else
          3.3.1 Compute Savings = regularPrice * couponValue/100.0
      3.4 Compute amountPaid = regularPrice - Savings
      3.5 Display regularPrice, couponType, Savings, amountPaid.

   4. else
      4.1 Compute amountPaid = regularPrice
      4.2 Display regularPrice, amountPaid.

   5. close input file.
}


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
// -------------------------------------------------------------------------------
// COP 3014C Fundamentals of Programming
//
// Assignment ID:   PROG6b
//
// File name:   coupons_b.cpp
//
// 
// 
// Description: You are shopping at your favorite store. You select one item 
//             of merchandise marked at its regular price. At the checkout, 
//             you are asked whether you have a coupon. If  you do not, you 
//             will be charged the regular price. If you have a coupon, the 
//             cashier will determine whether it is a cash-off coupon, or 
//             a percent-off coupon. A cash-off coupon will reduce the regular
//             price by a fixed cash amount; a percent-off coupon will reduce 
//             the price by a percentage of the regular price.
// 
//  NOTE:        This is the FIRST STEP in the evolution of this program.
//               ONE SHOPPER / keyboard INPUT / console OUTPUT
//---------------------------------------------------------------------------------

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
cout << "(c) 2012, bfields Byron Fields" << endl;
float regularPrice;
char couponUsed;
char couponType;
float couponValue;  // percent is entered as a whole number.
float Savings;
float amountPaid;
ifstream shopperF("shoppers.dat");

shopperF.open("shoppers.dat")

if (shopperF.fail())
{
cout << "FATAL ERROR - CAN NOT OPEN FILE shoppers.dat" << endl;
       exit(1); 
}

shopperF >> regularPrice >> couponUsed;
if (couponUsed == 'Y')
{
shopperF >> couponType;
if(couponType == 'C')
{ 
shopperF >> couponValue;
Savings = couponValue;
amountPaid = regularPrice - Savings;
cout << setw(16) << "ITEM COST   = $ " << fixed << setprecision(2) << regularPrice << endl;
cout << setw(14) << "COUPON TYPE = " << couponType << endl;
cout << setw(16) << "SAVINGS     = $ " << fixed << setprecision(2) << Savings << endl;
cout << setw(16) << "PAYMENT DUE = $ " << fixed << setprecision(2) << amountPaid << endl;
}
else if(couponType == 'P')
{ 
shopperF >> couponValue;
Savings = regularPrice * couponValue/100.0;
amountPaid = regularPrice - Savings;
cout << setw(16) << "ITEM COST   = $ " << fixed << setprecision(2) << regularPrice << endl;
cout << setw(14) << "COUPON TYPE = " << couponType << endl;
cout << setw(16) << "SAVINGS     = $ " << fixed << setprecision(2) << Savings << endl;
cout << setw(16) << "PAYMENT DUE = $ " << fixed << setprecision(2) << amountPaid << endl;
}
}
else 
{
amountPaid = regularPrice;
cout << setw(16) << "ITEM COST   = $ " << fixed << setprecision(2) << regularPrice << endl;
cout << setw(16) << "PAYMENT DUE = $ " << fixed << setprecision(2) << amountPaid << endl;
}
shopperF.close("shoppers.dat");
return 0;
}
im having problems with reading from the files and I guess the opening and closing of the file
error i get:
1
2
3
4
5
6
7
8
 coupons_b.cpp:2: parse error before `--' token
In file included from /usr/local/include/c++/3.2.2/iosfwd:45,
                 from /usr/local/include/c++/3.2.2/ios:44,
                 from /usr/local/include/c++/3.2.2/ostream:45,
                 from /usr/local/include/c++/3.2.2/iostream:45,
                 from coupons_b.cpp:31:
/usr/local/include/c++/3.2.2/bits/stringfwd.h:55: parse error before `<' token
/usr/local/include/c++/3.2.2/bits/stringfwd.h:58: confused by earlier errors, bailing out
Topic archived. No new replies allowed.