the cin.fail() not work good
If you write in x and y chars that closes the program
code:
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include "header.cpp"
int main() {
Tester x(13,64);
x.printMap();
x.setMap(43,42);
x.printMap();
x.addMap(40,34);
x.printMap();
x.writeMap();
cout << "(" << x.getX() << "," x.getY() << ")" << endl;
cin.get();
cin.ignore();
return 0;
}
|
header.cpp
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
|
#include <iostream>
using namespace std;
class Tester {
private:
int x,y;
public:
Tester(int xx, int yy) { x = xx; y = yy; }
Tester() { x = 0; y = 0; }
void setMap(int xx, int yy) { x = xx; y = yy; }
void addMap(int xx, int yy) { x += xx; y += yy; }
void printMap() { cout << "map(" << x << "," << y << ")" << endl; }
int getX() { return x; }
int getY() { return y; }
void writeMap() {
cout << "write x and y" << endl; cin >> x;
if(cin.fail()) {
cerr << "is a not number";
x = 0;
cin.clear();
}
cin >> y;
if(cin.fail()) {
cout << "is a not number" << endl;
y = 0;
cin.clear();
}
}
};
|
what are i need to do?
Mate, could you please elaborate on your question. What exactly you wish to do with your program?
Topic archived. No new replies allowed.