Hallo Stackoverflow Crew. Ich bin ein sehr Amateur C Programmierer und ich arbeite an einem Programm, das einige Eingaben über Hochzeitsgeschenke liest, und dann Informationen ausgibt, die den maximalen Geschenkwert, den minimalen Geschenkwert, den Gesamtdurchschnitt der Geschenkwerte und den Durchschnitt der Geschenke, die bei x> 0 bewertet wurden. Ich habe alles fertig geschrieben, aber das Programm scheint immer nach der ersten Runde zu stürzen. Ich habe es in den letzten Stunden angeschaut, also habe ich Probleme, den Fehler zu finden. Hier ist der Code, den ich habe:C: Programm stürzt ab, bevor für Schleife abgeschlossen
#include <stdio.h>
#include <stdlib.h>
int main() {
//Opens the file and creats a pointer for it.
FILE *ifp;
ifp = fopen("gifts.txt", "r");
//Declares the variables
int i, j, k, l, m, n, o, p, q, x, y;
int gift_sets, num_gifts, prices, max_value, max, avg_val, no_zero;
//Scans the file and assigns the first line to variable "gift_sets"
fscanf(ifp, "%d", &gift_sets);
//Begins a for loop that repeats based on the value of gift_sets
for (i = 0; i < gift_sets; i++) {
printf("Wedding Gifts #%d\n", i + 1);
printf("Gift Value\t Number of Gifts\n");
printf("----------\t ---------------\n");
//Scans the price values into the array prices[num_gifts]
fscanf(ifp, "%d", &num_gifts);
int prices[num_gifts];
//Creates a loop through the prices array
for (j = 0; j < num_gifts; j++){
fscanf(ifp, "%d", &prices[j]);
}
//Declares a frequency array
int freq[max + 1];
for (k = 0; k <= max; k++) {
freq[k] = 0;
}
for (l = 0; l < num_gifts; l++) {
freq[prices[l]]++;
}
for (m = 0; m < max + 1; m++) {
if (freq[m] > 0){
printf("%d\t%d",m, freq[m]);
}
}
printf("\n");
//Zeroes the variable "max_val."
int max_val = prices[0];
//Loops through the array to find the maximum gift value.
for (n = 0; n < num_gifts; n++){
if (prices[n] > max_value)
max_value = prices[n];
}
// Zeroes "min_val."
int min_val = prices[0];
//Finds the lowest value within the array.
for(o = 0; o < num_gifts; o++){
if(prices[o] !=0){
if(prices[o] < min_val){
min_val = prices[o];
}
}
}
//Calculates the total number of gifts.
double sum_gifts = 0;
for(p = 0; p < num_gifts; p++){
sum_gifts = sum_gifts + prices[p];
}
//Calculates the average value of all the gifts.
avg_val = (sum_gifts/num_gifts);
//find non zero average
double x = 0;
int y = 0;
for(q = 0; q < num_gifts; q++){
if (prices[q] != 0){
x += prices[q];
y++;
}
}
//Calculates the average value of the gifts, excluding the gifts valued zero.
int no_zero = x/y;
//Prints the maximum gift value.
printf("The maximum gift value is: $%d", max_value);
printf("\n");
//Prints the minimum gift value.
printf("The minimum gift value is: $%d\n", min_val);
//Prints the average of all the gifts.
printf("The average of all gifts was $%.2lf\n",avg_val);
//Prints the no zero average value of the gifts.
printf("The average of all non-zero gifts was $%.2lf",no_zero);
printf("\n\n\n");
}
return 0;
}
Vielen Dank im Voraus für die Hilfe Jungs. Wie immer wird es sehr geschätzt.
EDIT: Um weiter zu erarbeiten, der "Absturz" ist ein Windows-Fehler "gifts.exe funktioniert nicht mehr", wenn das Programm ausgeführt wird. Es sagt aber am unteren Rand des Fensters, das „Process -1073741819 < 0XC0000005 zurück>“
Es würde hilfreich sein, wenn Sie die Frage so bearbeiten, dass sie alle Fehlermeldungen enthält, die Sie erhalten. Haben Sie das Debuggen mit gdb oder ähnlichem versucht? – bernie
Das ist die Sache, es gibt keine Fehlermeldungen. Das Programm baut und läuft gut, aber ich bekomme einen Windows-Fehler "gifts.exe hat aufgehört zu arbeiten", wenn Sie das Programm ausführen. Es heißt am Ende des Fensters, dass "Process -1073741819 <0xC0000005>" – Batteries
diesen Fehlercode eine Zugriffsverletzung ist, was bedeutet, dass Sie wahrscheinlich versuchen, einen Nullzeiger irgendwo zu dereferenzieren. – pg1989