954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Currency Converer (Need help with this)

/*Help me finished this code.I trying to make a program that will ask 2 choices. 1 is for converting dollars to peso and 2 is for converiting peso to dollars. Im trying my best but i dont seem to get the right code for it. Im just a beginner in C Language. Its kinda confusing using if statements i dont even know how to make a condition for it. I know there a lot of talented programmers and coders here i kinda nid ur help.*/

[TEX]#include

int main ()
{
float dollar,peso,dollar2,peso2,dtotal,ptotal,;
int choice;
printf("Enter 1 if you want to convert dollars to peso");
printf("\nEnter 2 if you want to convert peso to dollars\n");
printf("Enter your choice: ");
scanf("%d",&choice);
getchar();
if (choice == 1)
{
printf("Enter the amount of dollar(s): ");
scanf("%.2f" ,&dollar);
getchar();

printf("%.2f dollar(s) is equivalent to %.2f peso(s)",dollar,dollar*(1/48));
}

getchar();
if (choice != 1)
{
printf("Enter the amount of peso(s): ");
scanf("f",&peso2);

dtotal=peso2*48;
printf("%f peso(s) is equivalent to %f dollar(s)",peso2, dtotal);
}
getchar();
return 0;

}[/TEX]

DoEds
Junior Poster in Training
63 posts since Jun 2009
Reputation Points: 6
Solved Threads: 0
 

It's hard to help you fix your code if you don't show it. ;)

Tom Gunn
Master Poster
733 posts since Jun 2009
Reputation Points: 1,446
Solved Threads: 135
 

Here's the english version:

If input is peso to dollar
  Multiply input by 0.08775 (canadian dollars) (use double datatype)
  print product to the screen
if input is dollar to peso
  Divide input by 0.08775 (canadian to peso)
  print result to the screen

Just change that decimal to 0.076208 for us dollars. Post your code if you need help with syntax. Remeber to put your code in "[ CODE ] and [ /CODE ]" without the spaces.

Hiroshe
Posting Whiz in Training
256 posts since Jun 2008
Reputation Points: 431
Solved Threads: 17
 

i dont know what you did, but your code is not visible.

so, please use code tags.

[code=c]
put your code here
[/code]

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

>>"[ CODE ] and [ /CODE ]" without the spaces.
Next time you can use the noparse tag to tell the exact usage.

[noparse]

bbTags are not parse with this tag. Even if you type [code][/code] it will appear as it is. This example was created using two noparse tags.

[/noparse]

siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
 
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

Wait, what?? If you hover your mouse over the red box with text you see the code. That sure is weird!

Hiroshe
Posting Whiz in Training
256 posts since Jun 2008
Reputation Points: 431
Solved Threads: 17
 

>Wait, what?? If you hover your mouse over the red box with text you see the code. That sure is weird!

That's because he has used TEX-tags to post his code, here's the code he was actually trying to post:

#include <stdio.h> 

int main ()
{
  float dollar, peso, dollar2, peso2, dtotal, ptotal;
  int choice;
  
  printf("Enter 1 if you want to convert dollars to peso");
  printf("\nEnter 2 if you want to convert peso to dollars\n");
  printf("Enter your choice: ");
  
  scanf("%d", &choice);
  getchar();
  
  if (choice == 1)
  {
    printf("Enter the amount of dollar(s): ");
    scanf("%.2f", &dollar);
    getchar();
    
    printf("%.2f dollar(s) is equivalent to %.2f peso(s)", dollar, dollar * ( 1 / 48 ));
  }

  getchar();
  
  if (choice != 1) 
  {
    printf("Enter the amount of peso(s): ");
    scanf("f", &peso2);
    
    dtotal = peso2 * 48;
    printf("%f peso(s) is equivalent to %f dollar(s)", peso2, dtotal);
  }
  getchar();
  return 0;
}

(I intended the code a bit as well :P)

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

DoEds, here's a fixed version of your code.

#include <stdio.h> 

int main ()
{
  float dollar, peso, dtotal, ptotal;
  int choice;
  
  printf("Enter 1 if you want to convert dollars to peso\n");
  printf("Enter 2 if you want to convert peso to dollars\n");
  printf("Enter your choice: ");
  
  scanf("%d", &choice);
  
  if (choice == 1)
  {
    printf("Enter the amount of dollar(s): ");
    scanf("%f", &dollar);
    getchar();
    
    ptotal = dollar * 0.07593;
    printf("%f dollar(s) is equivalent to %f peso(s)\n", dollar, ptotal);
  }
  else if (choice != 1) 
  {
    printf("Enter the amount of peso(s): ");
    scanf("%f", &peso);
    
    dtotal = peso * 13.170025;
    printf("%f peso(s) is equivalent to %f dollar(s)\n", peso, dtotal);
  }

  printf("Press any key to continue...");
  getchar();
  return 0;
}

Make sure you don't declare extra variables, and make sure your code is consistant (you had one answer in a variable, and one on the printf line). Also there is no need for the %.2f thing, %f will do.

Hiroshe
Posting Whiz in Training
256 posts since Jun 2008
Reputation Points: 431
Solved Threads: 17
 

