2016-10-26 4 views
-1

Guten Morgen, Leute! Ich bin gerade ein neu gestarteter Programmier-Lerner. Hier unten ist mein Code für einen Mini-App-Store. Es gibt jedoch ein Problem, aber ich konnte das Problem nicht finden. Das Problem passierte, als ich versuchte, eine App für $ 89,99 zu kaufen und ich entschied mich, $ 10 9 Mal einzulösen, also würde ich genug Geld haben, um die App zu kaufen (ich wählte nicht die $ 100 Option, weil es gut funktionieren würde). Der Restbetrag wurde jedoch $ -79,99 statt $ 0,01. Wie gesagt, wenn ich 100 $ einzahlen würde, wäre das verbleibende Guthaben 10,01 $, was normal ist. Ich verstehe nicht, wo ich falsch gemacht habe. Hier ist mein Code!Anzahlung gibt negative Zahl?

#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h> 
#include <ctype.h> 

int Compare(double deposit, double choiceCost); 
void DisplayApps(char *selectionPtr); 
void SetCost(char selection, double *costPtr); 
void PaymentOptions(double *depositPtr,double cost); 
void DoItAgain(char *quitPtr); 
//void Pay(double *depositPtr, double choiceCost); 
void GetChange(double *depositPtr, double choiceCost); 
void DoItAgain(char *quitPtr); 

int main() 
{ 
    char selectionPtr; 
    char selection; 
    char quitPtr; 
    double costPtr; 
    double deposit = 0.0; 
    double choiceCost; 
    double depositPtr = 0.0; 
    double cost = 0.0; 

    printf("Welcome to the App Store\n"); 
    printf("***************************\n\n"); 
    printf("Your deposit is: %.2f\n", deposit); 
    printf("\n"); 

    while (1) 
    { 
     do { 
      DisplayApps(&selectionPtr); 

      selection = selectionPtr; 
      SetCost(selection, &costPtr); 

      choiceCost = costPtr; 
      Compare(deposit, choiceCost); 

      while (Compare(deposit, choiceCost) == 0) 
      { 
       printf("Your balance isn't enough.\n"); 
       printf("In order to purchase this item, you have to redeem more money.\n"); 
       PaymentOptions(&depositPtr, cost); 
       deposit += depositPtr; 
       printf("You have redeemed $%.2f\n", depositPtr); 
       printf("Your balance now is: $%.2f\n", deposit); 
       printf("\n"); 
      } 
      deposit = depositPtr; 

      GetChange(&depositPtr, choiceCost); 

      DoItAgain(&quitPtr); 
     } while (quitPtr == 'Y' || quitPtr == 'y'); 
     return 1; 
    } 

    return 0; 
} 

void DisplayApps(char *selectionPtr) 
{ 
    printf("-------------------------\n"); 
    printf("HERE ARE THE SLECTIONS\n"); 
    printf("C -- Clown Punching    $299.99\n"); 
    printf("V -- Virtual Snow Globe   $349.99\n"); 
    printf("R -- Remote PC     $999.99\n"); 
    printf("G -- Grocery List Helper  $2.99\n"); 
    printf("M -- Mobile Cam Viewer   $89.99\n"); 
    printf("\n"); 

    printf("Please enter a selection: "); 
    scanf(" %c", &*selectionPtr); 
    printf("\n"); 

} 

void SetCost(char selection, double *costPtr) 
{ 
    if (selection == 'C' || selection == 'c') 
    { 
     *costPtr = 299.99; 
    } 
    else if (selection == 'V' || selection == 'v') 
    { 
     *costPtr = 349.99; 
    } 
    else if (selection == 'R' || selection == 'r') 
    { 
     *costPtr = 999.99; 
    } 
    else if (selection == 'G' || selection == 'g') 
    { 
     *costPtr = 2.99; 
    } 
    else if (selection == 'M' || selection == 'm') 
    { 
     *costPtr = 89.99; 
    } 
} 

int Compare(double deposit, double choiceCost) 
{ 
    if (deposit < choiceCost) 
    { 
     return 0; 
    } 
    else 
    { 
     return 1; 
    } 
} 

void PaymentOptions(double *depositPtr, double cost) 
{ 
    printf("You have (4) options to choose:\n"); 
    printf("1 - $1000.00\n"); 
    printf("2 - $500.00\n"); 
    printf("3 - $100.00\n"); 
    printf("4 - $10.00\n"); 
    printf("How much do you want to redeem?"); 
    printf(">>>>> "); 
    scanf("%lf", &cost); 
    printf("\n"); 
    printf("-------------------------------------\n"); 
    if (cost == 1) 
    { 
     *depositPtr = 1000.00; 
    } 
    else if (cost == 2) 
    { 
     *depositPtr = 500.00; 
    } 
    else if (cost == 3) 
    { 
     *depositPtr = 100.00; 
    } 
    else if (cost == 4) 
    { 
     *depositPtr = 10.00; 
    } 

} 

void GetChange(double *depositPtr, double choiceCost) 
{ 
    *depositPtr = *depositPtr - choiceCost; 
    printf("You have purchased this item successfully.\n"); 
    printf("You still have $%.2f remained in you balance.\n", *depositPtr); 
} 

void DoItAgain(char *quitPtr) 
{ 
    printf("Do you want to continue purchase another item? [Y/N]\n"); 
    scanf(" %c", &*quitPtr); 
} 
+3

Bitte beachten Sie [hier] passieren (http://stackoverflow.com/help/ mcve) zum Erstellen eines _minimalen, vollständigen und verifizierbaren Beispiels_ (mit Betonung auf * minimal *). Kurz und gut: Geben Sie nicht nur den gesamten Code hier ein, sondern beschränken Sie ihn auf einen kleinen Codeabschnitt, der das Problem reproduziert und uns das gibt. –

+1

Nach der while (Compare()) machen Sie Einzahlung = depositPtr, das löscht alle Einzahlungen, die Sie gemacht haben und macht es nur die letzte wert –

+1

Hinweis: ** Niemals ** Gleitkommatypen verwenden, wenn Sie ** genau benötigen ** Werte. Sie können nicht fast alle Bruchzahlen genau darstellen. Verwenden Sie skalierte Ganzzahlen für Währungen. – Olaf

Antwort

2

In diesem Code: GetChange(&depositPtr, choiceCost); Sie shold deposit (gesamt Kaution) und nicht &depositPtr (letzte Einzahlung, 10)

+0

Vielen Dank! Ich habe das Problem gefunden und behoben, wie Sie es vorgeschlagen haben :) – user6866732