Forum: PC-Programmierung Benötige euere Hilfe bei meinem C Programm (Funktionen)


von Christoph (Gast)


Angehängte Dateien:

Lesenswert?

Hallo zusammen,

ich lernen gerade C und hänge seit Tagen an einer Aufgabe fest.
Es geht hauptsächlich um Funktionen.
Leider werden die Funktionen nicht ausgeführt, ich habe schon alles 
mögliche ausprobiert, komme mit meinem Anfängerwissen aber leider nciht 
weiter.
Ich freue mich sehr über eure Hilfe!

Die Aufgabe ist im Anhang und hier ist mein Code :
1
#include <stdio.h>
2
3
int counter;
4
    int selection;
5
  int quantity;
6
    double money;
7
  double total_cost=0;
8
  double price;
9
  double sum;
10
  double change;
11
12
    int menu();
13
  int FoodSelected (int selection);
14
  double ItemCost(int selection);
15
  double totalprice(double price, int quantity);
16
    double EnteredMoney(double money, double sum);
17
18
int main()
19
{
20
  int counter;
21
    int selection;
22
  int quantity;
23
    double money;
24
  double total_cost=0;
25
  double price;
26
  double sum;
27
  double change;
28
29
    // write the code to print the first 4 lines of the output image in the output file //
30
  FILE *fp;
31
    fp  = fopen ("invoice.txt", "w");
32
    fprintf(fp, "The bill info of the selected item is:\n");
33
    fprintf(fp, "--------------------------------------------------------------------------------:\n");
34
    fprintf(fp, "The bill info of the selected item is:\n");
35
    fprintf(fp, "Item \t Price \t Quantity \t Total \t Paid Money \t Remaining Money\n");
36
    fprintf(fp, "--------------------------------------------------------------------------------:\n");
37
38
39
  for (counter=1, counter > 0, ++counter)
40
  {
41
42
    int menu ();
43
44
    if (selecion != 5)
45
    {
46
      int FoodSelected (int selection);
47
      double ItemCost(int selection);
48
      double totalprice(double price,int quantity);
49
      if (selection ==1)
50
      {
51
        fprintf(fp, "Apple\t  %f \t %d  \t %f \n", &price, &quantity, &total_cost);
52
      }
53
      else if (selection ==2)
54
      {
55
        fprintf(fp, "Banana\t  %f \t %d  \t %f \n", &price, &quantity, &total_cost);
56
      }
57
      else if (selection ==3)
58
      {
59
        fprintf(fp, "Orange\t  %f \t %d  \t %f \n", &price, &quantity, &total_cost);
60
      }
61
      else (selection ==4)
62
      {
63
        fprintf(fp, "Salad\t  %f \t %d  \t %f \n", &price, &quantity, &total_cost);
64
      }
65
      sum = sum + total_cost;
66
      printf("------------------------------------------");
67
    }
68
69
    else (selecion == 5)
70
    {
71
      printf("Please enter the Money");
72
      printf("\n");
73
        scanf("%f",&money);
74
      printf("\n");
75
76
      double EnteredMoney(double money, double sum);
77
      fprintf(fp, "--------------------------------------------------------------------------------:\n");
78
      fprintf(fp, "\t\t\t\t %d $\t %f $ \n", &money, &change);
79
      break;
80
    }
81
82
  }
83
84
  printf("Thank you for your payment, please check the reset file which has your payment details\n");
85
  fclose (fp);
86
87
  return 0;
88
89
90
  menu();
91
  int FoodSelected(int selection);
92
  int ItemCost(int selection);
93
  double totalprice(double price, int quantity);
94
    double EnteredMoney(double money, double sum);
95
96
  return 0;
97
}
98
99
/* Menu Function */
100
101
int menu ()
102
  {
103
    /*List the menu on the screen*/
104
    printf("MENU: ");
105
    printf("\n");
106
    printf("1. Apple                   $1.00\n");
107
    printf("2. Banana                  $1.50\n");
108
    printf("3. Orange                  $2.00\n");
109
    printf("4. Salad                   $3.00\n");
110
    printf("5. Quit");
111
    printf("\n\n");
112
113
    /* Ask the user to choose an option from the menu*/
114
    printf("Please enter your selection: ");
115
    scanf("%d",&selection);
116
    printf("\n");
117
118
    return selection;
119
  }
