Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix "return value ignored: 'scanf'" code C6031 in visual studio

Tags:

c

I am brand new to coding C (and coding in general) so I have been practicing with some random programs. This one is supposed to determine the cost of a transit ticket (Translink Vancouver prices) based on the user's age and the desired number of "zones" (how far they would like to go). I have compiled it successfully but for some reason which I can not figure out, the scanf functions are being ignored. How do I fix this? Please keep in mind I have only been coding for a few days. Thanks!

int main(void) {

int zones;
int age;
double price = 0.00;

printf("Welcome to TransLink cost calculator!\n\n");
printf("Please enter the desired number of zones (1, 2, or 3) you wish to travel: ");
scanf("%d", &zones);

if (zones < 1) {
    printf("Invalid entry\n");
    price = 0.00;
}

else if (zones > 3) {
    printf("Invalid entry\n");
    price = 0.00;
}

else if (zones == 1) {

    printf("Please enter your age: ");
    scanf("%d", &age);

    if (age < 0.00) {
        printf("Invalid Aage");
    }
    else if (age < 5) {
        price = 1.95;
    }
    else if (age >= 5) {
        price = 3.00;
    }
}

else if (zones == 2) {

    printf("Please enter your age: ");
    scanf("%d", &age);

    if (age < 0) {
        printf("Invalid Aage");
    }
    else if (age < 5) {
        price = 2.95;
    }
    else if (age >= 5) {
        price = 4.25;
    }
}

else if (zones == 3) {

    printf("Please enter your age: ");
    scanf("%d", &age);

    if (age < 0) {
        printf("Invalid Aage");
    }
    else if (age < 5) {
        price = 3.95;
    }
    else if (age >= 5) {
        price = 4.75;
    }
}

printf("The price of your ticket is: $%.2f + tax\n", price);

system("PAUSE");
return 0;
}
like image 502
Caiden Keller Avatar asked Jan 26 '23 19:01

Caiden Keller


2 Answers

From documentation of scanf() (e.g. https://en.cppreference.com/w/c/io/fscanf)

Return value
1-3) Number of receiving arguments successfully assigned (which may be zero in case a matching failure occurred before the first receiving argument was assigned), or EOF if input failure occurs before the first receiving argument was assigned.

You are ignoring that return value.

Replace

scanf("%d", &age);

by

int NofScannedArguments=0; /* Number of arguments which were
successfully filled by the most recent call to scanf() */

/* ...  do above once, at the start of your function */

NofScannedArguments= scanf("%d", &age);

/* check the return value to find out whether scanning was successful */
if(NofScannedArguments!=1) /* should be one number */
{
    exit(EXIT_FAILURE); /* failure, assumptions of program are not met */
}

... to find out whether scanning succeeded. Not doing so is a bad idea and worth the warning you got.

In case you want to handle failures more gracefully, e.g. prompt the user again,
use a loop and read http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html about the pitfalls you may encounter.
I am not saying you should NOT use scanf, the article explains a lot about using scanf, while trying to convince you not to.

like image 35
Yunnosch Avatar answered Jan 29 '23 21:01

Yunnosch


A bit too much here to put in a comment.

I use a version of Visual C but it never complains about the return value from scanf not being used. What it does is to complain that scanf is unsafe and deprecated, when it isn't.

MS thinks I should be using its own "safer" version scanf_s which is even tricker to use and IMO no safer at all – because it is not a like-for-like replacement but takes different arguments, and so it is easy to make mistakes in using it.

One consequent problem is the compiler issues a warning for every use of scanf (and some other functions) which obscures other warnings. I deal with it as advised by adding a #define before the first library header inclusion.

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

There are other matters which MS warns about too, and I actually place three #defines at the start of each file:

#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_DEPRECATE  
#define _CRT_NONSTDC_NO_DEPRECATE

#include <stdio.h>

And now the relevant warnings are easy to see.

like image 195
Weather Vane Avatar answered Jan 29 '23 19:01

Weather Vane