Please help have trouble my with porgram

This is a discussion on Please help have trouble my with porgram within the C++ Programming forums, part of the General Programming Boards category; I having some trouble with my if statement would someone help me out main.cpp Code: #include<stdio.h> void main() { char ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    3

    Please help have trouble my with porgram

    I having some trouble with my if statement would someone help me outmain.cpp
    Code:
    #include<stdio.h>
    
    void main()
    { 
        char instruc[2];
        const  double artichokes=1.25,beets=0.65,carrots=0.89,discount=0.05;
        double totalartichokes,totalbeets,totalcarrots,totalpound=0,shipping,input,totalcost,pounds;
        bool keepgoing =true;// a bolean data type
    
            printf("\n");
            printf("                           ABC Mail Order Grocery\n\n");
            printf(" Type 'a' for artichokes, 'b' for beets, 'c' for carrots , and 'q' to exit sale.\n\n");
        //frist we need an infinite loop
        while (1){// 1 is always logicaly true
            //next we will read an instruction from stdio
    
            scanf("%s %lf",instruc, &input);
                switch(instruc[0])
            {
               case 'a':
                   pounds+=input;
                   totalartichokes=artichokes*input;
                   printf(" Artichokes  %lf\n", totalartichokes);
                 break;
               case 'b':
                  pounds+=input;
                  totalbeets=beets * input;
                  printf(" beets  %lf\n", totalbeets);
                 break;
               case 'c':
                    pounds+=input;
                    totalcarrots=carrots * input;
                    printf(" carrots %lf\n", totalcarrots);
                 break;
              case 'd':
                    totalpound+=pounds;
                    totalcost= totalartichokes+totalbeets+totalcarrots ;
                    printf("\n Total Weight is %.2lf",totalpound);
                    printf("\n SubTotal is $%.2lf \n",totalcost);
                    if(totalcost>= 100)
                        totalcost=totalcost-(.05*totalcost);
                    if(totalpound >= 5)
                    totalcost=3.50+totalcost;// $3.50 shipping
                    shipping=3.50;
                    if(totalpound <= 5 )
                    if(totalpound>=20)
                    totalcost=10.00+totalcost;// $10.00 over 5pounds ,but under 20pounds shipping
                    shipping=10.00;
    
                    if(totalpound < 20)
                    totalcost=8.00+(.10*totalpound)+totalcost;
                    shipping=8.00+(.10*totalpound); 
                    break;
              case 'q':
                    keepgoing = false;
                    break;
                    {printf(" Shipping and handling is $%.2lf\n",shipping);
                    printf("\n Total is $%.2lf \n",totalcost);}
            
            }// end switch(instruc[0])
       
        
                        
            if (keepgoing==false)break; // exit the infinite loop
    
    
            }//end while (1) loop
    }//end main

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Which one. Elaborate. Only thing I can see is that

    Code:
                    if(totalpound <= 5 )
                    if(totalpound>=20)
                    totalcost=10.00+totalcost;// $10.00 over 5pounds ,but under 20pounds shipping
    Will never evaluate to true. Use extra braces around if statements if you are unsure.

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  3. #3
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84
    I am sorry I don't have time to really look at your code, but a quick pass reveals a lack of braces. They are free! Use them to space out your code and make it look more organized.

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    What is the if statements error? Also, this is a C++ board and that is coded in C.
    Please print the logical error you have encountered. it will be easier to tell you what you are doing wrong.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > #include<stdio.h>
    > void main()
    1. main returns an int
    2. Apart from your single bool, there is very little C++ in this C program. Which language are you learning?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3
    I'm new to C++. Just started taken class at Cincinnati State .

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    As I'm sure you are aware, no code is executed after you've breaked from a switch statement:
    Code:
              case 'q':
                    keepgoing = false;
                    break;
                    {printf(" Shipping and handling is $%.2lf\n",shipping);
                    printf("\n Total is $%.2lf \n",totalcost);}
            
            }// end switch(instruc[0])
    The code in red will never be executed.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3
    Thanks for the help I've my final code ready for tomorrows class
    Code:
    #include<stdio.h>
      int main(void)
    {
        char instruc[2];
        const  double artichokes=1.25,beets=0.65,carrots=0.89;
        double totalartichokes=0,totalbeets=0,totalcarrots=0,totalpound=0,shipping=0,input=0,totalcost=0,pounds=0,subtotalcost=0,discount=0;
        bool keepgoing =true;// a bolean data type
            printf("\n");
            printf("\n");
            printf("                           ABC Mail Order Grocery\n\n");
            printf(" Type 'a' for artichokes, 'b' for beets, 'c' for carrots , 't' to total sale ,\n and 'q' to exit sale.\n\n");
        //frist we need an infinite loop
        while (1){// 1 is always logicaly true
            //next we will read an instruction from stdio
    
            scanf("%s %lf",instruc, &input);
                switch(instruc[0])
            {
                case 'a':
                    pounds+=input;
                    totalartichokes=artichokes*input;
                    printf(" Artichokes  %.2lf\n", totalartichokes);
                   break;
                case 'b':
                    pounds+=input;
                    totalbeets=beets * input;
                    printf(" beets  %.2lf\n", totalbeets);
                  break;
                case 'c':
                    pounds+=input;
                    totalcarrots=carrots * input;
                    printf(" carrots %.2lf\n", totalcarrots);
                  break;
    
               { case 'q': {keepgoing = false;break; } }//end of exiting if statement
    
                 case 't':
                   subtotalcost= totalartichokes+totalbeets+totalcarrots ;
                   totalpound+=pounds;
                   printf("\n SubTotal is $%.2lf \n",subtotalcost);
                  { if (subtotalcost>= 100) {
                        totalcost = subtotalcost - .05 * subtotalcost ;
                          discount = .05 * subtotalcost ;
    
                        printf(" Discount is $%.2lf\n",discount);
                        printf("\n Total is $%.2lf \n",totalcost);     } }//end of discount if statement
                  {if(totalpound <=5)  {
                       totalcost=3.50+totalcost;// $3.50 shipping
                       shipping=3.50;
                     printf("\n Total Weight is %.2lf\n",totalpound);
                    printf(" Shipping and handling is $%.2lf\n",shipping);
                    printf("\n Total is $%.2lf \n",totalcost);
                      break; }
                    if(totalpound <= 20){
                    totalcost=10.00+totalcost;// $10.00 over 5pounds ,but under 20pounds shipping
                   shipping=10.00;
    
                   printf("\n Total Weight is %.2lf\n",totalpound);
                   printf(" Shipping and handling is $%.2lf\n",shipping);
                   printf("\n Total is $%.2lf \n",totalcost);break; }
                   if (totalpound >20)    {
                   totalcost=8.00+.10*totalpound+totalcost;
                   shipping=8.00+(.10*totalpound);
    
                   printf("\n Total Weight is %.2lf\n",totalpound);
                   printf(" Shipping and handling is $%.2lf\n",shipping);
                   printf("\n Total is $%.2lf \n",totalcost);break;  }   }//end of shipping if statement
    
                  
            }// end switch(instruc[0])
       
    
                        
            if (keepgoing==false)break; // exit the infinite loop
    
    
            }//end while (1) loop
    }//end main
    here my program is running
    Name:  outputassignment6.gif
Views: 82
Size:  15.2 KB

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    1. Make a safe backup

    Not too bad - now you can polish it a bit by tidying up the code formatting
    - making sure all the blocks of code are indented consistently for example.

    > while (1)
    Try while ( keepgoing )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with assignment in C
    By mohanlon in forum C Programming
    Replies: 17
    Last Post: 06-23-2009, 10:44 AM
  2. Replies: 6
    Last Post: 01-03-2007, 02:02 PM
  3. Is it so trouble?
    By Yumin in forum Tech Board
    Replies: 4
    Last Post: 01-30-2006, 03:10 PM
  4. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 11:58 AM
  5. C++ program trouble
    By senrab in forum C++ Programming
    Replies: 7
    Last Post: 04-29-2003, 11:55 PM

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