120
121
/* Food Selection Function */
122
123
/* This function displays the selected item by the user and ask him how many of that item he needs*/
124
int FoodSelected (int selection)
125
  {
126
  if (selection==1)
127
    {
128
      printf("You have selected Apple.                   $1.00 ");
129
      printf("\n");
130
      printf("How many of the item you need\n");
131
        scanf("%d",&quantity);
132
    }
133
  else if (selection==2)
134
    {
135
      printf("You have selected Banana.                   $1.50 ");
136
      printf("\n");
137
      printf("How many of the item you need\n");
138
        scanf("%d",&quantity);
139
    }
140
  else if (selection==3)
141
    {
142
      printf("You have selected Orange.                   $2.00 ");
143
      printf("\n");
144
      printf("How many of the item you need\n");
145
        scanf("%d",&quantity);
146
    }
147
  else if (selection==4)
148
    {
149
      printf("You have selected Salad.                   $3.00 ");
150
      printf("\n");
151
      printf("How many of the item you need\n");
152
        scanf("%d",&quantity);
153
    }
154
155
  else                                  /* If the user enter a value that is not between 1-5*/
156
    {
157
      printf("Invalid selection! Please try again.");
158
      scanf("%d",&selection);
159
      printf("\n");
160
      int FoodSelected (int selection);
161
    }
162
163
  return quantity;
164
  }
165
166
167
/* Selected Item Cost Function */
168
169
/* This funtion defines the value of the item selected by the user*/
170
171
double ItemCost(int selection)
172
  {
173
  if (selection==1)
174
    {
175
      price = 1.00;
176
    }
177
  else if (selection==2)
178
    {
179
      price = 1.50;
180
    }
181
  else if (selection==3)
182
    {
183
      price = 2.00;
184
    }
185
  else if (selection==4)
186
    {
187
      price = 3.00;
188
    }
189
  return price;
190
  }
191
192
193
194
195
/* Total Item Cost Function */
196
197
/* This finction calculates the total cost of the item. It is the price multiplies by the quantity*/
198
double totalprice(double price,int quantity)
199
  {
200
    total_cost = price*quantity;
201
    return total_cost;
202
  }
203
204
205
206
/* Entered Money and return change Functions */
207
208
/* This function calculates the change and checks if the money value is valid*/
209
210
double EnteredMoney(double money, double sum)
211
    {
212
            double change;
213
      if (money >= sum)
214
      {
215
        change = money - sum;
216
      }
217
      else
218
      {
219
      printf("Error! Please enter the money again!!\n");
220
      scanf("%f",&money);
221
      printf("\n");
222
      double EnteredMoney(double money, double sum);
223
      }
224
225
      return change;
226
    }
227
228
229
/* Main Function */

: Bearbeitet durch Moderator
von Oliver S. (oliverso)


Lesenswert?

Code besser als Anhang, dann lässt sich das einfacher lesen.

Zum Problem:
1
return 0;

macht immer genau das, was du erwartest.

Oliver

von Fabian (Gast)


Lesenswert?

