What is wrong with this program

This is a discussion on What is wrong with this program within the C Programming forums, part of the General Programming Boards category; ok so im making a purchase receipt kinda of program but everytime i try it it never works Code: #include ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    25

    Exclamation What is wrong with this program

    ok so im making a purchase receipt kinda of program but everytime i try it it never works

    Code:
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        char tea,y,n;
        double tot,dis,tax,toty,totu,toty2,tot1,tot2,totu2,taxs;
        printf("What is the total amount of the purchase: ");
        scanf("&#37;d",&tot);
        printf("Is the buyer a teacher?: ");
        scanf("%c",&tea); 
        printf("%c",tea);
        if (tea==y){
                    if (tot>=100){
                    tot1=tot*0.12;     
                    toty=tot-tot1;
                    taxs=toty*0.05;
                    toty2=toty+taxs;
                    printf("Your savings today are: %f\n",tot1);
                    printf("You get a 12% discount, so your total before tax is: %f\n",toty);
                    printf("Your Tax Amount is: %f\n",taxs);
                    printf("Your total with a 5% sales tax is: %f\n",toty2);
                    }
                    else if (tot>0){
                    tot2=tot*0.10;
                    totu=tot-tot2;
                    tax=totu*0.05;
                    totu2=totu+tax;
                    printf("Your savings today are: %f\n",tot2);
                    printf("You get a 10% discount, so your total before tax is: %f\n",totu);
                    printf("Your Tax Amount is: %f\n",tax);
                    printf("Your total with a 5% sales tax is: %f\n",totu2);
                    }                
                    else {
                    printf("Enter an amount Higher than Zero\n");
                    }
                    }
    getchar();
    }
    what is wrong with it, i don't understand any help would be appreciated
    Thanks
    Last edited by dals2002; 02-17-2008 at 08:46 PM.

  2. #2
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Quote Originally Posted by dals2002 View Post
    ok so im making a purchase receipt kinda of program but everytime i try it it never works
    what is wrong with it, i don't understand any help would be appreciated
    Thanks
    Code:
        char tea,y,n;
    Allright then... you declared three variables of type char with names: tea,y,n
    Code:
        if (tea==y){
    But do you think that a variables name is equal to it's value?
    You need to compare the 'tea' named variable with the character 'y'.
    That's the problem
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    How does it not work?

    Oh yes, and please indent your code more consistently.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    tot is a double so you should be using &#37;lf in your scanf
    Code:
    scanf("%lf",&tot);

    there is a newline character left over in the input buffer after entering a total, so tea is likely getting set to '\n'
    A simple solution is to call getchar() after scanf("%lf",&tot); to remove the new line character. A better suggestion may be to use fgets to read a line and sscanf to parse it.


    y is an uninitialized variable here. Either make it a literal 'y' or initialize the variable.
    Code:
    if (tea==y){

    When printing a percent sign you need to escape it with a percent.
    Code:
    printf("You get a 12%% discount, so your total before tax is: %f\n",toty);

    You should work on your indentation.
    There is no else to your first if, unless you just haven't gotten that far yet.


    And of course you should ask more specific questions.
    Last edited by spydoor; 02-17-2008 at 09:13 PM.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    25
    ok i have inputed all that u guys wrote, but now as soon as i input the Y it closes up, so why doesn't it let me pass the Y, is it not accepting the char Y?

    Code:
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        char tea;
        double tot,dis,tax,toty,totu,toty2,tot1,tot2,totu2,taxs;
        printf("What is the total amount of the purchase: ");
        scanf("%lf",&tot);
        getchar();
        printf("Is the buyer a teacher?: ");
        scanf("%c",&tea); 
        if (tea=='Y'){
                    if (tot>=100){
                       tot1=tot*0.12;     
                       toty=tot-tot1;
                       taxs=toty*0.05;
                       toty2=toty+taxs;
                                       printf("Your savings today are: %f\n",tot1);
                                       printf("You get a 12'%' discount, so your total before tax is: %f\n",toty);
                                       printf("Your Tax Amount is: %f\n",taxs);
                                       printf("Your total with a 5'%' sales tax is: %f\n",toty2);
                                       }
                    else if (tot>0){
                         tot2=tot*0.10;
                         totu=tot-tot2;
                         tax=totu*0.05;
                         totu2=totu+tax;
                                        printf("Your savings today are: %f\n",tot2);
                                        printf("You get a 10'%' discount, so your total before tax is: %f\n",totu);
                                        printf("Your Tax Amount is: %f\n",tax);
                                        printf("Your total with a 5'%' sales tax is: %f\n",totu2);
                                        }
                                        }
                             
         else {
           printf("Enter an amount Higher than Zero\n");
         }          
         getchar();
         }

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    You probably just need another getchar() at the end to "keep the window open"

    Since you have a leftover new line from your second scanf now

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    25
    oh nice thanks guys it worked took me a while but i got it thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Maze Program - What am I doing wrong?
    By Gipionocheiyort in forum C++ Programming
    Replies: 20
    Last Post: 08-02-2007, 01:31 PM
  2. Replies: 5
    Last Post: 01-13-2007, 01:14 AM
  3. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 01:03 PM
  4. Whats wrong with this program - Output of a series
    By duffmckagan in forum C Programming
    Replies: 2
    Last Post: 07-26-2006, 09:57 AM
  5. Whats wrong with my program?
    By Ruflano in forum C++ Programming
    Replies: 5
    Last Post: 02-21-2002, 04:09 PM

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