언어/C++
[ C++ ] 헹맨 게임
power-girl0-0
2021. 4. 13. 15:45
728x90
조건 :
- 배열에 입력되어 있는 문자를 랜덤으로 하나 뽑아내서 맞추는 게임이다.
- 문자 하나씩 입력받아와서, 전체를 맞추면 성공이다.
- 소문자, 대문자를 구분해야 한다.
- find를 이용해서 문자를 찾아야 한다.
소스코드 :
#include <string> #include <iostream> #include <ctime> //<time.h> #include <cstdlib> //<stdlib.h> using namespace std; int main(){ srand(time(NULL)); string list[]={"apple","C++ ++","banana","orange","grape","lemon"}; int r = rand()%6; string str=list[r]; string sol(str.length(),'_'); //str길이만큼 _문자로 뽑아달라! cout<<list[r]<<endl; char ch; cout<<"문자 알아맞추기"; cout<<endl<<sol<<endl; while(1){ cout<<endl<<"글자 : "; cin>>ch; if(str.find(ch)==-1){ continue; }else{ for(int i=0; i<sol.length(); i++){ if(str.find(ch,i)<=i){ sol[i] = ch; } } cout<<sol<<endl; } if(str == sol){ cout<<"성공"<<endl; break; } } return 0; }
결과 :


728x90