繁体   English   中英

我的程序在完成后打印错误的东西,我不知道为什么。 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;东西'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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM