What's new

Closed C++ Nested if else

Status
Not open for further replies.

Welcome Guest

Eternal Poster
Joined
Nov 29, 2015
Posts
1,002
Reaction
148
Points
509
Age
27
Mga ka ts, pa help naman po sa problem na to.... Pinagpuyatan ko siya dko parin makuha laging may error sa codes ko... Using if else lang po ang gagamitin .. no switch just if else.
Pwede po gumamit ng logical operators. Pa help naman sa mga henyo dyan sa programming please.... Still working on it, mag iintay ako ng sagot nyo while coding. Thanks po in advance
 

Attachments

Magbigay ka muna ng input mo tsaka mo ipa-check for errors or improvements.
Eto po no errors pero hindi po ako sure sa output niya if na meet ko ba hinihingi ng problem machine.

double membership, value, reward ;

cout << "1. Standard" << endl;
cout << "2. Plus" << endl;
cout << "3. ρrémíùm" << endl;
cout << "Choose membership type: " ;
cin >> membership ;

if (membership==1)
{
cout <<"You've choose Standard Membership. \n";
cout <<"Please enter purchased value: " ;
cin >> value ;
if (value<=75)
{
cout <<"You've earned 5% of the total monthly purchase. \n" ;
reward= value * 0.05 ;
cout << "You've earned: " << reward << "pts." <<endl;

}
else if (value<=149.99)
{
cout << "You've earned 7.5% of the total monthly purchase. \n" ;
reward= value * 0.075 ;
cout << "You've earned: " << reward <<"pts." << endl;
}
else if (value>=150)
{
cout << "You've earned 10% of the total monthly purchase. \n" ;
reward=value* 0.1 ;
cout << "You've earned: " << reward << "pts." <<endl;
}

}

if (membership==2)
{
cout <<"You've choose Plus Membership. \n" ;
cout <<"Please enter purchased value: " ;
cin >> value ;
if (value<=150)
{
cout <<"You've earned 6% of the total monthly purchase. \n" ;
reward= value * 0.06 ;
cout << "You've earned: " << reward <<"pts." << endl;
}
else if (value>=150)
{
cout << "You've earned 13% of the total monthly purchase. \n" ;
reward= value * 0.13 ;
cout << "You've earned: " << reward << "pts." <<endl;
}
}

if (membership==3)
{
cout <<"You've choose ρrémíùm Membership. \n" ;
cout <<"Please enter purchased value: " ;
cin >> value ;
if (value<=200)
{
cout <<"You've earned 4% of the total monthly purchase. \n" ;
reward= value * 0.04 ;
cout << "You've earned: " << reward <<"pts." << endl;
}
else if (value>=200)
{
cout << "You've earned 15% of the total monthly purchase. \n" ;
reward= value * 0.15 ;
cout << "You've earned: " << reward << "pts." << endl;
}
}
 
Eto po no errors pero hindi po ako sure sa output niya if na meet ko ba hinihingi ng problem machine.

double membership, value, reward ;

cout << "1. Standard" << endl;
cout << "2. Plus" << endl;
cout << "3. ρrémíùm" << endl;
cout << "Choose membership type: " ;
cin >> membership ;

if (membership==1)
{
cout <<"You've choose Standard Membership. \n";
cout <<"Please enter purchased value: " ;
cin >> value ;
if (value<=75)
{
cout <<"You've earned 5% of the total monthly purchase. \n" ;
reward= value * 0.05 ;
cout << "You've earned: " << reward << "pts." <<endl;

}
else if (value<=149.99)
{
cout << "You've earned 7.5% of the total monthly purchase. \n" ;
reward= value * 0.075 ;
cout << "You've earned: " << reward <<"pts." << endl;
}
else if (value>=150)
{
cout << "You've earned 10% of the total monthly purchase. \n" ;
reward=value* 0.1 ;
cout << "You've earned: " << reward << "pts." <<endl;
}

}

if (membership==2)
{
cout <<"You've choose Plus Membership. \n" ;
cout <<"Please enter purchased value: " ;
cin >> value ;
if (value<=150)
{
cout <<"You've earned 6% of the total monthly purchase. \n" ;
reward= value * 0.06 ;
cout << "You've earned: " << reward <<"pts." << endl;
}
else if (value>=150)
{
cout << "You've earned 13% of the total monthly purchase. \n" ;
reward= value * 0.13 ;
cout << "You've earned: " << reward << "pts." <<endl;
}
}

if (membership==3)
{
cout <<"You've choose ρrémíùm Membership. \n" ;
cout <<"Please enter purchased value: " ;
cin >> value ;
if (value<=200)
{
cout <<"You've earned 4% of the total monthly purchase. \n" ;
reward= value * 0.04 ;
cout << "You've earned: " << reward <<"pts." << endl;
}
else if (value>=200)
{
cout << "You've earned 15% of the total monthly purchase. \n" ;
reward= value * 0.15 ;
cout << "You've earned: " << reward << "pts." << endl;
}
}
Tama naman ito sir..medyo may pagka redundant lang sa ibang code pero na meet mo yung output..(y)
 
