gpt4 book ai didi

c - 读取我的输入后,程序在 Code::blocks 中没有响应

转载 作者:行者123 更新时间:2023-11-30 15:16:34 24 4
gpt4 key购买 nike

我正在尝试在 code::blocks 中编写一个程序,该程序将允许我选择要进行的多个单位转换。但是,每当我构建和编译并到达程序中扫描我的输入中的变量“选择”时, code::blocks 会立即显示一个窗口,显示我的 .exe 已停止工作,我无法运行弄清楚为什么会出现这种情况。我正在使用 GNU GCC 编译器。任何帮助将不胜感激。

#include <stdio.h>

int main()
{
float fahrenheit;
float celsius;
float pound;
float kilogram;
float inch;
float centimeter;
float mph;
float kph;
int foc;
int pok;
int ioc;
int mok;
int choice;

printf("\n1. Temperature Conversion\n");
printf("\n2. Weight Conversion\n");
printf("\n3. Length Conversion\n");
printf("\n4. Speed Conversion\n");
printf("\n5. Exit Program\n");
printf("\n");
printf("\nEnter the number of the program you would like to run :\n");
printf("\n");
scanf("%d", choice);

//Temperature Conversion
if(choice == 1) {
printf("\n1. Convert from Celsius to Fahrenheit\n");
printf("\n2. Convert from Fahrenheit to Celsius\n");
printf("\nEnter the number that corresponds with the conversion you would like to do:\n");
printf("\n");
scanf("%d", &foc);

if(foc == 1) {
//option 1
printf("\nEnter your temperature in Celsius : ");
scanf("%f", &celsius);
fahrenheit = 32 + (celsius * 1.8);
printf("\nYour temperature in Fahrenheit : %f ", fahrenheit);
}
else {
//option 2
printf("\nEnter your temperature in Fahrenheit : ");
scanf("%f", &fahrenheit);
celsius = (fahrenheit - 32) * 0.55555555;
printf("\nYour temperature in Celsius : %f ", celsius);
}
}
//Weight Conversion
else if(choice == 2) {
printf("\n1. Convert from Pound to Kilogram ");
printf("\n2. Convert from Kilogram to Pound ");
printf("\nEnter the number that corresponds with the conversion you would like to do: ");
printf("\n ");
scanf("%d", &pok);

if(pok == 1) {
//option 1
printf("\nEnter your weight in pounds : ");
scanf("%f", &pound);
kilogram = (2.20462 * pound);
printf("\nYour weight in kilograms : %f ", kilogram);
}
else {
//option 2
printf("\nEnter your weight in kilograms : ");
scanf("%f", &kilogram);
pound = (kilogram/2.20462);
printf("\nYour weight in pounds : %f ", celsius);
}
}
//Length Conversion
else if(choice == 3) {
printf("\n1. Convert from inches to centimeters ");
printf("\n2. Convert from centimeters to inches ");
printf("\nEnter the number that corresponds with the conversion you would like to do: ");
printf("\n ");
scanf("%d", &ioc);

if(ioc == 1) {
//option 1
printf("\nEnter your length in inches : ");
scanf("%f", &inch);
centimeter = (inch/2.54);
printf("\nYour length in centimeters : %f ", centimeter);
}
else {
//option 2
printf("\nEnter your length in centimeters : ");
scanf("%f", &centimeter);
inch = (centimeter*2.54);
printf("\nYour length in inches : %f ", inch);
}
}
//Speed Conversion
else if(choice == 4) {
printf("\n1. Convert from mph to kph ");
printf("\n2. Convert from kph to mph ");
printf("\nEnter the number that corresponds with the conversion you would like to do: ");
printf("\n ");
scanf("%d", &mok);

if(mok == 1) {
//option 1
printf("\nEnter your speed in mph : ");
scanf("%f", &mph);
kph = (mph/1.60934);
printf("\nYour speed in kilometers: %f ", kph);
}
else {
//option 2
printf("\nEnter your speed in kph : ");
scanf("%f", &kph);
mph = (1.60934*kph);
printf("\nYour length in inches : %f ", mph);
}
}
else if(choice == 5) {
printf("\nProgram has ended. Thanks for your time!");
}
else {
printf("\nThat is not a valid program number. ");
}
}

最佳答案

您必须将指向 choice 变量(即 &choice)的指针传递给您的 scanf()调用,就像您对其他 scanf() 用途所做的那样。

即而不是

scanf("%d", choice);

使用

scanf("%d", &choice);

关于c - 读取我的输入后,程序在 Code::blocks 中没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33005798/

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