คำตอบที่ได้รับเลือกจากเจ้าของกระทู้
ความคิดเห็นที่ 2
- c++ string class มี operators เทียบกับ null-terminated c-string (e.g. "ROCK") ทั้งกรณี c-string อยู่ซ้ายและขวาอยู่แล้ว
ดังนั้นเราสั่งเทียบ string object กับ "ROCK" และ "1" ได้เลย ("ROCK" == str)
- c++ string class มี std::getline ใช้เก็บ string ทั้งบรรทัด
- ใช้ enum type เพื่อความอ่านง่ายและแสดงเจตจำนงค์ (express intention) ของเราเองให้ผู้อ่าน (...ซึ่งก็คือตัวเราเองในอนาคตนั่นแหละ)
- ใช้ฟีเจอร์แบบล่าสุดจาก compiler เวอร์ชันล่าสุด http://winlibs.com/ หรือ compiler-explorer.com [Spoil] คลิกเพื่อดูข้อความที่ซ่อนไว้
enum class rps {
rock, paper, scissor, na
};
int main()
{
...
string input;
std::getline(std::cin, input);
switch (make_rps(input) ) {
using enum rps; // rock == rps::rock (c++ 20)
case (rock) : ...; break;
case (paper) : ...; break;
case (scissor) : ...; break;
default: ...; // case of na
}
}
rps make_rps(string s) { // or 'proper' const ref parameter passing => (const string& s)
using enum rps;
if ( ("ROCK" == s) || ("1" == s) ) { return rock; }
else if ( ("PAPER" == s) || ("2" == s) ) { return paper; }
else if (...
else { return na; }
}
ดังนั้นเราสั่งเทียบ string object กับ "ROCK" และ "1" ได้เลย ("ROCK" == str)
- c++ string class มี std::getline ใช้เก็บ string ทั้งบรรทัด
- ใช้ enum type เพื่อความอ่านง่ายและแสดงเจตจำนงค์ (express intention) ของเราเองให้ผู้อ่าน (...ซึ่งก็คือตัวเราเองในอนาคตนั่นแหละ)
- ใช้ฟีเจอร์แบบล่าสุดจาก compiler เวอร์ชันล่าสุด http://winlibs.com/ หรือ compiler-explorer.com [Spoil] คลิกเพื่อดูข้อความที่ซ่อนไว้
enum class rps {
rock, paper, scissor, na
};
int main()
{
...
string input;
std::getline(std::cin, input);
switch (make_rps(input) ) {
using enum rps; // rock == rps::rock (c++ 20)
case (rock) : ...; break;
case (paper) : ...; break;
case (scissor) : ...; break;
default: ...; // case of na
}
}
rps make_rps(string s) { // or 'proper' const ref parameter passing => (const string& s)
using enum rps;
if ( ("ROCK" == s) || ("1" == s) ) { return rock; }
else if ( ("PAPER" == s) || ("2" == s) ) { return paper; }
else if (...
else { return na; }
}
แสดงความคิดเห็น

พยามทำ โปรเเกรม เป่า ยิ้ง ชิบ ที่สามารถ input ทั้ง int เเละ string ได้ เเต่ไม่สำเร็จซักทีครับ ช่วยด้วย C++
#include<iostream>
#include<string>
#include<cstdlib>
#include<ctime>
using namespace std;
int random;
bool status;
bool check_int (int player_int){
bool status=0;
if(player_int==int (player_int)){
status=1;
}
return status;
}
bool check_string (string player_ch){
bool status=1;
if(player_ch==string (player_ch)){
status=0;
}
return status;
}
void game_string(){
string player_ch;
string system_rock="ROCK";
string system_paper="PAPER";
string system_scissor="SCISSOR";
int player_int;
player_int=player_int<0;
random=1+(rand()%3);
cout<<"ROCK PAPER SCISSOR GAME!!!\n";
cout<<"1:ROCK\n2
cout<<"Choose number to select your weapon or type it down:";
cin>>player_ch,player_int;
if(check_string(player_ch)==0){//1 STRING
if(player_ch==system_rock){
cout<<"Your weapon is ROCK\n";
}
else if(player_ch==system_paper){
cout<<"Your weapon is PAPER\n";
}
else if(player_ch==system_scissor){
cout<<"Your weapon is SCISSOR\n";
}
}//if check input data
else
{
switch(player_int){
case 1:
cout<<"Your weapon is ROCK\n";
break;
case 2:
cout<<"Your weapon is PAPER\n";
break;
case 3:
cout<<"Your weapon is SCISSOR\n";
break;
case 4:
cout<<"Program close";
system;exit(0);
}
}
game_string();
}
int main(){
srand(time(0));
game_string();
return 0;
}