-1

In this code when I change the position of the variable int year inside the main function the ouutput printed line replace every year with 1792 despite that I don't have 1792 at all

Code with year as local variable:

#include <stdio.h>

int main() {
    int year = 0;
    int rp1970 = 73, rp1980 = 66, rp1990 = 49, rp2000 = 38, rp2010 = 29;
    int up1970 = 27, up1980 = 34, up1990 = 51, up2000 = 62, up2010 = 71;
    float t1970=10.44, t1980=13.14, t1990=17.57, t2000=23.30, t2010=28.60;
    float rn1970=7.62, rn1980=8.67, rn1990=8.67, rn2000=8.85, rn2010=8.29;
    float un1970=2.82, un1980=4.47, un1990=8.90, un2000=14.45, un2010=20.31;

    printf("                                 welcome to population data base for years, 1970,1980,1990,2000,2010 ");
    char specific, choice;
    do {
        printf("\n\nenter a year(choose between 1970,1980,1990,2000,2010):");
        scanf("%d",&year);
        while (year!=1970 && year!=1980 && year!=1990 && year!=2000 && year!=2010) {
            printf("\nwrong input\n enter a year(choose between 1970,1980,1990,2000,2010):");
            scanf("%d", &year);
        }
        if (year == 1970) {
            printf("\npress t for total population r for rural population u for urban population: ");
            scanf("%s", &specific);
            while (specific!='t' && specific!='T'&& specific!='r'&& specific!='R'&& specific!='u'&& specific!='U') {
                printf("\nwrong input\n press t for total population r for rural population u for urban population: ");
                scanf("%s", &specific);
            }
            if (specific == 't' || specific == 'T') {
                printf("\ntotal population of year %d is %.2fm ",year,t1970);
            }
            if (specific == 'r' || specific == 'R') {
                printf("\nrural population of year %d is %.2fm and it is %d%% of total population", year, rn1970, rp1970);
            }
            if (specific == 'u' || specific == 'U') {
                printf("\nurban population of year %d is %.2fm and it is %d%% of total population", year, un1970, up1970);
            }
        }
        else if (year == 1980) {
            printf("\npress t for total population r for rural population u for urban population: ");
            scanf("%s", &specific);
            while (specific!='t' && specific!='T'&& specific!='r'&& specific!='R'&& specific!='u'&& specific!='U') {
                printf("\nwrong input\n press t for total population r for rural population u for urban population: ");
                scanf("%s",&specific);
            }
            if (specific=='t'||specific=='T') {
                printf("\ntotal population of year %d is %.2fm ",year,t1980);
            }
            if (specific=='r'||specific=='R') {
                printf("\nrural population of year %d is %.2fm and it is %d%% of total population",year,rn1980,rp1980);
            }
            if (specific=='u'||specific=='U') {
                printf("\nurban population of year %d is %.2fm and it is %d%% of total population",year,un1980,up1980);
            }
        }
        else if (year==1990) {
            printf("\npress t for total population r for rural population u for urban population: ");
            scanf("%s", &specific);
            while (specific!='t' && specific!='T'&& specific!='r'&& specific!='R'&& specific!='u'&& specific!='U') {
                printf("\nwrong input\n press t for total population r for rural population u for urban population: ");
                scanf("%s", &specific);
            }
            if (specific=='t'||specific=='T') {
                printf("\ntotal population of year %d is %.2fm",year,t1990);
            }
            if (specific=='r'||specific=='R') {
                printf("\nrural population of year %d is %.2fm and it is %d%% of total population",year,rn1990,rp1990);
            }
            if (specific=='u'||specific=='U') {
                printf("\nurban population of year %d is %.2fm and it is %d%% of total population",year,un1990,up1990);
            }
        }
        else if (year==2000) {
            printf("\npress t for total population r for rural population u for urban population: ");
            scanf("%s", &specific);
            while (specific!='t' && specific!='T'&& specific!='r'&& specific!='R'&& specific!='u'&& specific!='U') {
                printf("\nwrong input\n press t for total population r for rural population u for urban population: ");
                scanf("%s", &specific);
            }
            if (specific=='t'||specific=='T') {
                printf("\ntotal population of year %d is %.2fm", year, t2000);
            }
            if (specific=='r'||specific=='R') {
                printf("\nrural population of year %d is %.2fm and it is %d%% of total population",year,rn2000,rp2000);
            }
            if (specific=='u'||specific=='U') {
                printf("\nurban population of year %d is %.2fm and it is %d%% of total population",year,un2000,up2000);
            }
        }
        else if (year==2010) {
            printf("\npress t for total population r for rural population u for urban population: ");
            scanf("%s", &specific);
            while (specific!='t' && specific!='T'&& specific!='r'&& specific!='R'&& specific!='u'&& specific!='U') {
                printf("\nwrong input\n press t for total population r for rural population u for urban population: ");
                scanf("%s",&specific);
            }
            if (specific=='t'||specific=='T') {
                printf("\ntotal population of year %d is %.2fm",year,t2010);
            }
            if (specific=='r'||specific=='R') {
                printf("\nrural population of year %d is %.2fm and it is %d%% of total population",year,rn2010,rp2010);
            }
            if (specific=='u'||specific=='U') {
                printf("\nurban population of year %d is %.2fm and it is %d%% of total population",year,un2010,up2010);
            }
        }

        printf("\n\ndo you want to extract more data, press y for yes n for no: ");
        scanf("%s", &choice);
        while ((choice!='y'&&choice!='Y')&&(choice!='n'&&choice!='N')) {
            printf("\nwrong input\n do you want tto extract more data, press y for yes n for no: ");
            scanf("%s", &choice);
        }
    } while (choice=='y'||choice=='Y');

    printf("\n\nthank you for using our services");

    return 0;
}

