First time question

Joined
Dec 13, 2022
Messages
1
Reaction score
0
Im new to coding and this is my assignment, my question is why both of the if else segment doesn't count out when I run the program.

#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int state, vote = 0, sabah = 0, sarawak = 0, labuan = 0, packageA = 0, packageB = 0;
char package, select;
cout << "The Recreation Club, Kampus Kuala Terengganu\n\n"
"Given the following states:\n"
"[1] SABAH\n"
"[2] SARAWAK\n"
"[3] LABUAN\n\n"
"Choose the state you prefer to go (1/2/3): ";
cin >> state;
if (state == 1)
vote++, sabah++;
else if (state == 2)
vote++, sarawak++;
else if (state == 3)
vote++, labuan++;
cout << "\nPACKAGE REMARK COST (RM) PER PAX\n"
"A Inclusive all 1600\n"
"B Exclusive food 899\n\n"
"Choose your package: ";
cin >> package;
if (package == 'A' || package == 'a')
packageA++;
else if (package == 'B' || package == 'b')
packageB++;
cout << "\nContinue with the next person (Y/N): ";
cin >> select;
while (select != 'N' && select != 'n')
{
cout << "\n[1] SABAH\n"
"[2] SARAWAK\n"
"[3] LABUAN\n\n"
"\nChoose the state you prefer to go (1/2/3): ";
cin >> state;
if (state == 1)
vote++, sabah++;
else if (state == 2)
vote++, sarawak++;
else if (state == 3)
vote++, labuan++;
cout << "\nPACKAGE REMARK COST (RM) PER PAX\n"
"A Inclusive all 1600\n"
"B Exclusive food 899\n\n"
"\nChoose your package: ";
cin >> package;
if (package == 'A' || package == 'a')
packageA++;
else if (package == 'B' || package == 'b')
packageB++;
cout << "\nContinue with the next person (Y/N): ";
cin >> select;
}
cout << "\nThe ranking summary\n"
"--------------------\n"
"Number of voters: " <<vote<< "";
{
if ((sabah >> sarawak && sarawak >> labuan) || (sabah >> sarawak && sarawak == labuan))
cout << "\n\nFirst choice : Sabah (voted by " <<sabah<< " people)\n"
"Second choice : Sarawak (voted by " <<sarawak<< " people)\n"
"Third choice: Labuan (voted by " <<labuan<< " people)\n\n";
else if (sabah >> labuan && labuan >> sarawak)
cout << "\n\nFirst choice : Sabah (voted by " <<sabah<< " people)\n"
"Second choice : Labuan (voted by " <<labuan<< " people)\n"
"Third choice: Sarawak (voted by " <<sarawak<< " people)\n\n";
else if ((sarawak >> sabah && sabah >> labuan) || (sarawak >> sabah && sabah == labuan))
cout << "\n\nFirst choice : Sarawak (voted by " <<sarawak<< " people)\n"
"Second choice : Sabah (voted by " <<sabah<< " people)\n"
"Third choice: Labuan (voted by " <<labuan<< " people)\n\n";
else if (sarawak >> labuan && labuan >> sabah)
cout << "\n\nFirst choice : Sarawak (voted by " <<sarawak<< " people)\n"
"Second choice : Labuan (voted by " <<labuan<< " people)\n"
"Third choice: Sabah (voted by " <<sabah<< " people)\n\n";
else if ((labuan >> sabah && sabah >> sarawak) || (labuan >> sabah && sabah == sarawak))
cout << "\n\nFirst choice : Labuan (voted by " <<labuan<< " people)\n"
"Second choice : Sabah (voted by " <<sabah<< " people)\n"
"Third choice: Sarawak (voted by " <<sarawak<< " people)\n\n";
else if (labuan >> sarawak && sarawak >> sabah)
cout << "\n\nFirst choice : Labuan (voted by " <<labuan<< " people)\n"
"Second choice : Sarawak (voted by " <<sarawak<< " people)\n"
"Third choice: Sabah (voted by " <<sabah<< " people)\n\n";
else if ((sabah == sarawak && sabah >> labuan) || (sabah == labuan && sabah >> sarawak) || (labuan == sarawak && labuan >> sabah))
cout << "\n\nThere is a draw for first choice. Please redo the vote.\n\n";
}
cout << "Package Preference\n"
"--------------------\n"
"Package A: Preferred by " <<packageA<< " people\n"
"Package B: Preferred by " <<packageB<< " people";
{
if (packageA >= 10 && packageA >> packageB)
cout << "\n\nA group discount of 10% is applicable for Package A. The cost per person for Package A is now RM1440.\n\n";
else if (packageA << 10 && packageA >> packageB)
cout << "\n\nThe cost per person for Package A is RM1600.\n\n";
else if (packageB >= 10 && packageB >> packageA)
cout << "\n\nA group discount of 10% is applicable for Package B. The cost per person for Package B is now RM809.10.\n\n";
else if (packageB << 10 && packageB >> packageA)
cout << "\n\nThe cost per person for Package B is RM899.\n\n";
else if (packageA == packageB)
cout << "\n\nThere is a draw in the voting. Please redo the vote.\n\n";
}
system ("pause");
return 0;
}
 
Joined
Jan 30, 2023
Messages
107
Reaction score
13
It appears that the problem is in the ranking summary section. If two states have the same vote count, then neither of them will be ranked first. To fix this, you can add an additional condition for each pair of states to check if they have the same vote count. If so, you can list both of them as having the same ranking. Here's an example:

Swift:
cout << "\nThe ranking summary\n"
"--------------------\n"
"Number of voters: " <<vote<< "";

if (sabah > sarawak && sabah > labuan) {
    cout << "\n\nFirst choice : Sabah (voted by " <<sabah<< " people)\n";
    if (sarawak > labuan) {
        cout << "Second choice : Sarawak (voted by " <<sarawak<< " people)\n"
        "Third choice: Labuan (voted by " <<labuan<< " people)\n\n";
    } else if (sarawak == labuan) {
        cout << "Second choice : Sarawak (voted by " <<sarawak<< " people)\n"
        "Second choice : Labuan (voted by " <<labuan<< " people)\n\n";
    } else {
        cout << "Second choice : Labuan (voted by " <<labuan<< " people)\n"
        "Third choice: Sarawak (voted by " <<sarawak<< " people)\n\n";
    }
} else if (sarawak > sabah && sarawak > labuan) {
    cout << "\n\nFirst choice : Sarawak (voted by " <<sarawak<< " people)\n";
    if (sabah > labuan) {
        cout << "Second choice : Sabah (voted by " <<sabah<< " people)\n"
        "Third choice: Labuan (voted by " <<labuan<< " people)\n\n";
    } else if (sabah == labuan) {
        cout << "Second choice : Sabah (voted by " <<sabah<< " people)\n"
        "Second choice : Labuan (voted by " <<labuan<< " people)\n\n";
    } else {
        cout << "Second choice : Labuan (voted by " <<labuan<< " people)\n"
        "Third choice: Sabah (voted by " <<sabah<< " people)\n\n";
    }
} else if (labuan > sabah && labuan > sarawak) {
    cout << "\n\nFirst choice : Labuan (voted by " <<labuan<< " people)\n";
    if (sabah > sarawak) {
        cout << "Second choice : Sabah (voted by " <<sabah<< " people)\n"
        "Third choice: Sarawak (voted by " <<sarawak<< " people)\n\n";
    } else if (sabah == sarawak) {
        cout << "Second choice : Sabah (voted by " <<sabah<< " people)\n"
        "Second choice : Sarawak (voted by " <<sarawak<< " people)\n\n";
    } else {
        cout << "Second choice
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top