Ich habe eine Hausaufgabe, wo ich den Code ändern musste und eine zusätzliche Option hinzufügen, um eine ganze Zahl durch zwei zu teilen. Als ich jedoch meinen Abschnitt hinzugefügt habe, erhalte ich immer eine Fehlermeldung. HierErhalte Fehlermeldungen beim Erstellen einer neuen Funktion
ist der Code:
#include <stdio.h>
int main()
{
/* variable definition: */
int intValue, menuSelect,Results;
float floatValue;
intValue = 1;
// While a positive number
while (intValue > 0)
{
printf ("Enter a positive Integer\n: ");
scanf("%d", &intValue);
if (intValue > 0)
{
printf ("Enter 1 to calculate Square, 2 to Calculate Cube, 3 to divide by 2 \n: ");
scanf("%d", &menuSelect);
if (menuSelect == 1)
{
// Call the Square Function
Results = Square(intValue);
printf("Square of %d is %d\n",intValue,Results);
}
else if (menuSelect == 2)
{
// Call the Cube function
Results = Cube(intValue);
printf("Cube of %d is %d\n",intValue,Results);
}
else if (menuSelect == 3)
{
//Call the half function
Results = divide2(floatValue);
printf("Half of %d is %f\n", intValue,Results);
}
else
printf("Invalid menu item, only 1 or 2 is accepted\n");
}
}
return 0;
}
/* function returning the Square of a number */
int Square(int value)
{
return value*value;
}
/* function returning the Cube of a number */
int Cube(int value)
{
return value*value*value;
}
//Function returning the half of a number
float divide2(float value)
{
return value/2;
}
Und die Fehler, die ich bin immer sind:
prog.c: In function 'main':
prog.c:38:18: warning: implicit declaration of function 'Square' [-Wimplicit-function-declaration]
Results = Square(intValue);
^
prog.c:50:18: warning: implicit declaration of function 'Cube' [-Wimplicit-function-declaration]
Results = Cube(intValue);
^
prog.c:62:17: warning: implicit declaration of function 'divide2' [-Wimplicit-function-declaration]
Results = divide2(floatValue);
^
prog.c:64:14: warning: format '%f' expects argument of type 'double', but argument 3 has type 'int' [-Wformat=]
printf("Half of %d is %f\n", intValue,Results);
^
prog.c: At top level:
prog.c:101:7: error: conflicting types for 'divide2'
float divide2(float value)
^
prog.c:62:17: note: previous implicit declaration of 'divide2' was here
Results = divide2(floatValue);
^
Was ist falsch mit dem Code? Ich habe nicht die Nachrichten erhalten, wenn die Original-Code
Wenn Sie C-Code schreiben, stellen Sie sich vor, dass Sie den Compiler anweisen, Ihren Code Stück für Stück zu verstehen. Sie müssen Funktionen einführen, die andere Teile des Codes verwenden, bevor diese Verwendung erfolgt.In Ihrem speziellen Beispiel steht 'main' vor' Square', sodass der Compiler nicht wirklich weiß, was zu tun ist, wenn er Ihren 'Square'-Aufruf zum ersten Mal sieht. Versuchen Sie, Ihre Funktionen nach diesem Prinzip zu ordnen und zu prüfen, ob sie Ihr Problem lösen. –
Verwenden Sie nur Prototypen für alle Funktionen vor der Hauptfunktion und definieren Sie sie anschließend. – RastaJedi
Fügen Sie Prototypen hinzu oder definieren Sie die Funktion, bevor Sie sie tatsächlich aufrufen. Wenn Sie von main() aus aufrufen, definieren Sie sie vor main(). –