Welcome to Dream.In.Code
Become a C++ Expert!

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!




Converting C++ to C

2 Pages V  1 2 >  
Reply to this topicStart new topic

Converting C++ to C, Trying to convert a program from C++ to C

promiscuoustx
30 Aug, 2006 - 01:26 PM
Post #1

New D.I.C Head
*

Joined: 30 Aug, 2006
Posts: 36


My Contributions
I wrote a program in C++, but I need to convert it to C and I am having a terrible time at this!! Can someone please help??? I am using Dev C++.

CODE
#include <iostream>
#include <stdlib.h>
#include <stdio.h>

int main()
{

using namespace std;

//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

}


Any help will be appreciated!! blink.gif crazy.gif

EDIT: Added code tags. ~Videege
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Converting C++ To C
30 Aug, 2006 - 03:14 PM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
Can you explain where you're stuck?
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Converting C++ To C
30 Aug, 2006 - 05:16 PM
Post #3

 
Group Icon

Joined: 3 Aug, 2006
Posts: 1,790



Thanked: 1 times
Dream Kudos: 755
My Contributions
smile.gif
QUOTE

CODE

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
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Converting C++ To C
31 Aug, 2006 - 06:01 AM
Post #4

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,935



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
QUOTE
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.
User is offlineProfile CardPM
+Quote Post

promiscuoustx
RE: Converting C++ To C
31 Aug, 2006 - 07:55 AM
Post #5

New D.I.C Head
*

Joined: 30 Aug, 2006
Posts: 36


My Contributions
QUOTE(Amadeus @ 30 Aug, 2006 - 04:14 PM) *

Can you explain where you're stuck?


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
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Converting C++ To C
31 Aug, 2006 - 08:02 AM
Post #6

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
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?
User is offlineProfile CardPM
+Quote Post

promiscuoustx
RE: Converting C++ To C
31 Aug, 2006 - 08:31 AM
Post #7

New D.I.C Head
*

Joined: 30 Aug, 2006
Posts: 36


My Contributions
QUOTE(Amadeus @ 31 Aug, 2006 - 09:02 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?


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!!! crazy.gif
User is offlineProfile CardPM
+Quote Post

promiscuoustx
RE: Converting C++ To C
31 Aug, 2006 - 10:52 AM
Post #8

New D.I.C Head
*

Joined: 30 Aug, 2006
Posts: 36


My Contributions
QUOTE(promiscuoustx @ 31 Aug, 2006 - 09:31 AM) *

QUOTE(Amadeus @ 31 Aug, 2006 - 09:02 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?


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!!! crazy.gif


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;
}

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Converting C++ To C
31 Aug, 2006 - 11:00 AM
Post #9

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 45 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Works just fine in Dev-C++. I am getting the correct output.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Converting C++ To C
31 Aug, 2006 - 11:00 AM
Post #10

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
Take out the getch()...it is waiting for user input. Take it out, and it runs fine.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Converting C++ To C
31 Aug, 2006 - 11:02 AM
Post #11

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 45 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Good point Amadeus, I assumed that was intentional and just hit the return key myself.
User is offlineProfile CardPM
+Quote Post

promiscuoustx
RE: Converting C++ To C
31 Aug, 2006 - 11:08 AM
Post #12

New D.I.C Head
*

Joined: 30 Aug, 2006
Posts: 36


My Contributions
QUOTE(Amadeus @ 31 Aug, 2006 - 12:00 PM) *

Take out the getch()...it is waiting for user input. Take it out, and it runs fine.


THANK YOU!!! YOU ARE A LIFE SAVER!!!
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 12/5/08 03:03AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month