以下のソースを実行することはできるのですが、新しくつくる 1キー やめる 2キー この猫で戦う 3キー で数字ではない文字を打つとエラーになって高速文字打ち出しのループみたいなかんじになってしまいます・・・INT型でやっているからだとはおもうのですが、文字、数字対応の型はありませんか?詳しくえて下さい(ソースには無駄が多いとおもいますがそこは勘弁してください・・・)長文ごめんなさい(戦闘力は見かけだけで勝敗とはかかわっていません)
#include<iostream>
#include<string>
#include <cstdlib>
#include <ctime>
using namespace std;
class neko{
string name;
public:
neko(string);
void naku() const;
};
neko::neko(string s) : name(s){}
void neko::naku() const{
cout<<"あなたがご主人様なのにゃ?"<<endl;
cout<<" "<<endl;
cout<<"ぼくは"<< name <<"なのにゃ! よろしくにゃ!"<<endl;
cout<<" "<<endl;
int x = rand()%10 + 1;
cout<<"戦闘力"<<x<<"の猫が作成されました"<<endl;
cout<<" "<<endl;
}
int main(){
while(1){
string s;
int ans;
int ans3;
string ans2;
srand( (unsigned)time( NULL ) );
cout<<"猫を作成します 名前を付けてあげて下さい"<<endl;
cout<<" "<<endl;
cin>>s;
cout<<" "<<endl;
cout<<"猫がやってきました"<<endl;
cout<<" "<<endl;
neko first(s);
first.naku();
cout<<"この猫で戦いますか? または この猫を消去して新しい猫をつくりますか?"<<endl;
cout<<" "<<endl;
while(1){
cout<<endl;
cout<<" "<<endl;
cout<<"新しくつくる 1キー やめる 2キー この猫で戦う 3キー"<<endl;
cout<<" "<<endl;
cin>>ans;
if(ans == 1){
break;
}
else if(ans == 2){
break;
}
else if(ans ==3){
cout<<" "<<endl;
cout<<"相手の猫を作成します 名前を付けてあげて下さい"<<endl;
cout<<" "<<endl;
cin>>ans2;
cout<<" "<<endl;
cout<<s<<"と"<<ans2<<"のバトル!"<<endl;
if (rand() % 2 + 1 == 1){
cout<<" "<<endl;
cout<<s<<"が先制攻撃!"<<endl;
cout<<" "<<endl;
}
else if (rand() % 2 + 1 == 2){
cout<<" "<<endl;
cout<<ans2<<"が先制攻撃!"<<endl;
cout<<" "<<endl;
}
if (rand() % 2 + 1 == 1){
cout<<" "<<endl;
cout<<s<<"の勝利!"<<endl;
}
else{cout<<" "<<endl;
cout<<ans2<<"の勝利!"<<endl;}
}
else{
cout<<" "<<endl;
cout<<"不適切な数字です"<<endl;
cout<<" "<<endl;
cout<<"まだ猫をつくりますか?"<<endl;
}
}
if(ans == 2){
break;
}
}
cout<<" "<<endl;
cout<<" "<<endl;
cout<<"終了します"<<endl;
system("PAUSE");
}