By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

Error C2664, C2228

P: 9
I'm trying to learn how to set ranges by using classes and I get Error c2664 and C2228 when I build. I saw the question in my practice texrbook and I cant figure out what I'm missing.

It says "ui" should have a class on the left of "ui' but it already does.

Its declared right after the Range. Can you help
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <cctype>
  5. using namespace std;
  6.  
  7. class IntRange
  8. {
  9. private:
  10.     int input;
  11.     int lower;
  12.     int upper;
  13. public:
  14.     IntRange(int, int);
  15.     int    getInt();
  16. };
  17. IntRange::IntRange(int lower, int upper)
  18. {
  19.     lower = 50;
  20.     upper = 70;
  21. }
  22.  
  23. int IntRange::getInt()
  24. {
  25.     cin.get(input);
  26.     cin.ignore();
  27.  
  28.     while (input < lower || input > upper)
  29.     {    cout <<" The input number is out of range.\n";
  30.         cout <<" Please enter a value between " << lower;
  31.         cout <<" and " << upper << ".\n";
  32.         cin.get(input);
  33.         cin.ignore();
  34.     }
  35.     return input;
  36. }
  37. int main()
  38. {
  39.  
  40.     IntRange Range(55, 65);
  41.     int ui;
  42.  
  43.     cout << "Enter a number between 55 and 65.\n\n";
  44.     cout << "57 is an exit number, entering 57 would stop the program.\n\n";
  45.     ui = Range.getInt();
  46.     while (ui != 57)
  47.     {
  48.         cout <<"You entered " << ui << endl.
  49.         ui = Range.getInt();
  50.     }
  51.     return 0;
  52. }
Mar 18 '07 #1
Share this Question
Share on Google+
5 Replies


arne
Expert 100+
P: 315
I'm trying to learn how to set ranges by using classes and I get Error c2664 and C2228 when I build. I saw the question in my practice texrbook and I cant figure out what I'm missing.

It says "ui" should have a class on the left of "ui' but it already does.

Its declared right after the Range. Can you help

#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
using namespace std;

class IntRange
{
private:
int input;
int lower;
int upper;
public:
IntRange(int, int);
int getInt();
};
IntRange::IntRange(int lower, int upper)
{
lower = 50;
upper = 70;
}

int IntRange::getInt()
{
cin.get(input);
cin.ignore();

while (input < lower || input > upper)
{ cout <<" The input number is out of range.\n";
cout <<" Please enter a value between " << lower;
cout <<" and " << upper << ".\n";
cin.get(input);
cin.ignore();
}
return input;
}
int main()
{

IntRange Range(55, 65);
int ui;

cout << "Enter a number between 55 and 65.\n\n";
cout << "57 is an exit number, entering 57 would stop the program.\n\n";
ui = Range.getInt();
while (ui != 57)
{
cout <<"You entered " << ui << endl.
ui = Range.getInt();
}
return 0;
}
Your line
Expand|Select|Wrap|Line Numbers
  1.                 cout <<"You entered " << ui << endl.
  2.  
should not end with a '.', but with a ';'. Is that the line the compiler complains about?
Mar 18 '07 #2

P: 9
yeah, u r right but that's for the error c2228 [It should have been a semicolon]

For the error c2664 on line 26 and 33. If I took away the "input" in the cin.get(input), the error would go away but it doesnt yet do what I'd like it to do...
Mar 18 '07 #3

arne
Expert 100+
P: 315
yeah, u r right but that's for the error c2228 [It should have been a semicolon]

For the error c2664 on line 26 and 33. If I took away the "input" in the cin.get(input), the error would go away but it doesnt yet do what I'd like it to do...
What about
Expand|Select|Wrap|Line Numbers
  1. cin >> input;
  2.  
?
Mar 18 '07 #4

P: 9
What about
Expand|Select|Wrap|Line Numbers
  1. cin >> input;
  2.  
?
I tried that already so it builds but the problem is there is something wrong with the input, it doesn't redisplay the entered number. It just shows a 0 even if the number entered is 58.

Thats why I was thinking it was the way I was reading the input so I tried using cin.get
Mar 18 '07 #5

arne
Expert 100+
P: 315
I tried that already so it builds but the problem is there is something wrong with the input, it doesn't redisplay the entered number. It just shows a 0 even if the number entered is 58.

Thats why I was thinking it was the way I was reading the input so I tried using cin.get
You may change your constructor to
Expand|Select|Wrap|Line Numbers
  1. IntRange::IntRange( int l, int u )
  2. {
  3.     lower = l;
  4.     upper = u;
  5. }
  6.  
and add
Expand|Select|Wrap|Line Numbers
  1.     cout << input << endl;
  2.  
after the cin to echo your input.
Mar 18 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.