-2

When I run this code, and I press 1 at the end of it, instead of printing out message one, it just goes straight to scanf, and only after I enter in my input does it print message one.

#include <stdio.h>

void main() {
    int year;
    int decision;
    printf( "\nThis program tells the user what the GDP per capita of post-independence South Sudan was in a given year. The availability of a figure for a certain year is subject to the limitations of the data that was provided by the World Bank. " );
point_one :
    printf( "Enter a year below to get a figure for that year.\n\n" ); // message one
    scanf( "%d" , &year );
    if(2011 > year || year > 2015 )
        printf( "\nThe figure for that year is unavailable.\n" );
    else if( year == 2011 )
        printf( "\n1,516.40 USD\n" );
    else if( year == 2012 )
        printf( "\n1,179.40 USD\n" );
    else if( year == 2013 )
        printf( "\n1,779.47 USD\n" );
    else if( year == 2014 )
        printf( "\n1,322.82 USD\n" );
    else if( year == 2015 )
        printf( "\n1,119.65 USD\n" );
    printf( "\nDo you want to try again or leave?\n\n" );
    scanf ( "%d\n\n" , &decision );
    if ( decision == 1 )
        goto point_one;
    else return;
}
Oka
  • 23,367
  • 6
  • 42
  • 53
  • 1
    Please don't use `goto` and labels instead of loops. – Some programmer dude Aug 16 '22 at 17:17
  • 2
    `scanf` returns a valuable indicator of the number of items scanned. It's worth storing and checking. – h0r53 Aug 16 '22 at 17:17
  • 1
    As a hint about your problem: Any white-space character in a `scanf` format string will lead to `scanf` to read and ignore any kind of white-space. But take some time to think about how it will know that it has reached the end of the white-space? – Some programmer dude Aug 16 '22 at 17:19
  • Please see [What is the effect of trailing white space in a scanf() format string?](https://stackoverflow.com/questions/19499060/what-is-the-effect-of-trailing-white-space-in-a-scanf-format-string) The trailing whitespace in the `scanf()` format string tells it to filter *any amount* of whitespace, but the function won't know how much that is until you actually enter a non-whitespace character. Every character in the format string has a purpose. – Weather Vane Aug 16 '22 at 17:24
  • Guys, thank you so much! I removed the \n's at the function parameter of the last scanf. Then, I modified the point_one definition so that it'll be like this : "point_one : printf("\n");". It worked! Also, thanks to Oka for making modifying my code so that it's more readable. – Peer Kristijan Aug 16 '22 at 17:32

1 Answers1

3
  1. void main is invalid.
  2. Do not use goto. There are some circumstances when using goto is reasonable but IMO beginners should not use it at all.
  3. Check return values of scanf.
  4. Use correct formats ("\n\n") was nor correct
  5. In this case I would use swich instead of ifs
int main(void) {
    int year;
    int decision;
    printf( "\nThis program tells the user what the GDP per capita of post-independence South Sudan was in a given year. The availability of a figure for a certain year is subject to the limitations of the data that was provided by the World Bank. " );
    do{
        printf( "Enter a year below to get a figure for that year.\n\n" ); // message one
        if(scanf( "%d" , &year ) != 1) {/* error handling */ exit(2);}
        switch(year)
        {
        case 2011:
            printf( "\n1,516.40 USD\n" );
            break;
        case 2012:
            printf( "\n1,179.40 USD\n" );
            break;
        case 2013:
            printf( "\n1,779.47 USD\n" );
            break;
        case 2014:
            printf( "\n1,322.82 USD\n" );
            break;
        case 2015:
            printf( "\n1,119.65 USD\n" );
            break;
        default:
            printf( "\nThe figure for that year is unavailable.\n" );
            break;
        }
        printf( "\nDo you want to try again or leave?\n\n" );
        if(scanf ( "%d" , &decision ) != 1) {/* error handling */ exit(3);}
    }while(decision == 1);
}

https://godbolt.org/z/793PdYdrh

But instead of switch and ifs you can use table and make your code much better (for example it will be very easy to add more years).

int main(void) {
    int year;
    int decision;

    unsigned data[] = {151640, 117940, 177947, 132282, 111965};
    printf( "\nThis program tells the user what the GDP per capita of post-independence South Sudan was in a given year. The availability of a figure for a certain year is subject to the limitations of the data that was provided by the World Bank. " );
    do{
        printf( "Enter a year below to get a figure for that year.\n\n" ); // message one
        if(scanf( "%d" , &year ) != 1) {/* error handling */ exit(2);}
        if(year < 2011 || year > 2015) printf( "\nThe figure for that year is unavailable.\n" );
        else printf( "\n%d.%d USD\n", data[year - 2011] / 100, data[year - 2011] % 100);

        printf( "\nDo you want to try again or leave?\n\n" );
        if(scanf ( "%d" , &decision ) != 1) {/* error handling */ exit(3);}
    }while(decision == 1);
}
0___________
  • 60,014
  • 4
  • 34
  • 74