A few more improvements to the fixed code.

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

/* constants should be centralized */
#define PESO 13.170025
#define USD  0.07593

void clear(FILE *strm)
{
    int c;

    do c = getchar(); while (c != '\n' && c != EOF);
}

int main()
{
    /* double is usually a better choice than float */
    double amount;
    int choice;

    /* long strings can be broken down to avoid multiple printfs */
    /* if you don't print '\n', flush the stream */
    printf("1) USD to PESO\n"
           "2) PESO to USD\n"
           "> ");
    fflush(stdout);

    /* check input for failure */
    if (scanf("%d", &choice) != 1)
    {
        fputs("Bad input\n", stderr);
        return EXIT_FAILURE;
    }

    /* consolidate code to avoid redundancy */
    /* if you don't print '\n', flush the stream */
    printf("Enter the amount: ");
    fflush(stdout);

    /* check input for failure */
    if (scanf("%lf", &amount) != 1)
    {
        fputs("Bad input\n", stderr);
        return EXIT_FAILURE;
    }

    if (choice == 1)
    {
        printf("%gUSD == %gPESO\n", amount, amount * PESO);
    }
    else
    {
        printf("%gPESO == %gUSD\n", amount, amount * USD);
    }

    clear(stdin);
    fputs("Press <Enter> to continue...", stderr);
    getchar();

    return EXIT_SUCCESS;
}
Also there is no need for the %.2f thing, %f will do.


The %.2f thing won't work for scanf, it's only for formatting output in printf. For printf, currency converters usually stop at the 0s for precision, so %g is a better choice than %f. %f will print the maximum precision all of the time, even if it's all 0s.

Tom Gunn
Master Poster
733 posts since Jun 2009
Reputation Points: 1,446
Solved Threads: 135
 

I see the OP got their wish by successive approximation without having to put in any more effort.

Congrats to all :icon_rolleyes:

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

I see the OP got their wish by successive approximation without having to put in any more effort.

Congrats to all :icon_rolleyes:


If the code is already there, you might as well help make it better and hope the guy learns something instead of sitting on your #### waiting for him to fail at life.

Tom Gunn
Master Poster
733 posts since Jun 2009
Reputation Points: 1,446
Solved Threads: 135
 

>If the code is already there, you might as well help make it better and hope the
>guy learns something instead of sitting on your **** waiting for him to fail at life.

Why not nicely pack in a parcel with automatic installers and send him on his postal address with a Project website built?
;)
Not blaming you actually, but it was the post before yours which was the culprit.

siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
 

No, you blurted it out in response to Hiroshe's first suggestion without waiting for the OP to ask more questions.

If they're still confused, then your "advanced" answer is high and dry in the land of "huh?, wtf"
But hey, it's your time not mine - so that works for me.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 
No, you blurted it out in response to Hiroshe's first suggestion without waiting for the OP to ask more questions.


My post was for both of them, and anyone else reading the thread. I've learned from people who went the extra mile before and now I'm sharing the wealth.Why not nicely pack in a parcel with automatic installers and send him on his postal address with a Project website built?If they're still confused, then your "advanced" answer is high and dry in the land of "huh?, wtf"
And he can ask questions. That's how it works in most places. See something you don't understand, ask about it, get an answer, and learn. But here it's all about never giving away 'trade secrets' and assuming that everyone who isn't a bitter old guru is a thieving plagiarist scumbag. :icon_rolleyes:

Tom Gunn
Master Poster
733 posts since Jun 2009
Reputation Points: 1,446
Solved Threads: 135
 
If the code is already there, you might as well help make it better and hope the guy learns something instead of sitting on your **** waiting for him to fail at life.

or, conversely:

you post your code there, and wait for someone to make it better and hope some guy doesn't expect you to actually learn something, and you can just sit on your ass waiting for handouts all your lifeAnd he can ask questions. That's how it works in most places. See something you don't understand, ask about it, get an answer, and learn. O RLY?

and where are these "most places" of which you speak? The ones where people ask questions to get answers? Because it sure isn't here. DoEds
Newbie Poster
Posts: 1


.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

Enough, people. The facts are
1) fixed code should not have been posted by anyone other than the OP
2) fixed code should not have been improved by anyone unless the OP posted correct code in the first place.
3) People who expect the world to give them a free ride deserve the right to fail, and do not deserve the right to pass.

Now stop the flaming and get back on track.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

Woah.. Sorry guy's, didn't mean to start a war :(

Hiroshe
Posting Whiz in Training
256 posts since Jun 2008
Reputation Points: 431
Solved Threads: 17
 

Oh man i got it working thanks a lot...
Sorry for very late reply i was so busy at the moment...
I can only surf to internet during night time...

This so cool...It's my first time to join programming community (well, daniweb is the first one)...And i got lot of replies...
I'm gonna love this site...

I really had a hard time in programming 'coz our instructor was giving first an activity before he explains everything...

That's really kinda odd >,

DoEds
Junior Poster in Training
63 posts since Jun 2009
Reputation Points: 6
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You