0

I'm trying to create a simple converter that will ask a user to input data n times. The scanf() in the loop ideally should allow user to input a response in the terminal, press enter, then pass an output, then ask again....n times. But the program so far, just asks one time and never allows user to ask again.

There are many posts about scanf in loops I've seen, but I wasn't able to connect my issue with any of them.

I'm new to C, coming from Java.

#include <stdio.h>
double feet(double m);
double lbs(double g);
double f(double c);
//double askInput(int num);


int main()
{
    int num, i;
    i = 0;
    
    printf("how many?\n");
    scanf("%i\n", &num);
    
    for(i = 0; i < num; i++)
    {
    double input = 0.0;
    double output;
    char unit = NULL;
        printf("num to convert, units to convert?\n");
        
        //scanf("%lf %c\n", &input, unit);
        if(input == 0.0 && unit == NULL)
        {
            //input = askInput(num);
            scanf("%lf %c\n", &input, unit);
        }
        
        //meters to feet
        if(unit == 'm')
        {
            output = feet(input);
        }
        
    }

I've tried a number of things. I've tried using a while loop, I've tried putting scanf in a separate function and then also using if statements. I imagine I am not quite understanding how scanf works.

halfer
  • 19,824
  • 17
  • 99
  • 186
Katie Melosto
  • 1,047
  • 2
  • 14
  • 35
  • 3
    Note that `NULL` is technically a pointer, so the assignment `char unit = NULL;` is bad practice. What you want there is `char unit = '\0';` since `'\0'` is the NUL character. And the `if` statement surrounding the `scanf` is not needed. Instead, you need an `if` statement to verify that `scanf` returned 2, e.g. `if (scanf("%lf %c", &input, &unit) != 2) break;` – user3386109 Dec 25 '19 at 17:00

2 Answers2

4

You have a couple issues here.

  1. You're assigning NULL (of type void*) into a char as well as comparing these values later. Don't do this. If you want to use some sort of canary value you could instead use the character '\0' (or any other non-readily enter-able character).
  2. You've given scanf a parameter that it expects to be a char* as a char. In order for the line scanf("%lf %c\n", &input, unit); to work as intended, you should have an ampersand in front of unit in order to pass a pointer to the local variable unit.
  3. Giving scanf trailing whitespace requires it to read all subsequent whitespace until it can determine a block of whitespace has ended (see man 3 scanf for some more info, but note that any whitespace character in the format string is treated equivalently). In this instance having a newline on the end of your scanf calls will require them to read some amount of whitespace and then a non-whitespace character (see also the accepted answer here: white space in format string of scanf()). Just leave off the \n.
  4. Some of your braces (namely the block for the function main) aren't closed. I'm assuming this is just a function of copying-and-pasting into SO though.

Most of this could be avoided with a stricter compilation command. If you're using gcc with the C99 standard you could try gcc -Wall -Werror -Wextra -Wshadow -pedantic -std=c99 source_file.c.

kopecs
  • 1,545
  • 10
  • 20
2

First of all, I recommend using the _s functions instead of the regulars (for example printf_s and not printf). Second, I wrote down a version of the code that is working. I noticed that whenever I added some text to scanf_s (for example a \n) the program didn't stop but when I printed it with printf the program stopped.

FYI: For initialization, you need to put a number that the user won't enter - If the user doesn't put a value num won't store 0 but -9.2559631349317831e+61. And you should use || instead of && because you want that num and unit will store a value! :)

Here's my code:

#include <stdio.h>
#include <string.h>

int main()
{
    int amount;
    printf_s("How many numbers would you like to recive?\n");
    scanf_s("%i", &amount);

    for (int i = 0; i < amount; ++i)
    {

        // Initialization of all variables.
        double convertedNum;
        double rawNum;
        char unit[3] = "";

        // Getting the variables.
        printf_s("Please enter the number that you\'d like to convert and the unit to 
        convert to.\n");
        printf_s("Number: ");
        scanf_s("%lf", &rawNum);
        printf_s("Units (m/ft): ");
        scanf_s("%s", &unit, 3);

        // To make sure that we're getting right input & the wanted amount of 
        // numbers.

        if (rawNum == -9.2559631349317831e+61 || unit == "")
        {
            printf_s("Please enter a number and a unit!");
            --i;
        }

        else if (strcmp(unit, "m") == 0)
        {
            convertedNum = rawNum * 3.2808399;
            printf_s("\n\nAfter convention, the number is %f\n", convertedNum);
        }

        else if (strcmp(unit, "ft") == 0)
        {
            convertedNum = rawNum / 3.2808399;
            printf_s("\n\nAfter convention, the number is %f\n", convertedNum);
        }
    }

    return 0;
}