Hmm...

Kapag may train ka ng if-else, walang strong-association between membership_type:purchased_value:reward_points.
Mas tamang approach dyan, i-bundled mo ang bawat categories together with their own attributes.

Isa pa, parang may logic-error sa Plus Plan, ie bakit mas mataas pa reward sa less than 150 kesa sa more than that?

Eto sample code ko, (yung sa Plus Plan binago ko arbitrarily into 16%). BTW, walang input validation dyan at hindi naman iyan primary objective.

Code:
#include <iostream>

struct Standard {
    float get_reward() {
        if (purchased_value < 75) {
            reward_points = 0.05;
        }
        else if (purchased_value > 75 && purchased_value < 149.99) {
            reward_points = 0.075;
        }
        else {
            reward_points = 0.10;;
        }
        return reward_points;
    }

    const std::string name = "STANDARD";
    float purchased_value;
    private:
        float reward_points;
} standard;

struct Plus {
    float get_reward() {
        if (purchased_value < 150) {
            reward_points = 0.10;
        }
        else {
            reward_points = 0.16;;
        }
        return reward_points;
    }
    const std::string name = "PLUS";
    float purchased_value;
    private:
        float reward_points;
} plus;

struct ρrémíùm {
    float get_reward() {
        if (purchased_value < 200) {
            reward_points = 0.04;
        }
        else {
            reward_points = 0.15;;
        }
        return reward_points;
    }
    const std::string name = "ρrémíùm";
    float purchased_value;
    private:
        float reward_points;
} ρrémíùm;

int main(void) {
    short unsigned mem {0};
    float purchased_value {0.00};
    float reward {0.00};
    std::string mem_type;
    std::cout << "\nProgram that computes reward points" << std::endl;
    std::cout << "\nMEMBERSHIP TYPE" << "\n===============" << std::endl;
    std::cout << "1. Standard\n2. Plus\n3. ρrémíùm\nEnter membership type: " << std::flush;
    std::cin >> mem;
    std::cout << "\nPlease enter purchased value: ";
    std::cin >> purchased_value;
    switch (mem) {
        case 1:
            mem_type = standard.name;
            standard.purchased_value = purchased_value;
            reward = standard.get_reward() * purchased_value;
            break;
        case 2:
            mem_type = plus.name;
            plus.purchased_value = purchased_value;
            reward = plus.get_reward() * purchased_value;
            break;
        case 3:
            mem_type = ρrémíùm.name;
            ρrémíùm.purchased_value = purchased_value;
            reward = ρrémíùm.get_reward() * purchased_value;
            break;
    }

    std::cout << "\nYour " << mem_type << " Plan has earned you: " << reward << " points!" << std::endl;
    std::cout << "-----------------------------------------------\n" << std::endl;
    return 0;
}
 
tama naman yung code pero pwede pa na paiksiin yan.
Eto po no errors pero hindi po ako sure sa output niya if na meet ko ba hinihingi ng problem machine.

double membership, value, reward ;

cout << "1. Standard" << endl;
cout << "2. Plus" << endl;
cout << "3. ρrémíùm" << endl;
cout << "Choose membership type: " ;
cin >> membership ;

if (membership==1)
{
cout <<"You've choose Standard Membership. \n";
cout <<"Please enter purchased value: " ;
cin >> value ;
if (value<=75)
{
cout <<"You've earned 5% of the total monthly purchase. \n" ;
reward= value * 0.05 ;
cout << "You've earned: " << reward << "pts." <<endl;

}
else if (value<=149.99)
{
cout << "You've earned 7.5% of the total monthly purchase. \n" ;
reward= value * 0.075 ;
cout << "You've earned: " << reward <<"pts." << endl;
}
else if (value>=150)
{
cout << "You've earned 10% of the total monthly purchase. \n" ;
reward=value* 0.1 ;
cout << "You've earned: " << reward << "pts." <<endl;
}

}

if (membership==2)
{
cout <<"You've choose Plus Membership. \n" ;
cout <<"Please enter purchased value: " ;
cin >> value ;
if (value<=150)
{
cout <<"You've earned 6% of the total monthly purchase. \n" ;
reward= value * 0.06 ;
cout << "You've earned: " << reward <<"pts." << endl;
}
else if (value>=150)
{
cout << "You've earned 13% of the total monthly purchase. \n" ;
reward= value * 0.13 ;
cout << "You've earned: " << reward << "pts." <<endl;
}
}

if (membership==3)
{
cout <<"You've choose ρrémíùm Membership. \n" ;
cout <<"Please enter purchased value: " ;
cin >> value ;
if (value<=200)
{
cout <<"You've earned 4% of the total monthly purchase. \n" ;
reward= value * 0.04 ;
cout << "You've earned: " << reward <<"pts." << endl;
}
else if (value>=200)
{
cout << "You've earned 15% of the total monthly purchase. \n" ;
reward= value * 0.15 ;
cout << "You've earned: " << reward << "pts." << endl;
}
}

