Converting C++ to C

Trying to convert a program from C++ to C

Page 1 of 1

12 Replies - 2519 Views - Last Post: 31 August 2006 - 12:30 PM Rate Topic: -----

#1 promiscuoustx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 30-August 06

Converting C++ to C

Posted 30 August 2006 - 02:26 PM

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++.

#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!!
#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: :crazy:

EDIT: Added code tags. ~Videege

Is This A Good Question/Topic? 0
  • +

Replies To: Converting C++ to C

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Converting C++ to C

Posted 30 August 2006 - 04:14 PM

Can you explain where you're stuck?
Was This Post Helpful? 0
  • +
  • -

#3 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Converting C++ to C

Posted 30 August 2006 - 06:16 PM

:)

Quote

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:

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 August 2006 - 06:29 PM

Was This Post Helpful? 0
  • +
  • -

#4 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Converting C++ to C

Posted 31 August 2006 - 07:01 AM

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.
Was This Post Helpful? 0
  • +
  • -

#5 promiscuoustx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 30-August 06

Re: Converting C++ to C

Posted 31 August 2006 - 08:55 AM

View PostAmadeus, on 30 Aug, 2006 - 04:14 PM, said:

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 August 2006 - 09:07 AM

Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Converting C++ to C

Posted 31 August 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?
Was This Post Helpful? 0
  • +
  • -

#7 promiscuoustx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 30-August 06

Re: Converting C++ to C

Posted 31 August 2006 - 09:31 AM

View PostAmadeus, on 31 Aug, 2006 - 09:02 AM, said:

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:
Was This Post Helpful? 0
  • +
  • -

#8 promiscuoustx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 30-August 06

Re: Converting C++ to C

Posted 31 August 2006 - 11:52 AM

View Postpromiscuoustx, on 31 Aug, 2006 - 09:31 AM, said:

View PostAmadeus, on 31 Aug, 2006 - 09:02 AM, said:

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:


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

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


Was This Post Helpful? 0
  • +
  • -

#9 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Converting C++ to C

Posted 31 August 2006 - 12:00 PM

Works just fine in Dev-C++. I am getting the correct output.
Was This Post Helpful? 0
  • +
  • -

#10 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Converting C++ to C

Posted 31 August 2006 - 12:00 PM

Take out the getch()...it is waiting for user input. Take it out, and it runs fine.
Was This Post Helpful? 0
  • +
  • -

#11 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Converting C++ to C

Posted 31 August 2006 - 12:02 PM

Good point Amadeus, I assumed that was intentional and just hit the return key myself.
Was This Post Helpful? 0
  • +
  • -

#12 promiscuoustx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 30-August 06

Re: Converting C++ to C

Posted 31 August 2006 - 12:08 PM

View PostAmadeus, on 31 Aug, 2006 - 12:00 PM, said:

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


THANK YOU!!! YOU ARE A LIFE SAVER!!!
Was This Post Helpful? 0
  • +
  • -

#13 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1875
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Re: Converting C++ to C

Posted 31 August 2006 - 12:30 PM

Thanks for helping out guys!

promiscuoustx, glad you were able to get your problem solved, please don't hesitate to come back to dream.in.code in the future if you need programming help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1