我的程序在完成后打印了错误的东西,我不知道为什么。C++

My program is printing the wrong thing after completing, and I can't figure out why. C++

本文关键字:我不知道 为什么 C++ 错误 程序 打印 我的      更新时间:2023-10-16

程序正常运行时,应该先向用户询问代码(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;
}