eto solution ko ts:
pwede mo rin irun dito: You do not have permission to view the full content of this post. Log in or register now.
C++:
#include <iostream>
#include <string>

using namespace std;

int main()
{
    int membership_type;
    string type_data[] = {"Standard", "Plus", "ρrémíùm"};
    float purchase_value, reward_percentage = 0.0f;
    cout << "Please select Membership Type\n\t1.)Standard\n\t2.)Plus\n\t3.)ρrémíùm\n>> ";
    cin >> membership_type;
    cout << "[You have selected " << type_data[membership_type-1] << " membership]\n";
    cout << "Please enter your total monthly purchase\n>> ";
    cin >> purchase_value;
    if (membership_type == 1) {
        if (purchase_value<75) {
            reward_percentage = 0.05f;
        }
        else if(purchase_value>= 75 && purchase_value < 150) {
            reward_percentage = 0.075f;
        }
        else if(purchase_value >= 150) {
            reward_percentage = 0.1f;
        }
       
    }
    else if (membership_type == 2) {
        reward_percentage = purchase_value < 150 ? 0.1f : 0.06f;
    }
    else if(membership_type == 3) {
        reward_percentage = purchase_value < 200 ? 0.04f : 0.15f;
    }
    cout << "You have earned " << reward_percentage * purchase_value << " points (" << reward_percentage * 100 << "% of " << purchase_value << " monthly purchase)";
}
pasok padin naman siguro yung code ko sa teacher mo kasi gumamit ako ng nested if else na hinahanap nya.
kung makikita mo sa loob ng else if statement ko ternary operator po yung ginamit ko dito. pwede kang mag basa dito about sa ternary operator pag interesado ka You do not have permission to view the full content of this post. Log in or register now.
 
Last edited:
Hmm...

Kapag may train ka ng if-else, walang strong-association between membership_type:purchased_value:reward_points.
Mas tamang approach dyan, i-bundled mo ang bawat categories together with their own attributes.

Isa pa, parang may logic-error sa Plus Plan, ie bakit mas mataas pa reward sa less than 150 kesa sa more than that?

Eto sample code ko, (yung sa Plus Plan binago ko arbitrarily into 16%). BTW, walang input validation dyan at hindi naman iyan primary objective.

Code:
#include <iostream>

struct Standard {
    float get_reward() {
        if (purchased_value < 75) {
            reward_points = 0.05;
        }
        else if (purchased_value > 75 && purchased_value < 149.99) {
            reward_points = 0.075;
        }
        else {
            reward_points = 0.10;;
        }
        return reward_points;
    }

    const std::string name = "STANDARD";
    float purchased_value;
    private:
        float reward_points;
} standard;

struct Plus {
    float get_reward() {
        if (purchased_value < 150) {
            reward_points = 0.10;
        }
        else {
            reward_points = 0.16;;
        }
        return reward_points;
    }
    const std::string name = "PLUS";
    float purchased_value;
    private:
        float reward_points;
} plus;

struct ρrémíùm {
    float get_reward() {
        if (purchased_value < 200) {
            reward_points = 0.04;
        }
        else {
            reward_points = 0.15;;
        }
        return reward_points;
    }
    const std::string name = "ρrémíùm";
    float purchased_value;
    private:
        float reward_points;
} ρrémíùm;

int main(void) {
    short unsigned mem {0};
    float purchased_value {0.00};
    float reward {0.00};
    std::string mem_type;
    std::cout << "\nProgram that computes reward points" << std::endl;
    std::cout << "\nMEMBERSHIP TYPE" << "\n===============" << std::endl;
    std::cout << "1. Standard\n2. Plus\n3. ρrémíùm\nEnter membership type: " << std::flush;
    std::cin >> mem;
    std::cout << "\nPlease enter purchased value: ";
    std::cin >> purchased_value;
    switch (mem) {
        case 1:
            mem_type = standard.name;
            standard.purchased_value = purchased_value;
            reward = standard.get_reward() * purchased_value;
            break;
        case 2:
            mem_type = plus.name;
            plus.purchased_value = purchased_value;
            reward = plus.get_reward() * purchased_value;
            break;
        case 3:
            mem_type = ρrémíùm.name;
            ρrémíùm.purchased_value = purchased_value;
            reward = ρrémíùm.get_reward() * purchased_value;
            break;
    }

    std::cout << "\nYour " << mem_type << " Plan has earned you: " << reward << " points!" << std::endl;
    std::cout << "-----------------------------------------------\n" << std::endl;
    return 0;
}
If else lang po daw gagamitin namin eh... Walang switch or ung float na term... Nasa introduction palang kame. Pero salamat po sa effort sa pag code... Magagamit ko rin to soon thanks po !
 
Status
Not open for further replies.

Similar threads

Back
Top