LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-25-2005, 09:10 AM   #1
Slickmink
LQ Newbie
 
Registered: Aug 2005
Posts: 3

Rep: Reputation: 0
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.
 
Old 08-25-2005, 09:20 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
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.
 
Old 08-25-2005, 09:43 AM   #3
Slickmink
LQ Newbie
 
Registered: Aug 2005
Posts: 3

Original Poster
Rep: Reputation: 0
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.
 
Old 08-25-2005, 09:53 AM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
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.

Last edited by Nylex; 08-25-2005 at 10:07 AM.
 
Old 08-25-2005, 10:03 AM   #5
rstewart
Member
 
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 38
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
}
 
Old 08-25-2005, 10:06 AM   #6
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
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);

Last edited by Nylex; 08-25-2005 at 10:09 AM.
 
Old 08-25-2005, 10:41 AM   #7
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
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.

Last edited by Nylex; 08-25-2005 at 10:45 AM.
 
Old 08-25-2005, 10:50 AM   #8
Slickmink
LQ Newbie
 
Registered: Aug 2005
Posts: 3

Original Poster
Rep: Reputation: 0
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cd prog ahmed gamal Linux - Software 1 11-14-2005 06:55 AM
I want to start a prog from another prog but not as child grupoapunte Programming 5 05-23-2005 05:37 PM
[C prog] how to do this? dmigh Programming 7 10-31-2004 03:45 AM
Better buying "advanced linux prog" or "unix advanced prog" Dominik Programming 3 12-31-2003 01:11 PM
Can anyone help me with a C prog I wrote? WorldBuilder Linux - Software 10 10-23-2003 09:11 AM


All times are GMT -5. The time now is 12:18 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration