🔒 Closed C ++ Input country and display its capital

Status
Not open for further replies.

IkaMusume

Forum Guru
Hingi po sana ako tulong regarding dito, nag-aalangan po kasi ako kung tatanggapin ang if else loop sa current topic namin
1651430044319.webp


eto po code
C++:
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    char BANSA[100];
    
    cout << "COUNTRY NAME:  ";
    cin.getline(BANSA,100);
    
    int BRU, CAM, TIM, IND, LAO, MAL, MYA, PHI, SIN, THA, VIE;
    BRU = strcmpi("BRUNEI",BANSA);
    CAM = strcmpi("CAMBODIA",BANSA);
    TIM = strcmpi("EAST TIMOR",BANSA);
    IND = strcmpi("INDONESIA",BANSA);
    LAO = strcmpi("LAOS",BANSA);
    MAL = strcmpi("MALAYSIA",BANSA);
    MYA = strcmpi("MYANMAR",BANSA);
    PHI = strcmpi("PHILIPPINES",BANSA);
    SIN = strcmpi("SINGAPORE",BANSA);
    THA = strcmpi("THAILAND",BANSA);
    VIE = strcmpi("VIETNAM",BANSA);
    
    if (BRU == 0)
    {
        cout << "CAPITAL:  Bandar Seri Begawan";
    }
    else if (CAM == 0)
    {
        cout << "CAPITAL:  Phnom Penh";
    }
    else if (TIM == 0)
    {
        cout << "CAPITAL:  Dili";
    }
    else if (IND == 0)
    {
        cout << "CAPITAL:  Jakarta";
    }
    else if (LAO == 0)
    {
        cout << "CAPITAL:  Vientiane";
    }
    else if (MAL == 0)
    {
        cout << "CAPITAL:  Kuala Lumpur";
    }
    else if (MYA == 0)
    {
        cout << "CAPITAL:  Nay Pyi Daw";
    }
    else if (PHI == 0)
    {
        cout << "CAPITAL:  Manila";
    }
    else if (SIN == 0)
    {
        cout << "CAPITAL:  Singapore";
    }
    else if (THA == 0)
    {
        cout << "CAPITAL:  Bangkok";
    }
    else if (VIE == 0)
    {
        cout << "CAPITAL:  Hanoi";
    }
    else
    {
        cout << BANSA << " is not a South East Asian Country.";
    }
    
    return 0;
}
 
Alternative solution:

C++:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int findCountry(string arr[], int len, string seek)
{
    for (int i = 0; i < len; ++i)
    {
        if (arr[i] == seek) return i;
    }
    return -1;
}

int main(){
    //by dee-u
    string countries[5]={"Brunei", "Cambodia", "East Timor", "Indonesia", "Laos"};
    string capitals[5]={"Bandar Seri Begawan", "Phnom Penh", "Dili", "Jakarta", "Vientiane"};
        
    string country;
    
    cout << "Country: ";
    cin >> country;
    
    int index = findCountry(countries,5,country);
     if (index >= 0)
        cout << "Capital: " << capitals[index];
    else
        cout << "Wala sa listahan";
    
    return 0;
}
 
Alternative solution:

C++:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int findCountry(string arr[], int len, string seek)
{
    for (int i = 0; i < len; ++i)
    {
        if (arr[i] == seek) return i;
    }
    return -1;
}

int main(){
    //by dee-u
    string countries[5]={"Brunei", "Cambodia", "East Timor", "Indonesia", "Laos"};
    string capitals[5]={"Bandar Seri Begawan", "Phnom Penh", "Dili", "Jakarta", "Vientiane"};
       
    string country;
   
    cout << "Country: ";
    cin >> country;
   
    int index = findCountry(countries,5,country);
     if (index >= 0)
        cout << "Capital: " << capitals[index];
    else
        cout << "Wala sa listahan";
   
    return 0;
}
Maraming salamat po, godbless po
 
Alternative solution:

C++:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int findCountry(string arr[], int len, string seek)
{
    for (int i = 0; i < len; ++i)
    {
        if (arr[i] == seek) return i;
    }
    return -1;
}

int main(){
    //by dee-u
    string countries[5]={"Brunei", "Cambodia", "East Timor", "Indonesia", "Laos"};
    string capitals[5]={"Bandar Seri Begawan", "Phnom Penh", "Dili", "Jakarta", "Vientiane"};
       
    string country;
   
    cout << "Country: ";
    cin >> country;
   
    int index = findCountry(countries,5,country);
     if (index >= 0)
        cout << "Capital: " << capitals[index];
    else
        cout << "Wala sa listahan";
   
    return 0;
}
btw po, meron po ba kayong version using strcmpi para pwede siya ma search without case sensitive
 
Status
Not open for further replies.

About this Thread

  • 6
    Replies
  • 1K
    Views
  • 3
    Participants
Last reply from:
dee-u

Online now

Members online
1,020
Guests online
895
Total visitors
1,915

Forum statistics

Threads
2,276,230
Posts
28,968,483
Members
1,231,174
Latest member
jpryno
Back
Top