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

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

}

Recommended Answers

All 21 Replies

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

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.

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

so, please use code tags.

"[ CODE ] and [ /CODE ]" without the spaces.

Next time you can use the noparse tag to tell the exact usage.

[noparse][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][/noparse]

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

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

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.

commented: We don't fix people's problems for them. We HELP them fix it themselves -4

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.

commented: We don't fix people's problems for them. We HELP them fix it themselves -4

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

Congrats to all :icon_rolleyes:

commented: ROFL :P +11

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 arse waiting for him to fail at life.

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

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.

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:

commented: taking back some points for being completely obtuse. -2

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 life

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.

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


.

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.

commented: Finally! +11

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

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

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

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.

Ah so that's why %.2f wont work for scanf...Thanks...
Anyway this a good example but its gonna make my nose bleed...
I'll try to understand this one...

Ah so that's why %.2f wont work for scanf...

I had a lot of trouble with scanf and printf before I found out that they're not mirror images of each other. scanf doesn't use the same format string rules as printf because they have different needs and restrictions.

Anyway this a good example but its gonna make my nose bleed...
I'll try to understand this one...

Don't forget to ask questions if you need help understanding it. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.