gpt4 book ai didi

你能修改我的程序以在c中使用 "y/n"语句吗

转载 作者:行者123 更新时间:2023-11-30 19:26:17 25 4
gpt4 key购买 nike

在这里,我正在编写一个 C 编程代码,我需要你们的帮助!我想修改我的程序并想在循环中使用它,即如果用户输入“y”并且如果输入“n”,则必须结束程序,我正在尝试使用 switch case,我尽力结束程序,它正在返回结束,但我不知道如何使用“y”值再次运行程序!

#include<conio.h>
#include<stdio.h>

int main()
{
char n, A, B;
float kmTOm = 0.621371;
float inchesTOfoot = 0.0833333;
float cmTOinches = 0.393701;
float poundtokg = 0.453592;
float inchesTOmeter = 0.0254;
float a, b;
while (1)
{
printf("Enter the input character. q to quit\n1. kms to miles\n2. inches to foot\n3. cms to inches\n4. pound to kgs\n5. inches to meters\n");

scanf(" %c", &n);
switch (n)
{
case 'q':
printf("Quitting the program...");
goto end;
break;
case '1':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * kmTOm;
printf("%.2f Kms is equal to %.2f Miles\n\n\n", a, b);
break;
case '2':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * inchesTOfoot;
printf("%f Inches is equal to %f Foot\n", a, b);
break;
case '3':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * cmTOinches;
printf("%f Cms is equal to %f Inches\n", a, b);
break;
case '4':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * poundtokg;
printf("%f Pounds is equal to Kgs %f \n", a, b);
break;
case '5':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * inchesTOmeter;
printf("%f inches is equal to %f meters \n", a, b);
break;
default:
printf("In default now");
break;
}
}
end:
return 0;
}

我希望你们开发人员进行善意的修改来帮助我,因为我是一个初学者,学习了一个月,我除了实际的输出必须是这样的 ----o/p- do you Want to quit press "q”,你想再次运行按“y”

最佳答案

如果将 while(1) 更改为变量,则可以在每个循环后测试是否是时候退出。

我对您的代码所做的更改均在下面有注释:

#include<conio.h>
#include<stdio.h>

int main()
{
char n, A, B;
float kmTOm = 0.621371;
float inchesTOfoot = 0.0833333;
float cmTOinches = 0.393701;
float poundtokg = 0.453592;
float inchesTOmeter = 0.0254;
float a, b;
n = 'y'; //set n to make sure while repeats
while (n != 'n') //change while to test "n" instead of 1, this way the value of n can be changed and the loop can be exited
{
printf("Enter the input character. q to quit\n1. kms to miles\n2. inches to foot\n3. cms to inches\n4. po
und to kgs\n5. inches to meters\n");

scanf(" %c", &n);
switch (n)
{
case 'q':
printf("Quitting the program...");
return(0); //exit entire program
break;
case '1':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * kmTOm;
printf("%.2f Kms is equal to %.2f Miles\n\n\n", a, b);
break;
case '2':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * inchesTOfoot;
printf("%f Inches is equal to %f Foot\n", a, b);
break;
case '3':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * cmTOinches;
printf("%f Cms is equal to %f Inches\n", a, b);
break;
case '4':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * poundtokg;
printf("%f Pounds is equal to Kgs %f \n", a, b);
break;
case '5':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * inchesTOmeter;
printf("%f inches is equal to %f meters \n", a, b);
break;
default:
printf("In default now");
break;
}

//Ask the user if they want to run again. The code will exit if the user types 'n', it will repeat if they type anything else (including 'y').
printf("\nRun again? y/n:");
scanf(" %c",&n);

}

return 0;
}

关于你能修改我的程序以在c中使用 "y/n"语句吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57697332/

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