2 Replies - 329 Views - Last Post: 13 March 2013 - 07:28 PM Rate Topic: -----

#1 Speakeasys  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 19-September 12

Confused on the scope resolution operator.

Posted 11 March 2013 - 09:36 AM

 32 #include <iostream>
 33 #include <fstream>
 34 #include <cstdlib>
 35 #include <cctype>
 36 using namespace std;
 37 //Class for amounts of money in U.S. currency. 
 38 class Money
 39 {
 40 public:
 41     friend Money operator +(const Money& amount1, const Money& amount2);
 42     friend Money operator -(const Money& amount1, const Money& amount2);
 43     friend Money operator -(const Money& amount);
 44     friend bool operator ==(const Money& amount1, const Money& amoun2);
 45     friend bool operator <(const Money& amount1, const Money& amoun2);
 46     friend bool operator >(const Money& amount1, const Money& amoun2);
 47     friend bool operator <=(const Money& amount1, const Money& amoun2);
 48     friend bool operator >=(const Money& amount1, const Money& amoun2);
 49     //Precondition: amount1 and amount2 have been given values.
 50     //Returns true if amount1 is less than amount2;
 51     //otherwise, returns false.
 52 Money(long dollars, int cents);
 53     Money(long dollars);
 54     Money( );
 55     double get_value( ) const;
 56     friend istream& operator >>(istream& ins, Money& amount);
 57     //Overloads the >> operator so it can be used to input values of type Money.
 58     //Notation for inputting negative amounts is as in -$100.00.
 59     //Precondition: If ins is a file input stream, then ins has already been
 60     //connected to a file. 
 61     friend ostream& operator <<(ostream& outs, const Money& amount);
 62     //Overloads the << operator so it can be used to output values of type Money.
 63     //Precedes each output value of type Money with a dollar sign.
 64     //Precondition: If outs is a file output stream, 
 65     //then outs has already been connected to a file.
 66     Money percent(int percent_figure) const;
 67 private:
 68     long all_cents;
 69 };
 70 int digit_to_int(char c);
 71 //Used in the definition of the overloaded input operator >>.
 72 //Precondition: c is one of the digits '0' through '9'.
 73 //Returns the integer for the digit; for example, digit_to_int('3') returns 3.
 74 int main( )
 75 {
 76     Money amount;
 77     ifstream in_stream;
 78     ofstream out_stream;
 79     in_stream.open("infile.dat");
 80     if (in_stream.fail( ))
 81     {
 82         cout << "Input file opening failed.\n";
 83         exit(1);
 84     }
 85     out_stream.open("outfile.dat");
 86     if (out_stream.fail( ))
 87     {
 88         cout << "Output file opening failed.\n";
 89         exit(1);
 90     }
 91 in_stream >> amount;
 92     out_stream << amount
 93               << " copied from the file infile.dat.\n";
 94     cout << amount
 95          << " copied from the file infile.dat.\n";
 96     in_stream.close();
 97     out_stream.close( );
 98     return 0;
 99 }