Was soll dieser Bereich deiner Meinung nach machen?
1
for (counter=1, counter > 0, ++counter)
2
  {
3
4
    int menu ();

von BobbyX (Gast)


Lesenswert?

for (counter=1, counter > 0, ++counter)

Das ist bestimmt nicht im Sinne des Erfinders. Hier sollten zu 
mindestens Semikolone und nicht Kommas eingesetzt werden. Auch wird die 
counter Variable nicht initialisert. Ergo: Der Code in der for Schleife 
wird nie ausgeführt...

von Christoph (Gast)


Lesenswert?

BobbyX schrieb:
> Der Code in der for Schleife
> wird nie ausgeführt...

Oh dank, das habe ich auch eben erst bemerkt und behoben.

von Christoph (Gast)


Lesenswert?

Fabian schrieb:
> Was soll dieser Bereich deiner Meinung nach machen?
> for (counter=1, counter > 0, ++counter)
>   {
>
>     int menu ();

Dieser Code soll die Eingaben zählen welche eingelesen werden,
da mehrere Dinge über das Menu ausgewählt werden können

von Yalu X. (yalu) (Moderator)


Lesenswert?

Ich habe mal die Code-Tags
1
[c]
2
...
3
[/c]

eingefügt, so dass auch die zu Fehlern führenden Zeilenumbrüche
verschwinden. Syntaktisch korrekt ist der Code damit aber noch lange
nicht. Es scheinen auch noch einige Tippfehler darin zu sein.

von Wilhelm M. (wimalopaan)


Lesenswert?

Christoph schrieb:
> Dieser Code soll die Eingaben zählen welche eingelesen werden,
> da mehrere Dinge über das Menu ausgewählt werden können

Leider ist das kein Funktionsaufruf, so wie es da steht.

von Wilhelm M. (wimalopaan)


Lesenswert?

Ich kann mir kaum vorstellen, dass Dein Tutor Euch mit dieser Aufgabe 
überfallen hat. Da gab es sicher Übungen davor zum Thema Funktionen. 
Diese solltest Du erstmal wiederholen.

von Yalu X. (yalu) (Moderator)


Lesenswert?

Wenn du alle Syntaxfehler beseitigt hast, solltest im Compileraufruf die
Warnungen aktivieren. Dann werden dir mehrere Variablen, die nirgends
initialisiert werden, und jede Menge Typfehler in fprintf- und
scanf-Aufrufen angezeigt.

von Dirk B. (dirkb2)


Lesenswert?

1
int FoodSelected (int selection);
 ist kein Funktionsaufruf, sondern eine Funktionsdeklaration.

fopen ist eine Funktion (mit Rückgabewert). Schau mal, wie du die 
aufrufst.

Du benutzt sehr viele globale Variablen.
Das ist eher blöd. Zudem definierst du innerhalb von main (das ist auch 
eine Funktion) lokale Variablen mit denselben Namen. Diese überdecken 
dann die globalen Variablen.

Das heißt, dass die globale Variable quantity aus der Funktion 
FoodSelected  eine andere Variable ist, als quantity in main

von Dave4 (Gast)


Lesenswert?

Und wenn das ganze läuft, kannst du dir nochmal "case" als Alternative 
zu den "if" anschauen

Außerdem kannst du die Mengenabfrage noch in eine weitere Funktion 
auslagern damit sich der Code nicht so oft wiederholt.

von ichversuchsmal (Gast)


Lesenswert?

Schaue in deinen Unterlagen nochmal nach den Themen Gültigkeitsbereich 
von Variablen, den Unterschieden zwischen Deklaration und Aufruf von 
Funktionen, dem Unterschied zwischen dem Erzeugen einer Variable und 
einer Wertzuweisung an eine Variable, nach Parameterübergabe an und dem 
Rückgabewert von Funktionen.

Das setzt du dann mit einer Funktion um. Baue dir an Stellen in deinem 
Code "printfs" ein und lasse die Werte, die du erwartest auf der Konsole 
ausgeben.

Du wirst überrascht sein.

Versuche nicht dein ganzes Programm auf einmal zum Laufen zu bringen.

Nur Mut. Generationen vor dir haben es auch nur so gelernt.

von Christoph (Gast)


Lesenswert?

ichversuchsmal schrieb:
> Versuche nicht dein ganzes Programm auf einmal zum Laufen zu bringen.
>
> Nur Mut. Generationen vor dir haben es auch nur so gelernt.

Vielen Dank für diesen Beitrag, das ist sehr motivierend!

Vielen Dank auch an alle anderen!
Ich habe mich mitlerweile mit ein paar Freunden zusammengesetzt und die 
Aufgabe gleich hoffentlich gelöst.

Vielen Dank!

von Wilhelm M. (wimalopaan)


Lesenswert?

Christoph schrieb:
> Ich habe mich mitlerweile mit ein paar Freunden zusammengesetzt und die
> Aufgabe gleich hoffentlich gelöst.

Sehr gut. Face2face ist immer noch am besten.

von Dirk B. (dirkb2)


Lesenswert?

Christoph schrieb:
> Ich habe mich mitlerweile mit ein paar Freunden zusammengesetzt und die
> Aufgabe gleich hoffentlich gelöst.

Du kannst gerne den Code nochmal hier einstellen.
Dann schauen wir nochmal drüber.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.