C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 23rd, 2005, 03:22 PM
dekoi dekoi is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 12 dekoi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 27 m 52 sec
Reputation Power: 0
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;
}


Reply With Quote
  #2  
Old October 23rd, 2005, 04:47 PM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 8 m 6 sec
Reputation Power: 4
Looks good to me, and it ran fine on my computer.

What do you mean "run on an older compiler"? Once you compile the program, it won't need a compiler and can stand alone by itself. Speaking of which, which compiler are you using?

As far as the newer vs old computer, do they both use the same operating system? Are you sure you're providing the same input to both?
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips.



Reply With Quote
  #3  
Old October 24th, 2005, 06:45 AM
MichaelSoft MichaelSoft is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Location: The Netherlands
Posts: 121 MichaelSoft User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 20 sec
Reputation Power: 4
This source looks familiar to me...

The problem then was in the scanf, and it still is in this source.
Using scanf to get user input and parse it, is not the best way to go. When the parsing fails halfway a line (because the user typed a comma in stead of a space as delimiter), the next loop will continue from that point.

It is better to read the whole line in a buffer with gets().
Then parse it with sscanf, which will return the number of parsed variables.
Something like this:
Code:
char cBuf[81];

while(1)
{
  gets(cBuf, 80);
  if(sscanf(scanf(cBuf, "%d %lf %d", &prod_num, &base_cost, &number) != 3)
  {
    break;
  }
  // following the rest of the code...

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Please find an error in my noob program


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT