ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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;
}
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.
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.
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.
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
}
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:
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.