Thread: SOS no errors, but program not running

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    3

    Unhappy SOS no errors, but program not running

    Hey there

    I'm new to C programming, fluent in Java. I have a program that I need to create for part of an assignment. This is what I have programmed but for some reason, the program does not allow me to make a second selection, and terminates. Any advice would be appreciated.

    Code:
    #include <stdio.h>
    #include <math.h>
    #define pi 3.14159
    int main()
    {
    
    double al=2.70; /*2.70g/cm3*/
    double st=7.85;
    double cu=8.94;
    char opt1, opt2, metal;
    double dens;
    
    printf("Please enter the menu option corresponding \nto the shape of the machined part you wish to build.\n");
    printf("A - Cylinder \nB - Rectangle \nC - Cone\nEnter option: ");
    scanf("%c", &opt1);
    
    printf("Please enter the menu option corresponding \nto the metal you wish to build the part from: ");
    printf("\n1 - Aluminium \n2 - Copper \n3 - Steel\nEnter option: ");
    scanf("%c",&opt2);
    
    
    int check=0;
    
    switch(opt2)
    {
        case '1':
        {
            dens=al;
            metal=itoa("Aluminium");
            check=1;
            break;
            }
        case '2':
        {
            dens=cu;
            metal=itoa("Copper");
            check=1;
            break;
            }
        case '3':
        {
            dens=st;
            metal=itoa("Steel");
            check=1;
            break;
            }
         default:
        {
            printf("Invalid option selected.");
            break;
        }
    }
    
    
    if(check==1)
    {
    
    switch(opt1)
    {
        case 'A':
        {
            double r,h, vol, weight;
    
            printf("Please enter the radius of the cylinder in cm: ");
            scanf("%lf",&r);
            printf("Please enter the height of the cylinder in cm: ");
            scanf("%lf", &h);
    
            vol=pi*pow(r,2)*h;
            weight=vol*dens;
    
            printf("********************\n  MACHINE PART \n  Shape: Cylinder \n   Metal: %c\n    Metal density: %lf\n    Volume: %lf\n    Weight: %lf\n********************\n", metal, dens, vol, weight);
    
            break;
        }
        case 'B':
        {
             double l,b,h, vol, weight;
    
            printf("Please enter the length of the rectangle in cm: ");
            scanf("%lf",&l);
            printf("Please enter the breadth of the rectangle in cm: ");
            scanf("%lf",&b);
            printf("Please enter the height of the rectangle in cm: ");
            scanf("%lf", &h);
    
            vol=l*b*h;
            weight=vol*dens;
    
            printf("********************\n  MACHINE PART \n  Shape: Rectangle \n   Metal: %c\n    Metal density: %lf\n    Volume: %lf\n    Weight: %lf\n********************\n", metal, dens, vol, weight);
    
            break;
        }
        case 'C':
        {
             double r,h, vol, weight;
    
            printf("Please enter the radius of the cone in cm: ");
            scanf("%lf",&r);
            printf("Please enter the height of the cone in cm: ");
            scanf("%lf", &h);
    
            vol=(1.0/3.0)*pi*pow(r,2)*h;
            weight=vol*dens;
    
            printf("********************\n  MACHINE PART \n  Shape: Cone \n   Metal: %c\n    Metal density: %lf\n    Volume: %lf\n    Weight: %lf\n********************\n", metal, dens, vol, weight);
    
            break;
        }
        default:
        {
            printf("Invalid option selected.");
            break;
        }
    }
    }
    }

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    its atoi and NOT itoa

    a way to remember: ascii TO integer

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    Hey thanks still not running properly tho

    about the atoi - itoa function, i was wondering if maybe my datatype is wrong all together...is using a char to store what in Java is a string acceptable?

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    scanf("%c", &opt1);
    That scans in your character but leaves the newline character in your buffer, which is gladly accepted as a character for the next input. Cprogramming.com FAQ > How do I get a number from the user (C)

  5. #5
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    It only runs ONE TIME. You need to add a third option of telling it when to quit and then use a WHILE statement or a DO-WHILE until that third option is used. You can put that third option in the first switch(opt2), like a 'Q' or 'q'. It would work the same in Java also.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    sorry im confused...i understand y the scanf isnt working - the whole buffer thing. but im not sure how to fix it...

    @kcpilot what only runs one time and what must quit?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do we make the program underline errors?
    By chickenlittle in forum C++ Programming
    Replies: 2
    Last Post: 12-15-2010, 07:06 PM
  2. Few Simple Program Errors
    By pobri19 in forum C Programming
    Replies: 3
    Last Post: 05-22-2008, 06:12 PM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. Determaining if a program is running
    By WebmasterMattD in forum Linux Programming
    Replies: 4
    Last Post: 04-09-2002, 04:36 PM

Tags for this Thread