Code with year as global variable:

#include <stdio.h>

int year = 0;

int main() {
    int rp1970=73, rp1980=66, rp1990=49, rp2000=38, rp2010= 29;
    int up1970=27, up1980=34, up1990=51, up2000=62, up2010=71;
    float t1970=10.44, t1980=13.14, t1990=17.57, t2000=23.30, t2010=28.60;
    float rn1970=7.62, rn1980=8.67, rn1990=8.67, rn2000=8.85, rn2010=8.29;
    float un1970=2.82, un1980=4.47, un1990=8.90, un2000=14.45, un2010=20.31;


    printf("                                 welcome to population data base for years, 1970,1980,1990,2000,2010 ");
    char specific, choice;
    do{
        printf("\n\nenter a year(choose between 1970,1980,1990,2000,2010):");
        scanf("%d",&year);
        while(year!=1970 && year!=1980 && year!=1990 && year!=2000 && year!=2010){
            printf("\nwrong input\n enter a year(choose between 1970,1980,1990,2000,2010):");
            scanf("%d",&year);
        }
        if(year==1970){
            printf("\npress t for total population r for rural population u for urban population: ");
            scanf("%s",&specific);
            while(specific!='t' && specific!='T'&& specific!='r'&& specific!='R'&& specific!='u'&& specific!='U'){
                printf("\nwrong input\n press t for total population r for rural population u for urban population: ");
                scanf("%s",&specific);
            }
            if(specific=='t'||specific=='T'){
                printf("\ntotal population of year %d is %.2fm ",year,t1970);
            }
            if(specific=='r'||specific=='R'){
                printf("\nrural population of year %d is %.2fm and it is %d%% of total population",year,rn1970,rp1970);
            }
            if(specific=='u'||specific=='U'){
                printf("\nurban population of year %d is %.2fm and it is %d%% of total population",year,un1970,up1970);
            }
        }
        else if(year==1980){
            printf("\npress t for total population r for rural population u for urban population: ");
            scanf("%s",&specific);
            while(specific!='t' && specific!='T'&& specific!='r'&& specific!='R'&& specific!='u'&& specific!='U'){
                printf("\nwrong input\n press t for total population r for rural population u for urban population: ");
                scanf("%s",&specific);
            }
            if(specific=='t'||specific=='T'){
                printf("\ntotal population of year %d is %.2fm ",year,t1980);
            }
            if(specific=='r'||specific=='R'){
                printf("\nrural population of year %d is %.2fm and it is %d%% of total population",year,rn1980,rp1980);
            }
            if(specific=='u'||specific=='U'){
                printf("\nurban population of year %d is %.2fm and it is %d%% of total population",year,un1980,up1980);
            }
        }
        else if(year==1990){
            printf("\npress t for total population r for rural population u for urban population: ");
            scanf("%s",&specific);
            while(specific!='t' && specific!='T'&& specific!='r'&& specific!='R'&& specific!='u'&& specific!='U'){
                printf("\nwrong input\n press t for total population r for rural population u for urban population: ");
                scanf("%s",&specific);
            }
            if(specific=='t'||specific=='T'){
                printf("\ntotal population of year %d is %.2fm",year,t1990);
            }
            if(specific=='r'||specific=='R'){
                printf("\nrural population of year %d is %.2fm and it is %d%% of total population",year,rn1990,rp1990);
            }
            if(specific=='u'||specific=='U'){
                printf("\nurban population of year %d is %.2fm and it is %d%% of total population",year,un1990,up1990);
            }
        }
        else if(year==2000){
            printf("\npress t for total population r for rural population u for urban population: ");
            scanf("%s",&specific);
            while(specific!='t' && specific!='T'&& specific!='r'&& specific!='R'&& specific!='u'&& specific!='U'){
                printf("\nwrong input\n press t for total population r for rural population u for urban population: ");
                scanf("%s",&specific);
            }
            if(specific=='t'||specific=='T'){
                printf("\ntotal population of year %d is %.2fm",year,t2000);
            }
            if(specific=='r'||specific=='R'){
                printf("\nrural population of year %d is %.2fm and it is %d%% of total population",year,rn2000,rp2000);
            }
            if(specific=='u'||specific=='U'){
                printf("\nurban population of year %d is %.2fm and it is %d%% of total population",year,un2000,up2000);
            }
        }
        else if(year==2010){
            printf("\npress t for total population r for rural population u for urban population: ");
            scanf("%s",&specific);
            while(specific!='t' && specific!='T'&& specific!='r'&& specific!='R'&& specific!='u'&& specific!='U'){
                printf("\nwrong input\n press t for total population r for rural population u for urban population: ");
                scanf("%s",&specific);
            }
            if(specific=='t'||specific=='T'){
                printf("\ntotal population of year %d is %.2fm",year,t2010);
            }
            if(specific=='r'||specific=='R'){
                printf("\nrural population of year %d is %.2fm and it is %d%% of total population",year,rn2010,rp2010);
            }
            if(specific=='u'||specific=='U'){
                printf("\nurban population of year %d is %.2fm and it is %d%% of total population",year,un2010,up2010);
            }
        }

        printf("\n\ndo you want to extract more data, press y for yes n for no: ");
        scanf("%s",&choice);
        while((choice!='y'&&choice!='Y')&&(choice!='n'&&choice!='N')){
            printf("\nwrong input\n do you want tto extract more data, press y for yes n for no: ");
            scanf("%s",&choice);
        }
    }while (choice=='y'||choice=='Y');

    printf("\n\nthank you for using our services");

    return 0;
}

