มีปัญหาเรื่องกับเรื่องนี้เรียบง่ายโปรแกรม

0

คำถาม

โปรแกรมเป็นแค่เพียงต้องคำนวณผู้ใช้อายุโดย subtracting พวกเขา dob จากปัจจุบันอปีที่แล้ว ตอนที่ฉันวิ่งโปรแกรมมัน compiles เรียบร้อยแล้วแต่ฉันเข้าใจมานานจำนวอย่างเช่น -215863352. ที่หากและอีกเงื่อนไขเป็นการเพิ่มแค่จะทดสอบพวกเขาออกไปฉันกำลังเขียนโปรแกรมต่างๆโดยใช้พวกเขาเพื่อให้แน่ใจว่าฉันเข้าใจรูปแบบการสั่งงานใน c. ฉันคิดว่าฉันขาดอะไรบางอย่างง่ายอย่างแต่ไม่สามารถคิดออก

#include <stdio.h>
int main()
{
    int year;
    int cyear;
    int age = cyear - year;

    printf("Please enter the year you were born: \n");
    scanf("%i", &year);
    printf("Now enter the current year: \n");
    scanf("%i", &cyear);

    if (1+1 == 2){
        printf("You must be %i", age);
    }
    else {
        printf("Cannot compute age, GOODBYE:\n");
    }
    return 0;
}
2
0

คุณกำลังคำนวณอายุก่อนที่นำเข้าข้อมูลถูกพรากไปจากที่ใช้งาน ดังนั้น age ตัวแปรคือจัดเก็บถังขยะมูลค่าอยู่

ทางออก:

ตำแหน่งการคำนวณของยุคหลังจากรับข้อมูลจากของผู้ใช้นั้นคือหลังจากถ่ายข้อมูลของ cyear ใช้ scanf. ที่ถูกต้องรหัสคือให้ด้านล่างนี้ #รวม<stdio.h>

int main()
{
    int year;
    int cyear;
    int age =0;     //initialise with 0

    printf("Please enter the year you were born: \n");
    scanf("%i", &year);
    printf("Now enter the current year: \n");
    scanf("%i", &cyear);
    
    age = cyear - year;     //note the change here

    if (1+1 == 2){
        printf("You must be %i", age);
    }
    else {
        printf("Cannot compute age, GOODBYE:\n");
    }
    return 0;
}
2021-11-23 08:07:23
-2
enter code here
   #include <stdio.h>
   int main()
   {
  long long int year;
  printf("Please enter the year you were born: \n");
scanf("%lld",&year);
long long int cyear;
  printf("Now enter the current year: \n");
scanf("%lld",&cyear);

long long  int age = cyear-year;

if (1){
    printf("You must be %lld", age);
}
else {  printf("Now enter the current year: \n");
scanf("%lld",&cyear);

    printf("Cannot compute age, GOODBYE:\n");
}
return 0;

}

2021-11-23 08:23:51

มันดูเหมือนว่าปัญหาของโผล่มา beacuse initialization.
ali balhtiari

สวัสดียินดีต้อนรับสู่ดังนั้น! ได้โปรดรวมถึงการคำอธิบายคำตอบของคุณไม่ใช่แค่รหัส :)
Docteur

ถ้ามีการคำนวณ(ตัวอย่างเช่นอายุ=cyear_year)ก่อน initialization มันจะตอบผิดหรอกเพราะคุณไม่รู้จักคนของขวัญทำการเมานท์ของตัวแปรและมันจะองค่าบางอย่างจากความทรงจำของฉัน
ali balhtiari

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่