100 Money::Money(long dollars, int cents)
101 {
102     if(dollars * cents < 0) //if one is negative and one is positive
103     cout <<"Illegal values for dollars and cents.\n";
104     exit(1);
105 }
106 Money::Money(long dollars) : all_cents(dollars * 100)
107 {
108     //Body interntionally blank
109 }
110 Money::Money() : all_cents(0)
111 {
112     //Body intentionally left blank.
113 }
114 
115 
116 
117 //Uses iostream, cctype, cstdlib:
118 istream& operator >>(istream& ins, Money& amount)
119 {
120 
121     char one_char, decimal_point,
122          digit1, digit2; //digits for the amount of cents
123     long dollars;
124     int cents;
125     bool negative;//set to true if input is negative.
126     ins >> one_char;
127     if (one_char == '-')
128     {
129         negative = true;
130         ins >> one_char; //read '$'
131     }
132     else
133         negative = false;
134     //if input is legal, then one_char == '$'
135     ins >> dollars >> decimal_point >> digit1 >> digit2;
136     if ( one_char != '$' || decimal_point != '.'
137          || !isdigit(digit1) || !isdigit(digit2) )
138     {
139         cout << "Error illegal form for money input\n";
140         exit(1);
141     }
142 
143     cents = digit_to_int(digit1)*10 + digit_to_int(digit2);
144 
145     amount.all_cents = dollars*100 + cents;
146 
147     if (negative)
148         amount.all_cents = -amount.all_cents;
149 
150 return ins;
151 }
152 
153 int digit_to_int(char c)
154 {
155     return ( static_cast<int>(c) - static_cast<int>('0') );
156 }
157 
158 //Uses cstdlib and iostream:
159 ostream& operator <<(ostream& outs, const Money& amount)
160 {
161     long positive_cents, dollars, cents;
162     positive_cents = labs(amount.all_cents);
163     dollars = positive_cents/100;
164     cents = positive_cents%100;
165     if (amount.all_cents < 0)
166         outs << "-$" << dollars << '.';
167     else
168         outs << "$" << dollars << '.';
169     if (cents < 10)
170         outs << '0';
171     outs << cents;
172     return outs;
173 }
174 bool operator >(const Money& amount1, const Money& amount2)
175 {
176     return (amount1.all_cents >  amount2.all_cents);
177 }
178 
179 bool operator <(const Money& amount1, const Money& amount2)
180 {
181     return (amount1.all_cents < amount2.all_cents);
182 }
183 bool operator >=(const Money& amount1, const Money& amount2)
184 {
185     return (amount1.all_cents >= amount2.all_cents);
186 }
187 bool operator <=(const Money& amount1, const Money& amount2)
188 {
189     return (amount1.all_cents <= amount2.all_cents);
190 }
191 Money Money :: percent(int percent_figure) const
192     return ((percent_figure/100) * all_cents);



