Like Tree4Likes
  • 1 Post By c99tutorial
  • 2 Post By grumpy
  • 1 Post By c99tutorial

please what is wrong in this program.......

This is a discussion on please what is wrong in this program....... within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main() { float pin,code,choice,ch1,ch2,ch3,ch4; float bal,amount,diff; float account; float bill,p1,p2,p3,p4,p5,p6; char answer; clrscr(); do { printf("Welcome\n"); ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    13

    please what is wrong in this program.......

    Code:
    #include <stdio.h>
     int main()
     {
      float pin,code,choice,ch1,ch2,ch3,ch4;
      float bal,amount,diff;
      float account;
      float bill,p1,p2,p3,p4,p5,p6;
      char answer;
     
     clrscr();
     do
     {
      printf("Welcome\n");
      printf("To begin transaction\n");
      printf("Please enter your pin:");
      scanf("%f",& pin);
     
     clrscr();
     
     code=12345;
     if(pin==code)
     {
      printf("[1] Withdrawal\n");
      printf("[2] Balance Inquiry\n");
      printf("[3] Money Transfer\n");
      printf("[4] Bill Payments\n");
      printf("Please select transaction:");
      scanf("%f",& choice);
       ch1=1;
       ch2=2;
       ch3=3;
       ch4=4;
     clrscr();
       if(choice==ch1)
       {
       bal=5000;
       printf("Enter amount:");
       scanf("%f",& amount);
       printf("Previous Balance:%.2f\n",bal);
       printf("Withrawal:%.2f\n",amount);
       diff=bal-amount;
       printf("Current Balance:%.2f",diff);
       }
       else if(choice==ch2)
       {
       bal=5000;
       prinf("Current Balance:%.2f\n",bal);
       }
       else if(choice==ch3)
       {
       bal=5000;
       printf("Enter Account Number:");
       scanf("%f",& account);
       printf("Enter Amount to Transfer:");
       scanf("%f",& amount);
       printf("Previous Balance:%.2f\n",bal);
       printf("Amount Transfered:%.2f\n",amount);
       diff=bal-amount;
       printf("Current Balance:%.2f",diff);
       }
        else if(choice==ch4)
        {
        printf("[1] Maynilad\n");
        printf("[2] Meralco\n");
        printf("[3] Sky Cable\n");
        printf("[4] PLDT\n");
        printf("[5] Globe\n");
        printf("[6] Smart\n");
        printf("Please Select Bill:");
        scanf("%f",& bill);
           p1=1;
           p2=2;
           p3=3;
           p4=4;
           p5=5;
           p6=6;
            if(bill==p1)
            {
            bal=5000;
            printf("Maynilad\n");
            printf("Enter Account Number:");
            scanf("%f",& account);
            printf("Enter Amount:");
            scanf("%f",& amount);
            diff=bal-amount;
            clrscr();
            printf("Maynilad\n");
            printf("Account Number:%.2f\n",account);
            printf("Previous Balance:%.2f\n",bal);
            printf("Amount:%.2f\n", amount);
            printf("Current Balance:%.2f\n",diff);
            }
            else if(bill==p2)
            {
            bal=5000;
            p("Meralco\n");
            printf("Enter Account Number:");
            scanf("%f", & account);
            printf("Enter Amount:");
            scanf("%f",& amount);
            diff=bal-amount;
            clrscr();
            printf("Meralco\n");
            printf("Account Number:%.2f\n",account);
            printf("Previous Balance:%.2f\n",bal);
            printf("Amount:%.2f\n", amount);
            printf("Current Balance:%.2f\n",diff);
            }
            else if(bill==p3)
            {
            bal=5000;
            printf("Sky Cable\n");
            printf("Enter Account Number:");
            scanf("%f",& account);
            printf("Enter Amount:");
            scanf("%f",& amount);
            diff=bal-amount;
            clrscr();
            printf("Sky Cable\n");
            printf("Account Number:%.2f\n",account);
            printf("Previous Balance:%.2f\n",bal);
            printf("Amount:%.2f\n", amount);
            printf("Current Balance:%.2f\n",diff);
            }
            else if(bill==p4)
            {
            bal=5000;
            printf("PLDT\n");
            printf("Enter Account Number:");
            scanf("%f",& account);
            printf("Enter Amount:");
            scanf("%f",& amount);
            diff=bal-amount;
            clrscr();
            printf("PLDT\n");
            printf("Account Number:%.2f\n",account);
            printf("Previous Balance:%.2f\n",bal);
            printf("Amount:%.2f\n", amount);
            printf("Current Balance:%.2f\n",diff);
            }
            else if(bill==p5)
            {
            bal=5000;
            printf("Globe\n");
            printf("Enter Account Number:");
            scanf("%f",& account);
            printf("Enter Amount:");
            scanf("%f",& amount);
            diff=bal-amount;
            clrscr();
            printf("Globe\n");
            printf("Account Number:%.2f\n",account);
            printf("Previous Balance:%.2f\n",bal);
            printf("Amount:%.2f\n", amount);
            printf("Current Balance:%.2f\n",diff);
            }
            else if(bill==p6)
            {
            bal=5000;
            printf("Smart\n");
            printf("Enter Account Number:");
            scanf("%f",& account);
            printf("Enter Amount:");
            scanf("%f",& amount);
            diff=bal-amount;
            clrscr();
            printf("Smart\n");
            printf("Account Number:%.2f\n",account);
            printf("Previous Balance:%.2f\n",bal);
            printf("Amount:%.2f\n", amount);
            printf("Current Balance:%.2f\n",diff);
            }
            else
            {
            printf("Incorrect Bill choice");
            }
      }
      else
      {
      printf("Incorrect choice");
      }
      }
      else
      {
      printf("Incorrect Pin");
      }
      clrscr();
      printf("\nWould you like to try it again?[Y/N]");
      answer=getch();
      }
      while(answer=='y'||answer=='Y');
      exit();
      getch();
      }
    Code:
     

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,123
    Question 1. Does the program compile successfully without warnings?
    If yes, go to question 2.

    Question 2. Does the program give you the expected result?
    If no, go to question 3.

    Question 3. What result were you expecting and what have you tried to fix it?

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    13
    i got a problem in do...while
    Code:
     printf("\nWould you like to try it again?[Y/N]");
      answer=getch();
      }
      while(answer=='y'||answer=='Y');
      exit();
      getch();
      }
    i want to make a program...
    when i type Y it will ask again the pin
    when i type N it will exit.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,344
    Your problem is, most likely, that your code does not compile since you are using several functions (getch(), clrscr(), etc) that are not part of the standard C library.

    Your usage of exit() - which is in standard C, albeit declared in <stdlib.h> which your code does not #include - is also not correct. The exit() in the standard library accepts an argument, and never returns.

    There are also a few typos (eg leaving a 't' out of "printf()") which will cause a compiler to barf.

    My main advice is to try reading the error messages emitted by your compiler. Start at the top (the first error message emitted, not the bottom) and correct the problems in order.

    Beyond that, it would probably be useful if you indented your code to make it easier to understand. Code that is laid out in a manner that is easier to understand is easier to get right.
    AndiPersti and stahta01 like this.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    That's more complicated then. You have to be ready for an uncooperative user who won't necessarily enter 'y' or 'n' and respond with a reprompt.

    To get exclusively 'y' or 'n', follow the series of steps.

    1. Prompt and get yes or no
    2. If input is not yes or no, go back to 1. Otherwise go to 3.
    3. Return the input (or use the input)

    Once you have confirmation the user followed directions, you can use the input in your larger do-while loop.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,123
    Another problem is that you're combining things like scanf and getch or getchar to read input lines from the keyboard. This doesn't always work as expected. Consider this example

    Code:
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    printf("Do you want to print the number? ");
    int c = getchar();
    if (c == 'Y' || c == 'y')
        printf("The number is %d\n", num);
    If you run that program, the line `The number is ...' will never be printed because getchar() reads the '\n' or other whitespace that is left over in the buffer. A better overall approach would be to read an entire line of input at a time and then decide on what it contains.

    Fixed example:
    Code:
    char linebuf[1000];
    
    int num;
    printf("Enter a number: ");
    fgets(linebuf, 1000, stdin);
    sscanf(linebuf, "%d", &num);
    
    char c;
    printf("Do you want to print the number? ");
    fgets(linebuf, 1000, stdin);
    sscanf(linebuf, " %c", &c);
    if (c == 'Y' || c == 'y')
        printf("The number is %d\n", num);
    Note that the space in the format string " %c" allows that the user may precede the answer with some whitespace before typing a "y" or "Y". In this way, the program also can become more user tolerant.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Something is wrong with my program
    By november1992 in forum C Programming
    Replies: 1
    Last Post: 05-07-2012, 11:49 AM
  2. What is wrong with the following program?
    By roaan in forum C Programming
    Replies: 14
    Last Post: 08-27-2009, 12:52 PM
  3. Can Anyone tell me what is wrong with this program?
    By GrlNewB in forum C++ Programming
    Replies: 8
    Last Post: 03-06-2003, 10:50 AM
  4. Where is this program going wrong?
    By Kirby1024 in forum C Programming
    Replies: 8
    Last Post: 09-01-2002, 03:32 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21