简体   繁体   English

无法选择if循环的值

[英]Cant get choose value to if loops

Help 救命

#include <iostream>
#include <math.h>
using namespace std;
const float pi = 3.14;
void Odabir(int);
int main(){
    int choose;
    cout << "Odaberite 1 2 ili 3" << endl;
    cin >> choose;
    Odabir(choose);
    return 0;
}
void Odabir(int choose){
    if (choose = 1){
        float b, vb;
        cout << "Unesite duljinu stranice b: " << endl;
        cin >> b;
        cout << "Unesite duljinu visine na stranicu b vb: " << endl;
        cin >> vb;
        cout << "Povrsina raznostranicnog trokuta je: " << ((b*vb) / 2) << endl;
    }
    if (choose = 2){
        float r;
        cout << "Unesite duljinu polumjera: " << endl;
        cin >> r;
        cout << "Povrsina kruga je: " << pow(r, 2)*pi << endl;
    }

}

Been trying to solve that for few hrs and I cant get thru it , it looks like when I set choose value all three if's got executed and printed. 一直试图解决这个问题几个小时,我不能通过它,看起来当我设置选择值全部三个如果执行和打印。

You are using the assignment operator ( = ) instead of the comparison operator ( == ) in the if statements in the function. 您正在函数的if语句中使用赋值运算符( = )而不是比较运算符( == )

For example 例如

if (choose = 1){
          ^^^ 

Write instead 改为写

if (choose == 1){
          ^^^^ 

You are assigning the variable choose instead of comparing it inside the Odabir function. 您正在分配变量选择而不是在Odabir函数内进行比较。

for instance, choose = 1 should be choose == 1 例如,choose = 1应该选择== 1

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

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