chp11.cpp: In member function ‘Money Money::percent(int) const’:
chp11.cpp:192:12: error: expected identifier before ‘(’ token
chp11.cpp:192:5: error: named return values are no longer supported
chp11.cpp:192:46: error: expected ‘{’ at end of input

Is This A Good Question/Topic? 0
  • +

Replies To: Confused on the scope resolution operator.

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3450
  • View blog
  • Posts: 10,649
  • Joined: 05-May 12

Re: Confused on the scope resolution operator.

Posted 11 March 2013 - 10:47 AM

It's not the scope resolution operator that is giving you problems. It's your lack of curly braces to surround the body of your method.
Was This Post Helpful? 1
  • +
  • -

#3 Speakeasys  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 19-September 12

Re: Confused on the scope resolution operator.

Posted 13 March 2013 - 07:28 PM

 32 #include <iostream>
 33 #include <fstream>
 34 #include <cstdlib>
 35 #include <cctype>
 36 using namespace std;
 37 //Class for amounts of money in U.S. currency. 
 38 class Money
 39 {
 40 public:
 41     friend Money operator +(const Money& amount1, const Money& amount2);
 42     friend Money operator -(const Money& amount1, const Money& amount2);
 43     friend Money operator -(const Money& amount);
 44     friend bool operator ==(const Money& amount1, const Money& amoun2);
 45     friend bool operator <(const Money& amount1, const Money& amoun2);
 46     friend bool operator >(const Money& amount1, const Money& amoun2);
 47     friend bool operator <=(const Money& amount1, const Money& amoun2);
 48     friend bool operator >=(const Money& amount1, const Money& amoun2);
 49     //Precondition: amount1 and amount2 have been given values.
 50     //Returns true if amount1 is less than amount2;
 51     //otherwise, returns false.
 52 Money(long dollars, int cents);
 53     Money(long dollars);
 54     Money( );
 55     double get_value( ) const;
 56     friend istream& operator >>(istream& ins, Money& amount);
 57     //Overloads the >> operator so it can be used to input values of type Money.
 58     //Notation for inputting negative amounts is as in -$100.00.
 59     //Precondition: If ins is a file input stream, then ins has already been
 60     //connected to a file. 
 61     friend ostream& operator <<(ostream& outs, const Money& amount);
 62     //Overloads the << operator so it can be used to output values of type Money.
 63     //Precedes each output value of type Money with a dollar sign.
 64     //Precondition: If outs is a file output stream, 
 65     //then outs has already been connected to a file.
 66     Money percent(int percent_figure) const;
 67 private:
 68     long all_cents;
 69 };
 70 int digit_to_int(char c);
 71 //Used in the definition of the overloaded input operator >>.
 72 //Precondition: c is one of the digits '0' through '9'.
 73 //Returns the integer for the digit; for example, digit_to_int('3') returns 3.
 74 int main( )
 75 {
 76     Money amount;
 77     ifstream in_stream;
 78     ofstream out_stream;
 79     in_stream.open("infile.dat");
 80     if (in_stream.fail( ))
 81     {
 82         cout << "Input file opening failed.\n";
 83         exit(1);
 84     }
 85     out_stream.open("outfile.dat");
 86     if (out_stream.fail( ))
 87     {
 32 #include <iostream>
 33 #include <fstream>
 34 #include <cstdlib>
 35 #include <cctype>
 36 using namespace std;
 37 //Class for amounts of money in U.S. currency. 
 38 class Money
 39 {
 40 public:
 41     friend Money operator +(const Money& amount1, const Money& amount2);
 42     friend Money operator -(const Money& amount1, const Money& amount2);
 43     friend Money operator -(const Money& amount);
 44     friend bool operator ==(const Money& amount1, const Money& amoun2);
 45     friend bool operator <(const Money& amount1, const Money& amoun2);
 46     friend bool operator >(const Money& amount1, const Money& amoun2);
 47     friend bool operator <=(const Money& amount1, const Money& amoun2);
 48     friend bool operator >=(const Money& amount1, const Money& amoun2);
 49     //Precondition: amount1 and amount2 have been given values.
 50     //Returns true if amount1 is less than amount2;
 51     //otherwise, returns false.
 52 Money(long dollars, int cents);
 53     Money(long dollars);
 54     Money( );
 55     double get_value( ) const;
 56     friend istream& operator >>(istream& ins, Money& amount);
 57     //Overloads the >> operator so it can be used to input values of type Money.
 58     //Notation for inputting negative amounts is as in -$100.00.
 59     //Precondition: If ins is a file input stream, then ins has already been
 60     //connected to a file. 
 61     friend ostream& operator <<(ostream& outs, const Money& amount);
 62     //Overloads the << operator so it can be used to output values of type Money.
 63     //Precedes each output value of type Money with a dollar sign.
 64     //Precondition: If outs is a file output stream, 
 65     //then outs has already been connected to a file.
 66     Money percent(int percent_figure) const;
 67 private:
 68     long all_cents;
 69 };
 70 int digit_to_int(char c);
 71 //Used in the definition of the overloaded input operator >>.
 72 //Precondition: c is one of the digits '0' through '9'.
 73 //Returns the integer for the digit; for example, digit_to_int('3') returns 3.
 74 int main( )
 75 {
 76     Money amount;
 77     ifstream in_stream;
 78     ofstream out_stream;
 79     in_stream.open("infile.dat");
 80     if (in_stream.fail( ))
 81     {
 82         cout << "Input file opening failed.\n";
 83         exit(1);
 84     }
 85     out_stream.open("outfile.dat");
 86     if (out_stream.fail( ))
 87     {
 88         cout << "Output file opening failed.\n";
 89         exit(1);
 90     }
 91 in_stream >> amount;
 92     out_stream << amount
 93               << " copied from the file infile.dat.\n";
 94     cout << amount
 95          << " copied from the file infile.dat.\n";
 96     in_stream.close();
 97     out_stream.close( );
 98     return 0;
 99 }
100 Money::Money(long dollars, int cents)
101 {
102     if(dollars * cents < 0) //if one is negative and one is positive
103     cout <<"Illegal values for dollars and cents.\n";
104     exit(1);
105 }
106 Money::Money(long dollars) : all_cents(dollars * 100)
107 {
108     //Body interntionally blank
109 }
110 Money::Money() : all_cents(0)
111 {
112     //Body intentionally left blank.
113 }
114 
115 
116 
117 //Uses iostream, cctype, cstdlib:
118 istream& operator >>(istream& ins, Money& amount)
119 {
120 
121     char one_char, decimal_point,
122          digit1, digit2; //digits for the amount of cents
123     long dollars;
124     int cents;
125     bool negative;//set to true if input is negative.
126     ins >> one_char;
127     if (one_char == '-')
128     {
129         negative = true;
130         ins >> one_char; //read '$'
131     }
132     else
133         negative = false;
134     //if input is legal, then one_char == '$'
135     ins >> dollars >> decimal_point >> digit1 >> digit2;
136     if ( one_char != '$' || decimal_point != '.'
137          || !isdigit(digit1) || !isdigit(digit2) )
138     {
139         cout << "Error illegal form for money input\n";
140         exit(1);
141     }
142 
143     cents = digit_to_int(digit1)*10 + digit_to_int(digit2);
139         cout << "Error illegal form for money input\n";
140         exit(1);
141     }
142 
143     cents = digit_to_int(digit1)*10 + digit_to_int(digit2);
144 
145     amount.all_cents = dollars*100 + cents;
146 
147     if (negative)
148         amount.all_cents = -amount.all_cents;
149 
150 return ins;
151 }
152 
153 int digit_to_int(char c)
154 {
155     return ( static_cast<int>(c) - static_cast<int>('0') );
156 }
157 
158 //Uses cstdlib and iostream:
159 ostream& operator <<(ostream& outs, const Money& amount)
160 {
161     long positive_cents, dollars, cents;
162     positive_cents = labs(amount.all_cents);
163     dollars = positive_cents/100;
164     cents = positive_cents%100;
165     if (amount.all_cents < 0)
166         outs << "-$" << dollars << '.';
167     else
168         outs << "$" << dollars << '.';
169     if (cents < 10)
170         outs << '0';
171     outs << cents;
172     return outs;
173 }
174 bool operator >(const Money& amount1, const Money& amount2)
175 {
176     return (amount1.all_cents >  amount2.all_cents);
177 }
178 
179 bool operator <(const Money& amount1, const Money& amount2)
180 {
181     return (amount1.all_cents < amount2.all_cents);
182 }
183 bool operator >=(const Money& amount1, const Money& amount2)
184 {
185     return (amount1.all_cents >= amount2.all_cents);
186 }
187 bool operator <=(const Money& amount1, const Money& amount2)
188 {
189     return (amount1.all_cents <= amount2.all_cents);
190 }
191 Money Money :: percent(int percent_figure) const
192 {
193     return (percent_figure = percent_figure/100);
194 }




Ok so i fixed them. And created the Percent function.
I am suppose to:
1. Modify the definition of the class Money shown in Display 11.8 so that all of the following
3 are added:
4 a. The operators <,<=, >, and >= have each been overloaded to apply to the type Money.
5 (Hint see self-test exercise 13.)
6 b. The following member function has been added to the class definition. (We show the function
7 declaration as it should apear in the class definition. The definition of the function itself will
8 include the qualifier Money::.)
9
10 Money percent(intpercent_figure) const;
11 //Returns a percentage of the money amount in the
12 //calling object. For example, if percent_figure is 10,
13 //then the value if returned is 10% of the amount of
14 //money represented by the calling object.

I just am not sure if my percent function is operating properly or not. I just learned classes and now i'm overloading
operators. It's a bit overwhelming but I"m trying to grasp the concepts. Any pointers is very appreciated. No pun intended. :(
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1