LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Help with my prog. (https://www.linuxquestions.org/questions/programming-9/help-with-my-prog-356849/)

Slickmink 08-25-2005 09:10 AM

Help with my prog.
 
Hi, I'm new to C++ and I've run into a fair few compile problems trying to make this fairly simple program work. I was wondering if one of you guys could run an experienced eye over it and show me my mistakes.
Code:

#include <iostream>
#include <ctime>
#include <cstdlib>

using std::cout;
using std::endl;

int calculateTotal( int a, int b );

int main()
{
  int score;
  int wins = 0;
  int lose = 0;
 
  srand(time(0));
 
 

  for ( int counter = 1; counter <= 20; counter++ )
  {
    score = 1 + rand() % 6;
    switch ( score ) {
    case '6':
    cout << "You rolled a 6.  Congratulations you won £10" << endl;
    wins++;
    break;
   
    case '3':
      cout << "You rolled a 3.  I'm sorry you lose £5" << endl;
      lose++;
      break;
     
    default:
    cout << "You rolled a " << score << endl;
    break;
    }
  };
   
   
if ( calculateTotal > 0 )
  cout << "Congratulations you have won " << calculateTotal << " Pounds";
  else cout << "sorry you have lost " << calculeteTotal << " Pounds";

 
    b = lose;
  return 0;
}
  int calculateTotal( int a, int b )
  {
  int w;
  int l;
  int calc;
  calc = w * (+10) + l * (-5);
  return 0;
  }

Any help that can be given would be appricated.

Nylex 08-25-2005 09:20 AM

Re: Help with my prog.
 
Quote:

Originally posted by Slickmink
int calculateTotal( int a, int b )
{
int w;
int l;
int calc;
calc = w * (+10) + l * (-5);
return 0;
}

[/code]

Right here, I can see that you haven't initalised your first two integers, w and l. You're passing a and b to your function, but not doing anything with them. You are also simply returning 0. Are you sure that is what you intended? Perhaps you wanted to return the value of the calculation?

Also with your switch statement earlier on, you don't need the '' in case '6' for example, you can just write:

Code:

case 6:
            // do stuff

In your if statement:

Code:

if ( calculateTotal > 0 )
  cout << "Congratulations you have won " << calculateTotal << " Pounds";
  else cout << "sorry you have lost " << calculeteTotal << " Pounds";

You don't have a variable called "calculateTotal", it's a function and as such, you must use brackets. Since your function takes two arguments, you need to pass arguments to it when you call it (unless you were using default values, but in your definition, you're not). So you would call calculateTotal() with something like

Code:

calculateTotal(x, y)
assuming x and y were integer variables that had been previously initialised.

Slickmink 08-25-2005 09:43 AM

Thanks for the help but it still doesnt seem to want to run. I'm not upto speed with the language, would it be possible for you to give me an idiots guide to what I've got wrong.

Nylex 08-25-2005 09:53 AM

Let's start with your function:

Code:

int calculateTotal( int a, int b )
  {
  int w;
  int l;
  int calc;
  calc = w * (+10) + l * (-5);
  return 0;
  }

In the body, what you have done is declared two integer variables, w and l. This tells the compiler to allocate memory for them and because you haven't initalised them, you don't know what is stored in them. Thus, you won't know what value is stored in calc after the calculation happens.

Perhaps what you wanted to do is use the values you pass to the function (a and b) to perform the calculation and then return the result? If so, you would simply write

Code:

int calculateTotal(int a, int b)
{
        int calc = a*10 + b*(-5);          // perform the calculation
        return calc;                        //  return the result
}

Does that make sense? If not, tell us what you want your function to do.

rstewart 08-25-2005 10:03 AM

Quote:

Perhaps what you wanted to do is use the values you pass to the function (a and b) to perform the calculation and then return the result? If so, you would simply write

code:

int calculateTotal(int a, int b)
{
calc = a*10 + b*(-5); // perform the calculation
return calc; // return the result
}
Uh, don't you mean

Code:

int calculateTotal(int a, int b)
{
        int calc;                      // allocate space for the return variable

        calc = a*10 + b*(-5);          // perform the calculation
        return calc;                  //  return the result
}

:D

Nylex 08-25-2005 10:06 AM

Whoops, let me edit my post. Edit: of course, it is unnecessary to use another variable; you could just perform the calculation in the return statement:

Code:

return (a*10 + (-5)*b);

Nylex 08-25-2005 10:41 AM

Heh, I see that you can use the single quotes with case. It compiles fine (as I said though, it isn't necessary to have them there. "case 6: " would do).

So, the next problem is your if statement. You want to check whether the value returned by your function, calculateTotal() is greater than zero. As I said, calculateTotal() is a function and you need to include the brackets. The function takes two arguments and doesn't have default values so you must pass values to it. In your case, you would write:

Code:

if(calculateTotal(wins, lose) > 0)
What you can do is store the value returned by your function in a variable (which must be an int, since calculateTotal() returns an int) and then test that variable for being greater than zero. You could write:

Code:

int money = calculateTotal(wins, lose); // store value returned by calculateTotal in money

if(money > 0)
    cout << Congratulations you have won " << money << " Pounds";
else
    cout << "sorry you have lost " << money << " Pounds";

It just avoids calling the function more than once, when you don't need to.

Edit: also, you can get rid of that "b = lose;" line. Edit again: in relation to said line, you haven't declared b to be an int in main(). The b in your calculateTotal() function is local to that function.

Slickmink 08-25-2005 10:50 AM

Ok, it would probably have made more sence if I had told you what I was attempting.

Quote:

Write a program that simulates a dice rolling game that rolls 20 randomly generated rolls of a die. Every time a 6 is rolled, the player wins £10; every time a 3 is rolled, the player loses £5, any other number is ignored. Write a function calculateTotal that adds up the player's winnings after 20 rolls of the dice. Display the results of the dice roll and the total winnings/loses for each game.


All times are GMT -5. The time now is 02:58 PM.