gpt4 book ai didi

c - 它仅在我将年份设为全局变量时有效,尽管只使用了一个主要功能

转载 作者:太空宇宙 更新时间:2023-11-04 06:52:16 25 4
gpt4 key购买 nike

在这段代码中,当我在主函数中更改变量 int year 的位置时,输出打印行每年都会用 1792 替换,尽管我根本没有 1792

year 作为局部变量的代码:

#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;
}

year 作为全局变量的代码:

#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

输出应该是:(用户输入的年份)的农村/城市人口是......

只有当变量 int year 是全局的时候才有效

enter image description here

最佳答案

此问题与 year 变量的位置无关。您正在经历任何事情都可能发生的“未定义行为”。

真正的问题是您使用了错误的格式说明符来读取字符:

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

specific 变量只够容纳 1 个 char,但您告诉 scanf 您想要一串字符。即使您只在标准输入中输入 1 个字符,scanf 也会添加尾随 \0

扫描一个字符:

scanf(" %c",&specific);

您还必须为 choice 执行此操作。

关于c - 它仅在我将年份设为全局变量时有效,尽管只使用了一个主要功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49612092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com