Petefic
Petefic

Reputation: 677

Having trouble with do while loops in C

I am a beginner at C and I am writing a basic program that converts dollars to euros. For some reason the program is not running this line: "scanf("%c", &yn);". If I remove the do while loop the program works fine.

Instead of stopping and waiting for the user to enter 'y' or 'n' the loop restarts and asks for the usd amount again.

#include<stdio.h>

main()

{
    float usd = 0.00;
    float euro = 0.00;
    char yn;
    const float conversion = 0.75;

    do {
        /*get amount to convert*/
        printf("Please enter the amount of USD you want to convert to Euros: ");
        scanf("%f", &usd);

        /*convert amount*/
        euro = (usd * conversion);

        /*output results and ask to continue*/
        printf("\n%.2f USD equals %.2f Euros. Do you want to convert another amount? (y/n): ", usd, euro);
        scanf("%c", &yn);
        printf("\n");


        /*if yes, get new amount to convert. if no, program ends*/
    } while (yn = 'y');

    return 0;
}

Thanks in advance.

Upvotes: 1

Views: 1330

Answers (8)

Uffe
Uffe

Reputation: 10504

As others have pointed out, the problem is that you've accidentally written an assignment statement instead of the equality test. We've all done that.

But on a side note, as a junior C programmer you may come across advice telling you to switch the order of the operands in an equality test (which will cause a compile error if you make that same mistake), or #define EQUALS (or, on a related topic, TRUE and FALSE). This is very bad advice.

Reversing the order of the operands makes the code harder to read, because you don't actually want to compare the constant to the variable, you want to compare the variable to the constant. (Also, it won't help at all if you are comparing two variables.) Using #defines also clutters up the code with irrelevant nonsense. There is no boolean datatype in C, and C does have similarly named assignment and equality operators.

The better advice is to learn the language, not try to make it look like something it's not.

Upvotes: -1

stdcall
stdcall

Reputation: 28870

You've proboly noticed, and understood the silly bug, however, this is a very common bug, and it's really hard to trace, because the assignment operation always return the right value of the assignment. There are two ways to minimize the chance for such a bug. the first is to use #define equals =. The other one is two always replace the order of comparisment. so yn == 'y' will become 'y' == yn. In this way, if by mistake replace the == with = you'll get compilation error. I don't know a single person who haven't stumbbled upon this bug...

Upvotes: 0

Joe
Joe

Reputation: 57169

Your scanf is picking up the newline from reading the float previously. You need to account for the new line, scanf(" %c", &yn); should work.. Even once you enter a 'y' or a 'n' the evaluation will fail because you are assigning 'y' to yn which would always evaluate as true. change it to while( yn == 'y').

Upvotes: 4

Mahesh
Mahesh

Reputation: 34615

do {
  // ...
} while( yn == 'y'); // At this statement you are making an assignment,
                     // not comparison. Use the equal to operator.

Upvotes: 4

Sadique
Sadique

Reputation: 22813

Give a space in the second scanf before the %c like this scanf(" %c", &yn);.

Also go through this question, quite similar to your problem:

Noticing strange behavior with strings in c

Also as pointed out by others:

while (yn = 'y'); will be (yn == 'y')

Upvotes: 1

ville6000
ville6000

Reputation: 151

while (yn = 'y') should be while (yn == 'y')

Upvotes: 0

Jacob Relkin
Jacob Relkin

Reputation: 163228

You meant to do == in your while condition, not a single =, that will always evaluate truthfully.

Upvotes: 2

Graeme Perrow
Graeme Perrow

Reputation: 57238

You need to change yn = 'y' to yn == 'y'. In the first case, you're setting yn to 'y' while what you want to do is compare it to 'y'. The == operator is used for comparisons.

Upvotes: 2

Related Questions