简体   繁体   中英

Netbeans C program return value 2

I just got my Netbeans tools configured,and ( finally ) it worked :) (I checked with Hello World). We were shifting so I have had like a 3 month gap so I though I'd make a calculator rather than some other program like a palindrome checker and bleh bleh bleh. So here's the code :

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void addition(int,int);
void subtraction(int,int);
void  mutiplication(int,int);
void division(int,int);
int main() {
    int x,y,choice,redo = 1;
    while(redo)
    {
    printf("\nWelcome to the CalC :D\nPlease make a choice\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n>");
    scanf("%d",&choice);
    switch(choice);
    {
    case '1' :
        {
            printf("Enter the first number\n>");
            scanf("%d",&x);
            printf("\nThe second number?\n>");
            scanf("%d",&y);
            addition(x,y);
        }
        case '2' :
        {
            printf("Enter the first number\n>");
            scanf("%d",&x);
            printf("\nThe number to be subtracted from %d is?\n>",x);
            scanf("%d",&y);
            subtraction(x,y);
        }
        case '3' :
        {
            printf("Enter the first number\n>");
            scanf("%d",&x);
            printf("\nThe number to be multiplied with %d is?\n>",x);
            scanf("%d",&y);
            multiplication(x,y);

        }

        case '4' :
        {
            printf("Enter the first number\n>");
            scanf("%d",&x);
            printf("\nThe number to be divided by %d is?\n>",x);
            scanf("%d",&y);
            division(x,y);
        }
    }
    printf("\nWould you like to make another calculation?\n1.Yes '_' \n2.No! :p\n>");
    scanf("%d",&redo);

    }
    return (EXIT_SUCCESS);
}

void addition(int x,int y)
{
    int sum;
    sum = x + y;
    printf("\nThe sum of %d and %d is %d\n(Press enter to display the menu)",x,y,sum);
    getch();
}

void subtraction(int x,int y)
{
    int difference;
    ce;
    difference = x - y;
    printf("The difference between %d and %d is %d\n(Press enter to display the        menu)",x,y,difference);
    getch();
}

void multiplication(int x,int y)
{
    int product;
    product = x * y;
    printf("The product of %d and %d is %d\n(Press enter to display the menu)",x,y,product);
    getch();
}

void division(int x,int y)
{
    float quotent;
    quotent = (float)x/(float)y;
    printf("The quotient of %d and %d is %.2f\n(Press enter to display the menu)",x,y,quotent);
    getch();
}

And this is the error I get :

"/C/MinGW/bin/make.exe" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory 'C:/Users/CaptFuzzyboots/Documents/NetBeansProjects/CalC'
"c:/MinGW/bin/make.exe"  -f nbproject/Makefile-Debug.mk dist/Debug/MinGW-Windows/calc.exe
make[2]: Entering directory 'C:/Users/CaptFuzzyboots/Documents/NetBeansProjects/CalC'
mkdir -p build/Debug/MinGW-Windows
rm -f build/Debug/MinGW-Windows/main.o.d
gcc    -c -g -MMD -MP -MF build/Debug/MinGW-Windows/main.o.d -o build/Debug/MinGW-Windows/main.o main.c
main.c: In function 'main':
main.c:26:5: error: case label not within a switch statement
main.c:34:9: error: case label not within a switch statement
main.c:42:9: error: case label not within a switch statement
main.c:52:9: error: case label not within a switch statement
main.c: In function 'subtraction':
main.c:78:5: error: 'ce' undeclared (first use in this function)
main.c:78:5: note: each undeclared identifier is reported only once for each function it appears in
main.c: At top level:
main.c:83:6: warning: conflicting types for 'multiplication' [enabled by default]
main.c:48:13: note: previous implicit declaration of 'multiplication' was here
nbproject/Makefile-Debug.mk:66: recipe for target 'build/Debug/MinGW-Windows/main.o' failed
make[2]: *** [build/Debug/MinGW-Windows/main.o] Error 1
make[2]: Leaving directory 'C:/Users/CaptFuzzyboots/Documents/NetBeansProjects/CalC'
nbproject/Makefile-Debug.mk:59: recipe for target '.build-conf' failed
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory 'C:/Users/CaptFuzzyboots/Documents/NetBeansProjects/CalC'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
make: *** [.build-impl] Error 2


BUILD FAILED (exit value 2, total time: 661ms)

I'm used to code::blocks so I don't really know what's the problem here :\\ please help me out,I clearly defined the switch labels as '1','2','3','4'! Thanks guys :)

You have an extra semicolon after switch(choice) . Your code has

switch(choice);

and it should be

switch(choice)

A few other problems:

1) You misspelled multiplication when you defined the function at the top.
2) There's a stray ce; in your subtraction function.
3) You don't have any break statements in your case statements (it looks like you don't want the cases to fall through, so you probably need them)
4) choice is an int , but you're casing on char s. Since you're reading ints with scanf , you probably want to have 1,2,3,4 instead of '1','2','3','4' .

  • do case '1' instead of case 1
  • write multiplication and not mutiplication
  • remove the "ce" from the subtraction function
  1. remove semicolon after switch(choice)
  2. have a 'break' in each case
  3. at the begining, correct spelling of multiplication
  4. remove "ce;" inside the function subtraction.
  5. Change cases '1', '2', etc to 1, 2...
  6. Please note that, running a normal C code from Netbeans IDE should produce the same result if you run it in Eclipse or some other IDE. Your error is noway related to Netbeans.

Your main issue is the extra semi-colon after switch (choice) . Remove that semi-colon, and be sure to have a break after each case as well.

You also have choice declared as an integer, but your case labels are switching on a char ; it would be advisable to get rid of the single quotes around each case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM