| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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. ![]() |
|
#3
|
|||
|
|||
|
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...
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Please find an error in my noob program |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|