char - 与“cout”一起使用的自由字符指针函数
问题描述
在接下来的代码中,我构建了“char *card_name(int x)”函数,但是当我在主程序中使用它时,我没有将它分配给指针,而是直接在“cout”语句中使用它。我应该在主程序中使用它后释放这个函数吗?怎么做?
// turning cards valuse into strings function
char *card_name(int x)
{
char *c=(char *) malloc((strlen("queen")+1)*sizeof(char));
switch (x)
{
case 11:
strcpy(c,"Jack");
break;
case 12:
strcpy(c,"Queen");
break;
case 13:
strcpy(c,"King");
break;
case 14:
strcpy(c,"Ace");
break;
default:
sprintf(c,"%d",x);
}
return c;
}
在:
/* Acey Ducey game,
originally programmed in
BASIC */
#include <iostream>
#include <cstdlib>
#include <string>
#include <cctype>
#include <cstring>
#include <cstdio>
using namespace std;
char *card_name(int x); // turning cards valuse into strings function
void no_money_test(int& your_money); // money = 0 test
int main(void)
{
int your_money=100;
int card_A,
card_B, card_C;
char q;
int bet_much;
srand(time(NULL));
do // 1st do
{
cout << "You've "
<< your_money
<< "$" << endl;
// Generating the two cards:
do
{
card_A = (rand()%13)+2;
card_B = (rand()%13)+2;
} while (!(card_A < card_B) && !((card_B - card_A) >=2));
cout << "Here are your two cards: "
<< card_name(card_A) << " "
<< card_name(card_B) << endl;
// Betting query:
card_C = (rand()%13)+2;
do
{
cout << "Do you'll bet (y/n/e = (exit))? ";
cin >> q;
} while ((tolower(q)!='y') &&
(tolower(q)!='n') &&
(tolower(q)!='e'));
// agreeing betting
if (tolower(q)=='y')
{
do
{
cout << "With how much? ";
cin >> bet_much;
if (bet_much>your_money)
{
cout << "You don't have this much to bet with,"
<< endl << "you have " << your_money << "$"
<< endl;
}
} while (!(bet_much<=your_money));
if ((card_C>=card_A) && (card_C<=card_B)) // following agreeing betting 'if'
{
cout << "You are right! " << endl
<< "3rd card is: "
<< card_name(card_C) << endl;
your_money+=bet_much;
}
else
{
cout << "Oops! you are wrong!" << endl
<< "3rd card is: "
<< card_name(card_C) << endl;
your_money-=bet_much;
}
}
else // 'else if' of agreeing betting 'if'
if(tolower(q)=='e')
exit(0);
else // considered final 'else' of agreeing betting 'if'
cout << "CHICKEN!!!" << endl;
// Your money = 0 test
no_money_test(your_money);
} while (true); // 1st do
return 0;
}
// turning cards valuse into strings function
char *card_name(int x)
{
char *c=(char *) malloc((strlen("queen")+1)*sizeof(char));
switch (x)
{
case 11:
strcpy(c,"Jack");
break;
case 12:
strcpy(c,"Queen");
break;
case 13:
strcpy(c,"King");
break;
case 14:
strcpy(c,"Ace");
break;
default:
sprintf(c,"%d",x);
}
return c;
}
// money = 0 test
void no_money_test(int& your_money)
{
char q;
if (your_money == 0)
{
do
{
cout << "Oops! you have no money to bet with,"
<< endl << "do you want playing again (y/n)? ";
cin >> q;
} while ((tolower(q)!='y') &&
(tolower(q)!='n'));
if (tolower(q)=='y') // nasted in 'if money = 0'
{
your_money = 100;
}
else
exit(0);
} // if money = 0
}
解决方案
推荐阅读
- mysql - 我如何将重叠列作为嵌套数组?NodeJS/MySQL (mysql2)
- javascript - VueJS - 数据列表输入未清除值
- reactjs - 如何在反应中添加我们的外部js文件
- c# - 使用 itext7 在 net core 中读取 PDF 返回“\n\n\n\n\n....”
- android - 平板设备中数字的 EditText inputType,总是显示额外的键
- javascript - 如何使用管道有条件地显示 innerHTML 值
- c# - 将原始 RGB 字节数组转换为 JPG 编码字符串 base64
- c# - 尝试在我的列表中使用数组索引时出错
- python - 如何在 Django 中内连接 4 个表?
- bash - 使用 grep 过滤后如何忽略行首和行的一部分