Join 137,398 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,112 people online right now. Registration is fast and FREE... Join Now!
//Some food items and all non-food items are subject to a statewide sales tax and local //district taxes. Due to differences in district taxes, each store uses a different tax rate
char str_DelMar[] = "Del Mar", str_Encinitas[] = "Encinitas", str_LaJolla[] = "La Jolla"; //The above is the three districts double tax_DelMar = 7.25, tax_Encinitas = 7.5, tax_LaJolla = 7.75, purchase = 125.00; //The above is the distinct district taxes for the three districts char s; char mychar; mychar = getchar();
//This is to ensure the user of this program knows what the purpose of this program is cout << "This is a program to calculate the taxes on articles " << endl; cout << "purchased at the price of $125, depending on the district. " << endl;
//The user needs to choose which option they want cout << "For the district Del Mar, please press 1. " << endl; cout << "For the district Encinitas, please press 2. " << endl; cout << "For the district La Jolla, please press 3. " << endl; //Added the option to exit for users cout << "To exit, please press 0. " << endl;
for(;(s = getchar()) != '0';
//The following if / else statements calcualte the actual tax //amount according to the district the user chooses )if(s == '1') cout << "The tax for the purchase at the price of $" << purchase << " in district " << str_DelMar << " is $" << purchase * tax_DelMar / 100 << endl; else
if(s == '2') cout << "The tax for the purchase at the price of $" << purchase << " in district " << str_Encinitas << " is $" << purchase * tax_Encinitas / 100 << endl; else
if(s == '3') cout << "The tax for the purchase at the price of $" << purchase << " in district " << str_LaJolla << " is $" << purchase * tax_LaJolla / 100 << endl;
return 0; }
I have the start for converting it, but I am stuck!!
CODE
#include <stdlib.h> #include <stdio.h>
main() {
/*Some food items and all non-food items are subject to a statewide sales tax and local*/ /*district taxes. Due to differences in district taxes, each store uses a different tax rate*/
int iResponse = 0; char str_DelMar[] = "Del Mar", str_Encinitas[] = "Encinitas", str_LaJolla[] = "La Jolla"; /*The above is the three districts*/ double tax_DelMar = 7.25, tax_Encinitas = 7.5, tax_LaJolla = 7.75, purchase = 125.00; /*The above is the distinct district taxes for the three districts*/ char s; char mychar; mychar = getchar();
/*This is to ensure the user of this program knows what the purpose of this program is*/ printf("\nThis is a program to calculate the taxes on articles\n"); printf("\npurchased at the price of $125, depending on the district.\n");
/*The user needs to choose which option they want*/ printf("\nFor the district Del Mar, please press 1.\n"); printf("\nFor the district Encinitas, please press 2.\n"); printf("\nFor the district La Jolla, please press 3.\n"); /*Added the option to exit for users*/ printf("\nTo exit, please press 0.\n"); scanf(“%d”, &iResponse);
getch(); if (iResponse == 1) printf("\nThe tax for the purchase at the price of $" << purchase << " in district " << str_DelMar << " is $" << purchase * tax_DelMar / 100 <<; else
printf("\nThe tax for the purchase at the price of $" << purchase << " in district " << str_DelMar << " is $" << purchase * tax_DelMar / 100 <<;
I guess this is the problem: The insertion operator ('<<') is used for cout not printf. In printf u use format specifiers for printing variables so the above line should become:
CODE
printf("\nThe tax for the purchase at the price of $%lf in districy %s is $ %lf ",purchase,str_DelMar, purchase * tax_DelMar / 100 );
This post has been edited by Louisda16th: 30 Aug, 2006 - 05:29 PM
I guess this is the problem: The insertion operator ('<<') is used for cout not printf.
Well, not just for cout, this operator can be overloaded by any class. So it can be used by any class in C++ provided it overloads the operator just like how the iostream class does.
I am really stuck on my if...else statements in trying to convert them from C++ to C...
I think I may have them, but it says that there is an error now with my scanf statement to get the user input??? It says "Syntax error before '%' token??? Any ideas??
Thanks!!
This post has been edited by promiscuoustx: 31 Aug, 2006 - 08:07 AM
Is it the syntax of the if/else (it is the same for both languages) or the code being executed? Posts above note the correct use of the printf() function. Also, you seem to have switched from using char s as a switch to iResponse. Was this intended?
Is it the syntax of the if/else (it is the same for both languages) or the code being executed? Posts above note the correct use of the printf() function. Also, you seem to have switched from using char s as a switch to iResponse. Was this intended?
I thought I had to switch the char s to i response??? So I guess you could say that it was intended. I am really confused now more than anything because my program ran great using C++, and now I can not get it to run!! AAGGHH!!!
Is it the syntax of the if/else (it is the same for both languages) or the code being executed? Posts above note the correct use of the printf() function. Also, you seem to have switched from using char s as a switch to iResponse. Was this intended?
I thought I had to switch the char s to i response??? So I guess you could say that it was intended. I am really confused now more than anything because my program ran great using C++, and now I can not get it to run!! AAGGHH!!!
Okay, this is what I have come up with so far, but it is not printing the sales tax after the user inputs their number. I know I am almost there!!
CODE
#include <stdlib.h> #include <stdio.h>
main() {
/*Some food items and all non-food items are subject to a statewide sales tax and local*/ /*district taxes. Due to differences in district taxes, each store uses a different tax rate*/
int iResponse = 0; char str_DelMar[] = "Del Mar", str_Encinitas[] = "Encinitas", str_LaJolla[] = "La Jolla"; /*The above is the three districts*/ double tax_DelMar = 7.25, tax_Encinitas = 7.5, tax_LaJolla = 7.75, purchase = 125.00; /*The above is the distinct district taxes for the three districts*/ char s; char mychar; mychar = getchar();
/*This is to ensure the user of this program knows what the purpose of this program is*/ printf("\nThis is a program to calculate the taxes on articles\n"); printf("\npurchased at the price of $125, depending on the district.\n");
/*The user needs to choose which option they want*/ printf("\nFor the district Del Mar, please press 1.\n"); printf("\nFor the district Encinitas, please press 2.\n"); printf("\nFor the district La Jolla, please press 3.\n"); /*Added the option to exit for users*/ printf("\nTo exit, please press 0.\n"); scanf ("%d", &iResponse); getch();
if (iResponse == 1){ printf("\nThe tax for the purchase at the price of $%.2f in district %s is $ %.2f\n ",purchase,str_DelMar, purchase * tax_DelMar / 100 ); } else {
if (iResponse == 2) printf("\nThe tax for the purchase at the price of $%.2f in district %s is $ %.2f ",purchase,str_Encinitas, purchase * tax_Encinitas / 100 ); else
if (iResponse == 3) printf("\nThe tax for the purchase at the price of $%.2f in district %s is $ %.2f ",purchase,str_LaJolla, purchase * tax_LaJolla / 100 ); } return 0; }