首页 > 解决方案 > 与“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

}

标签: charfree

解决方案


推荐阅读