ZeeK ZeeK - 4 months ago 15
C Question

c2059 error in do while loop C, what's wrong?

My code:

#include "cs50.h"
#include <stdio.h>
#include <math.h>

int main (void)
{
do
{
printf("How much money do I owe you ?\n");
float change = GetFloat(); //gets a floating point value from the user
if(change <0.01 )
{
printf("Try again you big dummy\n");
}
else
{
printf("Capitalism Ho!\n");
}
}
while (float change < 0.00); //this is line 20
}


in compiler:

greedo.c (20) error 2059:syntax error:"type"

This is part of cs50's problem set 1

Answer

1) You need to eliminate "float change" in your while() expression

2) You should move the declaration for "float change" to the top

#include "cs50.h"
#include <stdio.h>
#include <math.h>

int main (void)
{
    float change;
    do
    {
        printf("How much money do I owe you ?\n");
        change = GetFloat(); //gets a floating point value from the user
        if(change <0.01 )
        {
            printf("Try again you big dummy\n");
        }
        else
        {
            printf("Capitalism Ho!\n");
        }
    }
    while (change < 0.00); //this is line 20
}

3) I would also suggest defining a "min value", then check against it:

#include "cs50.h"
#include <stdio.h>
#include <math.h>

#define MIN_VALUE 0.01

int main (void)
{
    float change;
    do
    {
        printf("How much money do I owe you ?\n");
        change = GetFloat(); //gets a floating point value from the user
        if(change >= MIN_VALUE)
        {
            printf("Capitalism Ho!\n");
            break;
        }
    }
    while (change < MIN_VALUE); //this is line 20
}