What's new

Closed Nested loop

Status
Not open for further replies.

PHC-Sabina

Forum Guru
Joined
Sep 4, 2017
Posts
6,177
Reaction
5,863
Points
1,895
Gandang umaga PHC.
Pahelp naman sa project namin oh.
Binigyan kasi kami ni sir ng project na 5 programs with nested looping and conditional statements (with simple input/output) ..ang problema eh dpa kami nakakapagtacle sa nested loop, sariling pag-aaral lang daw, diko sya masyado gets eh. Baka may alam kayong program dyan mga sir/ma'am, pacopy po ng code. Salamuch
 
YøùTùbé par...

Pero para sa akin ..yung nested loop..
Sa loob lang siya ng loop.. Pero pag output na... ganun parin naman yung loop..

Di yan nakakalito.. Dapat intindihin mo maige yung flow ng loop..
 
Marami akong nested loop kaso java at walang gui. Sa console lang yun yung procedural programming namin nung 1st year kami or meron din kaming oop with gui kung trip mo pm moko.
 
same lang kami na new sa c++ tol hahahaha ang ginawa ko nanghingi ako ng code sa mga ka phc natin at pinag aralan ko kaya ko nakagawa ng seatwork ko hahaha si google nalang talaga ang kailangan ngayon at si yt hahaha
baka matulungan mo yan tol code na code ka pa naman hahahah
 
Try nyo po eto.
Code:
#include <iostream>
//PANG GENERATE NG RANDOM NUMBER
#include <stdlib.h>
#include <time.h>

using namespace std;

void program1() {
    char confirm;
    do {
        cout << "\nEnter non-negative numbers\n";
        int total = 0, input;
        for (int i = 1; i <= 3; i++) {
            cout << i << "). Enter a number: ";
            cin >> input;
            if (input < 0)
            {
                cout << "Number must be non-negative\n";
                i = (i - 1) < 1 ? 0 : i - 1;
            }
            else {
                total = total + input;
            }
        }
        cout << "Sum of numbers: " << total << "\n";
        cout << "Would you like to enter another 3 sets of numbers? [y/n]: ";
        cin >> confirm;

    } while (confirm == 'y' || confirm == 'Y');
}

void program2() {
    int star;
    int limit =14;
    cout <<"\nEnter star numbers (max "<< limit << "): ";
    cin >> star;
    if (star > limit)
    {
        cout << "Input exceeded limit(" << limit << ")\n";
    }
    else {
        for (int i = 1; i <= star; i++)
        {
            for (int x = 0; x < i; x++)
            {
                cout << "*";
            }
            cout << "\n";
        }
    }



}

void program3() {
    char confirm = 'y';
    cout << "\nEnter number must be a non-negative and does not exceed 20\n";
    while (confirm == 'y' || confirm == 'Y')
    {
        int x, y;
        cout << "Enter box length: ";
        cin >> x;
        if (x < 0 || x > 20)
        {
            cout << "Invalid Input";
            break;
        }
        cout << "Enter box height: ";
        cin >> y;
        if (y < 0 || y > 20)
        {
            cout << "Invalid Input";
            break;
        }
        for (int i = 0; i < x; i++)
        {
            cout << "*";
        }
        cout << "\n";
        for (int i = 0; i < y-2; i++)
        {
            cout << "*";
            for (int i = 0; i < x-2; i++)
            {
                cout << " ";
            }
            cout << "*\n";         
        } 
        for (int i = 0; i < x; i++)
        {
            cout << "*";
        }
        cout << "\nWould you like to make another square?[y/n]: ";
        cin >> confirm;
    }
 
}

void program4() {

    int number, input, player = 2, maxrand; 
    cout << "Welcome to guess the number! the player who correctly guesses the number wins!\n";
    cout << "Please enter the max number: ";
    cin >> maxrand;
    srand(time(NULL));
    number = rand() % maxrand;
    do {
        cout << "\n";
        if (player == 2)
        {
            cout << "Player 1: ";
            player = 1;
        }
        else {
            cout << "Player 2: ";
            player = 2;
         
        }

        cin >> input;
        if (number < input) {
            cout << "Number is lower!\n";
        }
        else if (number > input) {
            cout << "Number is higher!\n";
        }

    } while (number != input);
 
    cout << "Congratulations! Player " << player << " Won! Yay!!\n=====";

}

int main()
{
    char input, confirm;
    cout << "Welcome to 4-in 1 program simulator!\n";
    do
    {
        do {

            cout << "\nType the letter of your choice: \n";
            cout << "\ta.) Sum of three Integers\n";
            cout << "\tb.) Star generator\n";
            cout << "\tc.) Box virtualizer\n";
            cout << "\td.) Guess the number\n";
            cout << "Your choice: ";
            cin >> input;
            putchar(tolower(input));

            switch (input)
            {
            case 'a':
                program1();
                break;
            case 'b':
                program2();
                break;
            case 'c':
                program3();
                break;
            case 'd':
                program4();
                break;
            default:
                cout << "Invalid input";
                break;
            }

        }while(input == 'a' || 'b' || 'c' || 'd');
        if (input != 'a' || 'b' || 'c' || 'd')
        {
            cout << "\nInvalid input! \n";
        }


        cout << "Would you like to play another one? [y/n]: ";
        cin >> confirm;
        putchar(tolower(confirm));
    } while (confirm == 'y');
    return 0;
}

output:
577881_401.jpg


Mga ginamit ko. Read mo din kung gusto mo:
 

Attachments

Last edited:
Status
Not open for further replies.

Similar threads

Back
Top