我的程序在完成后打印了错误的东西,我不知道为什么。C++
My program is printing the wrong thing after completing, and I can't figure out why. C++
程序正常运行时,应该先向用户询问代码(a, C,D),然后再询问半径。
它做得很好
但是当它完成时,它应该计算出一些看起来像"半径为6.75的圆的面积是143.14"的东西
但是当我运行我的时,它打印出"半径为6.75的圆的面积是a"。我似乎就是找不到我做错了什么,任何帮助都非常感谢。
#include <iostream>
#include <iomanip>
using namespace std;
/*run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) {
cout << fixed << setprecision(2);
char Code;
double Radius;
//promt for a code and A, C, or D
cout << "Please enter your code A, C, or D ";
// read the input
cin >> Code;
//promt user for value of radius
cout << "Please enter a value for the radius";
//read the input radius
cin >> Radius;
// based on the code, calculate the required computed result
double Const;
Const = 3.1416;
'A' == Const * Radius;
'C' == 2 * Const * Radius;
'D' == 2 * Radius;
if (Code == 'A')
cout << "The area of a circle with radius" << Radius << "is" << 'A' << endl;
else if (Code == 'C')
cout << "The circumference of a circle with radius" << Radius << "is" << 'C' << endl;
else if (Code == 'D')
cout << "The diameter of a circle with radius" << Radius << "is" << 'D' << endl;
//output the result
return 0;
}
我想你最好先复习一下最新的c++课。
这是你的代码的修复:
double a = Const * Radius; //'A' == Const * Radius;
double c = 2 * Const * Radius; //'C' == 2 * Const * Radius;
double d = 2 * Radius; //'D' == 2 * Radius;
if (Code == 'A')
cout << "The area of a circle with radius" << Radius << "is" << a /*'A'*/<< endl;
else if (Code == 'C')
cout << "The circumference of a circle with radius" << Radius << "is" << c /*'C'*/ << endl;
else if (Code == 'D')
cout << "The diameter of a circle with radius" << Radius << "is" << d /*'D'*/ <<
endl;
'A', 'B', 'C'和1,3,100等一样都是值。
=
为赋值运算符,==
为比较运算符。
一旦你写了像'A' == Radius;
这样的东西,它将被评估为布尔值(很可能是false
值),就是这样。以同样的方式,您可以在代码中编写false;
或5;
。
你不是赋值,而是使它等于那个值。你可以给char赋值
#include <iostream>
#include <iomanip>
using namespace std;
/*run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) {
cout << fixed << setprecision(2);
char Code;
double Radius;
//promt for a code and A, C, or D
cout << "Please enter your code A, C, or D ";
// read the input
cin >> Code;
//promt user for value of radius
cout << "Please enter a value for the radius";
//read the input radius
cin >> Radius;
// based on the code, calculate the required computed result
double Const;
Const = 3.1416;
/* NO need of this. you can't assign value to a char.
'A' == Const * Radius;
you have used == sign whic is not assigning
'C' = 2 * Const * Radius;
'D' = 2 * Radius;
*/
if (Code == 'A'){
float temp; //change
temp=Const * Radius; //change
cout << "The area of a circle with radius" << Radius << "is" << temp << endl;
}
else if (Code == 'C'){
float temp;
temp=2 * Const * Radius;
cout << "The circumference of a circle with radius" << Radius << "is" << temp<< endl;
}
else if (Code == 'D'){
float temp;
temp=2 * Radius;
cout << "The diameter of a circle with radius" << Radius << "is" << temp << endl;
}
//Hope this helps.
//output the result
return 0;
}
相关文章:
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 我不知道为什么这段代码会让核心被转储?
- 没有函数模板的实例与我不知道为什么的参数列表匹配
- if-else 语句仅按特定顺序工作,我不知道为什么
- DirectX 11 引擎在第三次代码改进后没有突然运行,我不知道为什么
- 函数抛出seg错误,我不知道为什么
- 我不知道为什么复制构造函数的调用在 c++ 中不稳定
- 随着时间的推移,我的程序越来越慢,我不知道为什么.内存泄漏
- "错误:在'.标记'之前出现预期的主表达式,我不知道为什么
- 我正在制作c ++代码,但发生错误我不知道为什么
- std::cout << glGetString(GL_RENDER) << std::endl;抛出错误但不GL_Renderer或GL_Verision,我不知道为什么?
- 我的Arduino串行命令行不起作用,我不知道为什么
- 我正在编写一个拆分为 3 的合并排序,我不知道为什么它不起作用
- 我不知道为什么我的矢量无法推回
- 语法检查器指出一些我不知道为什么的错误
- 我不知道为什么我的字符串无法保存值
- 编写蒙特卡洛集成程序,我不知道为什么我会收到某些错误
- 我不知道为什么会发生堆损坏(关于内存分配问题)
- 库特是空的,我不知道为什么
- C++:我需要在这里包含这个标题,我不知道为什么