2017-04-05 111 views
0

我的程序試圖模擬自動售貨機。除硬幣退還或退還零件以外的所有零件都可以使用。我試圖把插入到機器中的美分量輸入到主體外的另一個代碼中,然後返回一個字符串。這在C中是可能的,因爲我不斷得到一個初始化使得整型指針沒有轉換錯誤。如果這是不可能的,關於如何做到這一點的任何想法?如何返回字符串變量c

謝謝!

#include <stdio.h> 
#include <stdlib.h> 
#include <stdbool.h> 
#define NI 5 
#define DI 10 
#define QU 25 



bool isValid(int num) { 
    return (num >= 10 && num <= 100); 
} 

bool isMultiple(int num) { 
     return (num % 5 == 0); 
} 


char *coinReturn(int insert) { 

           int dimes = insert/DI; 
           int remainder = insert % DI; 
           int nickels = remainder/NI; 
           int remainder2= nickels % NI; 
           char *m = ("Change returned. %d nickels and %d dimes\n",nickels, dimes); 
           return m; 
} 


int 
main (int argc, char *argv[]) 
{ for (int i = 1; i != argc; ++i) { 
     int price = atoi(argv[i]); 
     if (!isValid(price)) { 
      printf("Price muse be from 10 cents to 100 cents.\n"); 
      break; 
     } else if (!isMultiple(price)) { 
       printf("Price must be a multiple of 5.\n"); 
       break; 
     } else { 
       printf(" Welcome to my Vending Machine!\n"); 
       printf("Pop is %d cents. Please enter nickels, dimes, or quarters\n", price); 

         int cents = 0; 
         char coin = '\0'; 

         while (cents <= price && coin != 'E') { 
         printf(" PLease enter a coin [NDQR]\n"); 
         scanf (" %c", &coin); 

         if (coin == 'N'|| coin == 'n') { 
           cents = cents + NI; 
           printf(" You have inserted 5 cents\n"); 
         } 
         else if (coin == 'd' || coin == 'D') { 
           cents = cents + DI; 
           printf("You have inserted 10 cents\n"); 
         } 
         else if (coin == 'Q' || coin == 'q') { 
           cents = cents + QU; 
           printf("You have entered 25 cents\n"); 
         } 
         else if (coin == 'R' || coin == 'r'){ 
           printf("Change requested\n"); 
           char *rtn2= coinReturn(cents); 
           printf("%s\n",rtn2); 
         } else { 
           printf("Unknown coin. Rejected.\n"); 
         } 
         int balance = price - cents; 
         printf("You have entered a total of %d cents\n", cents); 

         if (balance > 0) { 
         printf("You must enter %d more cents\n", balance); 
        } else { 

           char *rtn = coinReturn(cents); 
           printf("%s\n",rtn); 
         cents = 0; 

        } 

        } 


       printf("DONE!\n"); 
       return 0; 
} 
} 

回答

0

這行不通:

字符* M =( 「更改返回%d鎳和%d硬幣\ n」。,鎳,角錢);

也許擺脫char *並只是調用printf?

+0

但我需要以某種方式返回聲明。我可以用printf來做到這一點? – JVAN

+0

不是。你可以'malloc'緩衝區,使用['snprintf'](http://www.cplusplus.com/reference/cstdio/snprintf/)格式化它,然後返回它。 –

+0

但你所做的所有回報是printf它。爲什麼不直接在函數中執行printf? –

0

它實際上可以做你在說什麼。以下是您可能會喜歡的兩種解決方案:

解決方案1:使用printf而不是返回。

首先,你沒有真正寫

char *m = ("Change returned. %d nickels and %d dimes\n", nickels, dimes);

我們需要將這些值傳遞給函數來進行實際的格式格式化您的字符串!

其次,我不明白爲什麼我們需要從coinReturn返回......當我們可以簡單地寫

printf("Change returned. %d nickels and %d dimes\n", nickels, dimes);

,我們就大功告成了!

但也許這不是我們想要的。也許我們要真正返回一個字符串指針這個功能,我們可以做到這一點太:)

解決方案2:使用的snprintf並返回一個指向靜態緩衝區

char *coinReturn(int insert) { 
    // set aside a buffer for our string. 
    static char buf[512]; 
    int dimes = insert/DI; 
    int remainder = insert % DI; 
    int nickels = remainder/NI; 
    int remainder2= nickels % NI; 
    // output our string into the buffer. 
    snprintf(buf, sizeof(buf), 
     "Change returned. %d nickels and %d dimes\n", nickels, dimes); 
    return buf; 
} 

首先我們設置留出一定的空間存儲我們的格式化字符串,

static char buf[512];

注意它的neccesary聲明bufstatic所以它在我們的coinReturn函數的範圍內倖存下來。

512將是我們的最大字符串長度,在這裏應該足夠了。

讓我們來看看,明年部分:

snprintf(buf, sizeof(buf), 
    "Change returned. %d nickels and %d dimes\n", nickels, dimes); 

(參見:https://linux.die.net/man/3/printf

第一個參數是我們的字符串將被寫入,buf位置。

第二個參數是字節snprintf的最大數目將寫(包括空終止)。

一個參數是一樣printf,你已經很熟悉。

return buf; 

終於可以回到buf知道結果會是一個指向我們的格式化字符串。


出這兩種解決方案,第一個肯定是更有意義 - 有很多我看到#2(例如:它不是線程安全的)問題,但它得到是工作如果做這就是你想要的。