Okay, so I am having a few problems with my computer science lab. Below is what I have so far. I am almost positive that the equations are correct, but for some reason I keep getting a different answer than the cs labs example program. So obviously something is wrong. Could you please help me find the problem and solve it?

My imput for my program and the example program has been this.

N
2H
Y
10

Now the difference is that my output is this.

2 H by North, making 10 for 280 points

And the example programs output is this.

2 H by North, making 4 for 170 points

#include <iostream>
#include <string>

using namespace std;

bool isGame (int, string);

int main()
{
string trump;
int score, contract, numTricks;
char winner, vuln;
bool win = true;

cout << "Who won the bidding? " "(N/S/E/W): ";
cin >> winner;

if(winner!='N' && winner!='S' && winner!='E'&& winner !='W')
{
cout << "Directions must be N, S, E, or W.\n";
return 0;
}


cout << "What won the contract? " "(Such as 2 H or 3 NT): ";

cin >> contract;
cin >> trump;

if (trump!="NT" && trump!="S" && trump!="A" && trump!="D" && trump!="H"){

cout << "The contract's suit must be NT, S, H, D or A.\n";
return 0;
}

if(contract > 7||contract < 1){
cout << "Contract is wrong.\n";
return 0;
}

cout << "Was the partnership vulnerable? (Y/N): ";
cin >> vuln;

if(vuln != 'Y' && vuln != 'N'){
cout << "Vulnerability must be Y or N.";
return 0;
}

//check if we won enough tricks

cout << "How many tricks were taken? ";
cin >> numTricks;


if((contract+6) > numTricks)
{
win = false; // we lost so update our win boolean
if (vuln == 'Y')
{
score= -100*((contract+6)-numTricks);// do calculations for this case
}
else
{
score= -50*((contract+6)-numTricks);// do calcs for no case

}
}
else // we won calculate score for winning
{
if (vuln == 'N')
{
score= 20*(numTricks-6);
}

else (trump == "S" || trump == "H"|| trump == "A" || trump == "D");{//Won Enough)
score= 30*(numTricks-6);
}

if (trump=="NT");{
score=20*((numTricks-6)+(10));
}
}

cout << contract << " " << trump << " by";

if ( winner == 'N') {cout << " North, ";}
if ( winner == 'S') {cout << " South, ";}
if ( winner == 'W') {cout << " West, ";}
if ( winner == 'E') {cout << " East, ";}

if (win)
{
cout << "making";
}
else
{
cout << " down";
}

cout << " " << numTricks <<" for" << " " << score << " " << "points\n";

return 0;
}

Recommended Answers

All 4 Replies

Here it is properly indented.

#include <iostream>
#include <string>

using namespace std;

bool isGame (int, string);

int main()
{
    string trump;
    int score, contract, numTricks;
    char winner, vuln;
    bool win = true;

    cout << "Who won the bidding? " "(N/S/E/W): ";
    cin >> winner;

    if(winner!='N' && winner!='S' && winner!='E'&& winner !='W')

    {
        cout <<  "Directions must be N, S, E, or W.\n";
        return 0;





        cout << "What won the contract? " "(Such as 2 H or 3 NT): ";


        cin >> contract;

        cin >> trump;

        if (trump!="NT" && trump!="S" && trump!="A" && trump!="D" && trump!="H"){

            cout << "The contract's suit must be NT, S, H, D or A.\n";

            return 0;
    }


        if(contract > 7||contract < 1){

            cout << "Contract is wrong.\n";

            return 0;
        }


        cout << "Was the partnership vulnerable? (Y/N): ";
   cin >> vuln;


        if(vuln != 'Y' && vuln != 'N'){

            cout << "Vulnerability must be Y or N.";

            return 0;
        }

    //check if we won enough tricks


        cout << "How many tricks were taken? ";

        cin >> numTricks;



        if((contract+6) > numTricks)
        {

            win = false; // we lost so update our win boolean
        }

        if (vuln == 'Y')
        {

            score= -100*((contract+6)-numTricks);// do calculations for this case
        }

        else
        {
            score= -50*((contract+6)-numTricks);// do calcs for no case

        }
  

    else  //  we won calculate score for winning

    {
        if (vuln == 'N')
        {
            score= 20*(numTricks-6);
        }

        else (trump == "S" || trump == "H"|| trump == "A" || trump == "D");{//Won Enough)
            score= 30*(numTricks-6);
            }
    if (trump=="NT");{
            score=20*((numTricks-6)+(10));
            }



    }

    cout << contract << " " << trump << " by";

    if ( winner == 'N') {cout << " North, ";}
    if ( winner == 'S') {cout << " South, ";}
    if ( winner == 'W') {cout << " West, ";}
    if ( winner == 'E') {cout << " East, ";}



    if (win)
    {
        cout << "making";
    }
    else
    {
        cout << " down";
    }

    cout << " " << numTricks <<" for" << " " << score << " " << "points\n";

    return 0;
}

Extraneous semicolons are fouling up your logic. Remove them.

else (trump == "S" || trump == "H"|| trump == "A"
            || trump == "D");{ //Won Enough
        score= 30*(numTricks-6);
    }
    if (trump=="NT");{
        score=20*((numTricks-6)+(10));
    }
commented: well spotted +5

First, well spotted nucleon for the additional semi-colons
But there are other horrible errors here.

First off I am surprised that you don't ask about the doubled/redoubled status, it affects scoring greatly.

Second, you should use any of the editors that lay out the code/or do it by hand AND used GNU style or any of the styles that line up the { }.

Then you would see for example, that you initial test if(winner!='N' && winner!='S' && winner!='E' ... doe not close after the comment and the return. Let me write it out how my editor sets it out.

if(winner!='N' && winner!='S' && winner!='E'&& winner !='W')
    {
      cout <<  "Directions must be N, S, E, or W.\n";
      return 0;
      // The line below is indented to far: [forgotten } above] 
      cout << "What won the contract? " "(Such as 2 H or 3 NT): ";

So you see that the next question is already in the loop.

Thirdly, you are drowing in miles of code in one function. First write the code WITHOUT all the checks in for "if the input is correct". THEN when it works for good input, go back and put in the checks. What you will then find that you can then read the logic of the code and the layout will show you the error of you logic.

Finally, before putting back the test, think about writing a function to do the checks. It will make the code clearer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.