C++ 而循环不重复,直接退出
我尝试在计算过程结束后询问用户,如果他想对我使用的do while循环进行更多计算,但出于某种原因,它应该首先计算面积,然后询问用户是否想计算更多,它应该重复或退出C++ 而循环不重复,直接退出,c++,C++,我尝试在计算过程结束后询问用户,如果他想对我使用的do while循环进行更多计算,但出于某种原因,它应该首先计算面积,然后询问用户是否想计算更多,它应该重复或退出 #include <math.h> #include <stdio.h> #include <stdlib.h> int main() { int option; float Area, Radius, Length, Width, Height, Circumference, V
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
int option;
float Area, Radius, Length, Width, Height, Circumference, Volume;
char select;
do {
printf("\nPlease select the option\n");
printf(
"1.Area and Circumference of circle \n2.Area of rectangle \n3.Area of "
"triangle \n4.Volume of sphere");
printf("\nEnter your selection : ");
scanf("%i", &option);
system("cls");
switch(option) {
case 1:
printf("Enter the radius of the circle : ");
scanf("%f", &Radius);
Area = 3.14159 * pow(Radius, 2);
Circumference = 2 * 3.14159 * Radius;
system("cls");
printf("Area of circle is %f Circumference of circle is %f", Area,
Circumference);
break;
case 2:
printf("Enter length of the rectangle :");
scanf("%f", &Length);
system("cls");
printf("Enter width of the rectangle :");
scanf("%f", &Width);
system("cls");
Area = Length * Width;
printf("The Area of rectangle is %f", Area);
break;
case 3:
printf("Enter the height of triangle :");
scanf("%f", &Height);
system("cls");
printf("Enter the lenght of triangle :");
scanf("%f", &Length);
system("cls");
Area = Height * Length / 2;
printf("The area of the triangle is %f", Area);
break;
case 4:
printf("Enter the radius of the sphere :");
scanf("%f", &Radius);
system("cls");
Volume = 4 * 3.14159 * pow(Radius, 3) / 3;
printf("The surface area of the sphere is %f", Volume);
break;
default:
printf("Invalid option\t\t");
break;
}
printf("\nDo you want to do another calculation");
scanf("%c", &select);
} while(select == 'y');
}
#包括
#包括
#包括
int main(){
int选项;
浮动面积、半径、长度、宽度、高度、周长、体积;
字符选择;
做{
printf(“\n请选择选项“\n”);
printf(
“1.圆的面积和周长\n2.矩形的面积\n3.矩形的面积”
“三角形\n4.球体的体积”);
printf(“\n输入您的选择:”);
scanf(“%i”,选项(&O));
系统(“cls”);
开关(选件){
案例1:
printf(“输入圆的半径:”);
扫描频率(“%f”、&Radius);
面积=3.14159*pow(半径2);
周长=2*3.14159*半径;
系统(“cls”);
printf(“圆的面积为%f,圆的周长为%f”,面积,
周长);
打破
案例2:
printf(“输入矩形的长度:”);
扫描频率(“%f”和长度);
系统(“cls”);
printf(“输入矩形的宽度:”);
扫描频率(“%f”和宽度);
系统(“cls”);
面积=长度*宽度;
printf(“矩形的面积为%f”,面积);
打破
案例3:
printf(“输入三角形的高度:”);
扫描频率(“%f”和高度);
系统(“cls”);
printf(“输入三角形的长度:”);
扫描频率(“%f”和长度);
系统(“cls”);
面积=高度*长度/2;
printf(“三角形的面积为%f”,面积);
打破
案例4:
printf(“输入球体的半径:”);
扫描频率(“%f”、&Radius);
系统(“cls”);
体积=4*3.14159*pow(半径,3)/3;
printf(“球体的表面积为%f”,体积);
打破
违约:
printf(“无效选项\t\t”);
打破
}
printf(“\n是否要进行另一次计算”);
scanf(“%c”,选择(&S);
}while(select=='y');
}
不工作代码运行正常,在进行下一次输入之前,您只需刷新输入缓冲区,您可以使用
fflush(stdin)
。我在代码中添加了这一行
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
int option;
float Area, Radius, Length, Width, Height, Circumference, Volume;
char select;
do {
printf("\nPlease select the option\n");
printf(
"1.Area and Circumference of circle \n2.Area of rectangle \n3.Area of "
"triangle \n4.Volume of sphere");
printf("\nEnter your selection : ");
scanf("%i", &option);
system("cls");
switch(option) {
case 1:
printf("Enter the radius of the circle : ");
scanf("%f", &Radius);
Area = 3.14159 * pow(Radius, 2);
Circumference = 2 * 3.14159 * Radius;
system("cls");
printf("Area of circle is %f Circumference of circle is %f", Area,
Circumference);
break;
case 2:
printf("Enter length of the rectangle :");
scanf("%f", &Length);
system("cls");
printf("Enter width of the rectangle :");
scanf("%f", &Width);
system("cls");
Area = Length * Width;
printf("The Area of rectangle is %f", Area);
break;
case 3:
printf("Enter the height of triangle :");
scanf("%f", &Height);
system("cls");
printf("Enter the lenght of triangle :");
scanf("%f", &Length);
system("cls");
Area = Height * Length / 2;
printf("The area of the triangle is %f", Area);
break;
case 4:
printf("Enter the radius of the sphere :");
scanf("%f", &Radius);
system("cls");
Volume = 4 * 3.14159 * pow(Radius, 3) / 3;
printf("The surface area of the sphere is %f", Volume);
break;
default:
printf("Invalid option\t\t");
break;
}
printf("\nDo you want to do another calculation : ");
fflush(stdin); // Flushing the input buffer
scanf("%c", &select);
} while(select == 'y');
}
#包括
#包括
#包括
int main(){
int选项;
浮动面积、半径、长度、宽度、高度、周长、体积;
字符选择;
做{
printf(“\n请选择选项“\n”);
printf(
“1.圆的面积和周长\n2.矩形的面积\n3.矩形的面积”
“三角形\n4.球体的体积”);
printf(“\n输入您的选择:”);
scanf(“%i”,选项(&O));
系统(“cls”);
开关(选件){
案例1:
printf(“输入圆的半径:”);
扫描频率(“%f”、&Radius);
面积=3.14159*pow(半径2);
周长=2*3.14159*半径;
系统(“cls”);
printf(“圆的面积为%f,圆的周长为%f”,面积,
周长);
打破
案例2:
printf(“输入矩形的长度:”);
扫描频率(“%f”和长度);
系统(“cls”);
printf(“输入矩形的宽度:”);
扫描频率(“%f”和宽度);
系统(“cls”);
面积=长度*宽度;
printf(“矩形的面积为%f”,面积);
打破
案例3:
printf(“输入三角形的高度:”);
扫描频率(“%f”和高度);
系统(“cls”);
printf(“输入三角形的长度:”);
扫描频率(“%f”和长度);
系统(“cls”);
面积=高度*长度/2;
printf(“三角形的面积为%f”,面积);
打破
案例4:
printf(“输入球体的半径:”);
扫描频率(“%f”、&Radius);
系统(“cls”);
体积=4*3.14159*pow(半径,3)/3;
printf(“球体的表面积为%f”,体积);
打破
违约:
printf(“无效选项\t\t”);
打破
}
printf(“\n是否要进行另一次计算:”);
fflush(stdin);//刷新输入缓冲区
scanf(“%c”,选择(&S);
}while(select=='y');
}
EDIT-正如@David C.Rankin和@molbdnilo所指出的注释中未定义的行为,我不鼓励您将fflush
作为常规做法,您甚至可以简单地编写scanf(“%C”,&select)代码>作为scanf(“%c”,&select)
或者,您可以创建另一个临时字符变量来浸泡新行
输入字符,如
scanf(“%c”,&select)代码>
替换为-
chartemp代码>
scanf(“%c”、&temp)代码>
scanf(“%c”,&select)代码>
为什么这是标记的css?请正确格式化您的代码,它目前完全不可读。请参阅。使用scanf(“%c”,选择(&select))读取的字符代码>将成为该新行。要读取第一个非空白字符,使用“代码>”%C“/CODE >。这可以编译为C++程序,但它看起来像纯C。在读取了一个数之后,输入流中的下一个内容是空白字符。读取单个字符不会跳过空白。有关标准输入的fflush
ing就是fflush(stdin)除了Microsoft的实现之外,代码>在所有实现上都是未定义的行为。感谢您的帮助