jeudi 4 mars 2021

Variable 'hasil' is used uninitialized whenever 'if' condition is false

#include<iostream>

using namespace std;

int main()
{
    float a,b,hasil;
    char aritmatika;
    
    cout <<"Selamat datang di Program Calculator Sederhana \n \n";
    
    //Memasukkan input dari user
    
    cout <<"Silahkan masukkan nilai pertamamu: ";
    cin >>a;
    
    cout <<"Pilih operator+,-,*,/: ";
    cin >>aritmatika;
    
    cout <<"Silahkan masukkan nilai keduamu: ";
    cin >>b;
    
    cout <<"\n \nHasil Perhitungan:\n\n";
    cout <<a <<aritmatika <<b;
    
    if(aritmatika =='+'){
    hasil = a+b;
    }else if(aritmatika =='-'){
    hasil = a-b;
    }else if(aritmatika =='*'){
    hasil = a*b;
    }else if(aritmatika =='/'){
    hasil = a/b;
    }else{
        cout <<"Operator yang anda masukkan salah"<<endl;
            }
      cout <<"= "    <<hasil<<endl;
        
        cin.get();
        return 0;
    }

So I'm a beginner and I'm making this simple calculator for my assignment, but then there's this warning in else if(aritmatika =='/'){ hasil = a/b; I don't understand why it shows that it's uninitialized and when I erased that line, the same thing goes with the else if on top of it. I copied my teacher's code and it's still not working and not running. I'll appreciate it if you can help me!

Aucun commentaire:

Enregistrer un commentaire