What's new

Closed C++ Nested if else

Status
Not open for further replies.

Welcome Guest

Eternal Poster
Joined
Nov 29, 2015
Posts
1,000
Reaction
146
Points
509
Age
27
Mga aydol yung magagaling dyan sa c++ programming. Paki solve naman tong machine problem using nested if else lang. Para po mapag aralan ko codes nyo kung pano ung flow niya...
 

Attachments

parang ganito ata yan t.s :D

1572013577030.png
 

Attachments

Pa check naman po ng codes ko if tama... if may mali paki correct nalang po ako please.
MY CODES:

#include <iostream>
using namespace std;

int main ()
{

int op;
cout << "Select an operation:\n Press 1 for Addition \n Press 2 for Subtraction \n Press 3 For Multiplication \n Press 4 for Division \n:";
cin >> op ;
cout << endl;

int num1, den1;
cout <<"Numerator: ";
cin >> num1;
cout <<"Denominator: ";
cin >> den1 ;
cout << endl;

int num2, den2;
cout <<"Numerator: ";
cin >> num2;
cout <<"Denominator: ";
cin >> den2;

int ans1;
if (op==1)
{
ans1 = (num1*den2) + (num2*den1);
cout << endl;
cout << "Equals to: " ;
cout << ans1;
cout << "/";
ans1 = den1*den2;
cout << ans1 << endl;
}

int ans2;
if (op==2)
{
ans2 = (num1*den2) - (num2*den1);
cout << endl;
cout << "Equals to: " ;
cout << ans2;
cout << "/";
ans2 = den1*den2;
cout << ans2 << endl;
}

int ans3;
if (op==3)
{
ans3 = (num1*num2) ;
cout << endl;
cout << "Equals to: ";
cout << ans3;
cout << "/";
ans3 = den1*den2;
cout << ans3 << endl;
}

int ans4;
if (op==4)
{
ans4 = num1*den2;
cout << endl;
cout << "Equals to: ";
cout << ans4;
cout << "/";
ans4 = num2 * den1;
cout << ans4 << endl;
}

return 0;
}
 
For a starter, using only (nested) ifs is quite a silly idea because you will end up with if-blocks longer than a train. Regardless, to satisfy the requirements, here's my silly code. (ADDITION is the only one implemented. I'll let you do the rest.)

C++:
#include <iostream>
#ifdef _WIN32
#define CLEAR "cls"
#elif __linux__
#define CLEAR "clear"
#endif

int main(void) {

    int operation, num1a, num1b, num2a, num2b;
    std::string menu { "\nOPERATIONS\n----------\n 1) Addition\n 2) Subtraction\n 3) Multiplication\n 4) Division\nEnter operation: " };
    std::cout << menu;
    std::cin >> operation ;
    while (operation < 1 || operation > 4) {
        system(CLEAR);
        std::cout << "\nINVALID INPUT. PLEASE RE-ENTER\n------------------------------\n" << std::endl;
        std::cout << menu;
        std::cin >> operation;
    }

    if (operation == 1) {
        int sum, num1a, num1b, num2a, num2b;
        float quotient1, quotient2;
        // FIRST FRACTION SET
        std::cout << "\nPerforming Addition\n-------------------\n\nFIRST FRACTION SET" << std::endl;
        std::cout << "Enter num1: ";
        std::cin >> num1a;
        std::cout << "Enter num2: ";
        std::cin >> num2a;
        while (num2a == 0) {
            std::cout << "\nERROR! DIVISION BY ZERO!" << std::endl;
            std::cout << "Please re-enter num2: ";
            std::cin >> num2a;
        }
        quotient1 = (float) num1a / (float) num2a;
    
        // SECOND FRACTION SET
        std::cout << "\nSECOND FRACTION SET" << std::endl;
        std::cout << "Enter num1: ";
        std::cin >> num1b;
        std::cout << "Enter num2: ";
        std::cin >> num2b;
        while (num2b == 0) {
            std::cout << "\nERROR! DIVISION BY ZERO!" << std::endl;
            std::cout << "Please re-enter num2: ";
            std::cin >> num2b;
        }
        quotient2 = (float) num1b / (float) num2b;
        std::cout << "\nThe SUM of " << num1a << "/" << num2a << " and " <<  num1b << "/" << num2b << " is " << quotient1+quotient2 << "\n" << std::endl;

    }
   return 0;
}
 
djdoolky76 , ganda sir ng flow-chart mo. Pero sa switch block, yung default value won't be hit kasi sa menu selection palang pre-filtered na yung input (=>1 && <=5), so it's kinda redundant. What do you think?
 
For a starter, using only (nested) ifs is quite a silly idea because you will end up with if-blocks longer than a train. Regardless, to satisfy the requirements, here's my silly code. (ADDITION is the only one implemented. I'll let you do the rest.)

C++:
#include <iostream>
#ifdef _WIN32
#define CLEAR "cls"
#elif __linux__
#define CLEAR "clear"
#endif

int main(void) {

    int operation, num1a, num1b, num2a, num2b;
    std::string menu { "\nOPERATIONS\n----------\n 1) Addition\n 2) Subtraction\n 3) Multiplication\n 4) Division\nEnter operation: " };
    std::cout << menu;
    std::cin >> operation ;
    while (operation < 1 || operation > 4) {
        system(CLEAR);
        std::cout << "\nINVALID INPUT. PLEASE RE-ENTER\n------------------------------\n" << std::endl;
        std::cout << menu;
        std::cin >> operation;
    }

    if (operation == 1) {
        int sum, num1a, num1b, num2a, num2b;
        float quotient1, quotient2;
        // FIRST FRACTION SET
        std::cout << "\nPerforming Addition\n-------------------\n\nFIRST FRACTION SET" << std::endl;
        std::cout << "Enter num1: ";
        std::cin >> num1a;
        std::cout << "Enter num2: ";
        std::cin >> num2a;
        while (num2a == 0) {
            std::cout << "\nERROR! DIVISION BY ZERO!" << std::endl;
            std::cout << "Please re-enter num2: ";
            std::cin >> num2a;
        }
        quotient1 = (float) num1a / (float) num2a;
   
        // SECOND FRACTION SET
        std::cout << "\nSECOND FRACTION SET" << std::endl;
        std::cout << "Enter num1: ";
        std::cin >> num1b;
        std::cout << "Enter num2: ";
        std::cin >> num2b;
        while (num2b == 0) {
            std::cout << "\nERROR! DIVISION BY ZERO!" << std::endl;
            std::cout << "Please re-enter num2: ";
            std::cin >> num2b;
        }
        quotient2 = (float) num1b / (float) num2b;
        std::cout << "\nThe SUM of " << num1a << "/" << num2a << " and " <<  num1b << "/" << num2b << " is " << quotient1+quotient2 << "\n" << std::endl;

    }
   return 0;
}
Thanks po dito sir... But i can't use your code kasi wala pa kami sa void and mga while loop or loops. Pero magagamit ko din to later on. Salamat po sa codes thank you sa effor aydol.
 
Status
Not open for further replies.

Similar threads

Back
Top