Please find an error in my noob program

This is a discussion on Please find an error in my noob program within the C Programming forums, part of the General Programming Boards category; The code works on another system, but when run on another computer (and possible an older compiler), the program either ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    19

    Please find an error in my noob program

    The code works on another system, but when run on another computer (and possible an older compiler), the program either runs in an infinite loop or does not recognize the redirected input file.

    Here is the code:

    Code:
    #include <stdio.h>
    
    int main()
    {
    
           int prod_num, number, counter=0, major_code=0, minor_code=0,
    tax_code=0, where=0, total_number=0;
           double base_cost, minor_factor=0, shipping_costs=0, tax_amount=0,
    final_unitprice=0,average_final, final_subtotal=0, final_price_product;
    
           while ( scanf("%d %lf %d", &prod_num, &base_cost, &number) != EOF)
           {
                   total_number += number;
    			   counter++;
                   major_code = prod_num / 100000;
                   minor_code = (prod_num % 100000) / 100;
                   tax_code = (prod_num % 100) / 10;
                   where = prod_num % 10;
    
                   if(minor_code >= 0 && minor_code <= 399) {
                           minor_factor = 1.45;
                   }
    
                   else if ((minor_code>=400 && minor_code <=799) || (minor_code>=950
    && minor_code <=955)) {
                           minor_factor = 1.12;
                   }
    
                   else {
                           minor_factor = 0.89;
                   }
    
                   switch(where)
                   {
                           case 0:         shipping_costs = 7.50;
                                                   break;
    
                           case 1:         shipping_costs = 8.47;
                                                   break;
    
                           case 2:         shipping_costs = 6.05;
                                                   break;
    
                           case 3:         shipping_costs = 10.20;
                                                   break;
    
                           default:        shipping_costs = 20.00;
                                                   break;
    
                   }
    
                   if(tax_code == 1)
    
                           {
    
                           if(major_code>=0 && major_code<=250)
                                   tax_amount = 0.12;
    
                           else if(major_code>=251 && major_code<=450)
                                   tax_amount = 0.15;
    
                           else
                                   tax_amount = 0.25;
    
                           }
    
                   else if (tax_code == 0)
                           tax_amount = 0;
    
                   final_unitprice = (base_cost * minor_factor + shipping_costs) * (1 + tax_amount);
                   final_price_product = final_unitprice * number;
    			   final_subtotal += final_price_product;
    			   
                   printf("---------------------------> PRODUCT [%d]<---------------------------\n", counter);
                   printf("\n");
                   printf("Product #: %d || Price/Unit: $%.2f || # of Products: %d || 
    Total: $%.2f\n", prod_num, final_unitprice, number, final_price_product);
                   printf("\n");
                   printf("\n");
    
           }
    
    average_final = final_subtotal / total_number;
    
    		printf("\n");
    		printf("The total price is: $%.2f\n", final_subtotal);
    		printf("The average price per product is: $%.2f", average_final);
    		printf("\n");
    		
           return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    what operating system does the program not work on?

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    First,
    Code:
                   printf("Product #: %d || Price/Unit: $%.2f || # of Products: %d || 
    Total: $%.2f\n", prod_num, final_unitprice, number, final_price_product);
    don't wraps quoted strings this way. And avoid the line continuation thingy (which would be this):
    Code:
                   printf("Product #: %d || Price/Unit: $%.2f || # of Products: %d || \
    Total: $%.2f\n", prod_num, final_unitprice, number, final_price_product);
    Instead, make use of automatic concatenation of adjacent string literals:
    Code:
          printf("Product #: %d || Price/Unit: $%.2f || # of Products: %d || "
                 "Total: $%.2f\n", prod_num, final_unitprice, number, final_price_product);
    Here
    Code:
       while ( scanf("%d %lf %d", &prod_num, &base_cost, &number) != EOF )
    it would be better to check for success rather than one potential failure:
    Code:
       while ( scanf("%d %lf %d", &prod_num, &base_cost, &number) == 3 )
    Can you post a sample input that causes the problem?
    Last edited by Dave_Sinkula; 10-23-2005 at 02:57 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  2. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  3. my program can't find "allegro.h"
    By Leeman_s in forum C++ Programming
    Replies: 3
    Last Post: 09-10-2002, 08:05 PM
  4. Replies: 4
    Last Post: 08-15-2002, 11:35 AM
  5. noob with a progblem in program
    By Klinerr1 in forum C++ Programming
    Replies: 4
    Last Post: 06-15-2002, 11:38 AM

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