0

As I was trying to structure the user input section of both problems, I ran into some errors that I don't understand. For Mario, I separated the user input of the problem and int main(void) by placing int get_n(string prompt); above 'int main(void)' and then creating a separate function that would be called into int main(void) using the line of code int z = get_n("Height: ");. The function I created to get user input is given below,

int get_n(string prompt)
{
    int n;
    do
    {
        n = get_int("%s", prompt);
    }
    while (n<1 || n>8);
    return n;
}

This worked and I did not receive any errors! However, when I tried to do this same process within cash, I received the following error when using help50,

"Looks like you're redeclaring the function get_float, but with a different return type on line 18 of cash.c. The function get_float is already declared in the library cs50.h. Try renaming your function."

Why am I receiving an error in this case and not in Mario? Isn't get_n also a part of the CS50 library? I changed my code to include the user question within int main(void) and I did not receive any errors (code given below).

int main(void)
{
    float dollars;
    do
    {
        dollars = get_float("Change owed: ");
    }
    while (dollars<0);
    return dollars;

Why does this work and the other case does not?

Thanks for your help!

2 Answers 2

0

Isn't get_n also a part of the CS50 library?

There is not a function named get_n in the CS50 library. Perhaps you are thinking of get_int.

The cs50.h functions are:

get_char - prompts user for a line of text from stdin and returns the equivalent char
get_double - prompts user for a line of text from stdin and returns the equivalent double
get_float - prompts user for a line of text from stdin and returns the equivalent float
get_int - prompts user for a line of text from stdin and returns the equivalent int
get_long - prompts user for a line of text from stdin and returns the equivalent long
get_string - prompts user for a line of text from stdin and returns it as a string
0

In second case you probably named your function get_float name it something else and it will work without any problems. (and get_n isn't any function fron cs50 library)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .