[英]Why my function in C++ is not being executed?
我有一個 C++ 實驗室,問題是:用戶應該為 X 輸入一個值(X 是舉行的測試次數)。 如果 x<15,程序不計算任何東西。 如果 X 介於 16 和 30 之間,則程序應計算 C=1.0/10.0*(24 A),如果 X>30,則程序應計算 C=0.15 (24*A)。 我的多個 if 代碼有效,但是當我輸入 X 的值時,方程未解。 有人有什么主意嗎??
#include<iostream>
#include<cmath>
#define _USE_MATH_DEFINES
using namespace std;
int main()
{
//variables defined here
float A, X, C, F;
//A stands for number of classes scheduled
//X is for number of tests and C is the modification
cout << "This program predicts the number of cars parked at HVCC at any given hour \n";
cout << "enter a value for A \n";
cin >> A;
cout << "enter a value for number of tests X \n";
cin >> X;
if (X < 15)
{
cout << "No modificatons needed for under 15 tests \n";
}
else if (X > 15 && X < 20)
{
cout << "Approximation for between 15-30 tests \n";
C = 1.0 / 10.0 * (24 * A);
cin >> C;
}
else
{
cout << "Approximation for more than 30 tests \n";
C = 0.15 * (24 * A);
cin >> C;
}
}
使用cin
可以讀取用戶輸入。 使用cout
打印結果:
#include<iostream>
#include<cmath>
#define _USE_MATH_DEFINES
using namespace std;
int main()
{
//variables defined here
float A, X, C, F;
//A stands for number of classes scheduled
//X is for number of tests and C is the modification
cout << "This program predicts the number of cars parked at HVCC at any given hour \n";
cout << "enter a value for A \n";
cin >> A;
cout << "enter a value for number of tests X \n";
cin >> X;
if (X < 15)
{
cout << "No modificatons needed for under 15 tests \n";
}
else if (X > 15 && X < 20)
{
cout << "Approximation for between 15-30 tests \n";
C = 1.0 / 10.0 * (24 * A);
cout << C; // replace cin >> with cout <<
}
else
{
cout << "Approximation for more than 30 tests \n";
C = 0.15 * (24 * A);
cout << C; // replace cin >> with cout <<
}
}
cin >> C; will get the user input for C.
在兩個 else 塊中將其更改為cout << C
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.