Output:

output

the output should be : rural/ urban population of (the year entered by the user) is.....

it only works when the variable int year is global

enter image description here

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • 1
    try to limit line length. No one likes horizontal scroll. And put more spaces to make it readable – phuclv Apr 02 '18 at 14:35
  • When you learn about arrays and structures, you'll be able to make your code easier to read with less repetition. I assume you've simply not covered these yet (and it might be as well not to disillusion me if you have). – Jonathan Leffler Apr 02 '18 at 15:02
  • You should also write the code for obtaining 'total', 'rural' or 'urban' once, before you run tests on which year was requested. That way, you avoid repeating the code N times. – Jonathan Leffler Apr 02 '18 at 15:05

1 Answers1

2

The issue has nothing to do with the placement of the year variable. You are experiencing "undefined behavior" where anything can happen.

The real issue is that you are using the wrong format specifier to read in a char:

char specific;
scanf("%s",&specific);

The specific variable is only large enough to hold 1 char but you are telling scanf you want a string of chars. Even if you only enter 1 char in stdin, scanf will add a trailing \0.

To scan a char:

scanf(" %c",&specific);

You must also do this for choice.

001
  • 13,291
  • 5
  • 35
  • 66
  • first of all I want to thank you a looooot for taking the time and reading all of that. I noticed before that using %c for scanning choice for do while loops causes problems and sometimes it doesn't work at all ( it causes problems with cache or smth I don't really know) the teacher said that we have to use get char(); it didn't work for me, then someone told me to use %s and it worked very well for do whiles, then I started to use it instead of %c for everything. now I tried your solution it solved the year 1792 problem but the program keeps repeating wrong input twice, can u please try it – Edd Ahmed Apr 02 '18 at 17:04
  • @EddAhmed The problem you were probably having with `%c` is that it does not read the newline. So if you hit "t" the `scanf` will read the "t" but leave the newline character in the input buffer. So the next time you use `%c` it will read the newline char. The solution is to put a space before `%c`. This tells `scanf` to skip all whitespace before the char. Make sure you have that space in there. – 001 Apr 02 '18 at 17:10