Sponsored Content
Top Forums Programming C++ Bug probably scope problem Post 302914891 by yifangt on Thursday 28th of August 2014 03:27:33 PM
Old 08-28-2014
C++ Bug probably scope problem

Hello,
I have some difficulty to understand the scope of this program adapted from C++ book of D.S. Malik I am reading.
I put the header and the main program as attachment to save space. My problem is the output:
Code:
$ ./prog1009_die
Line 4: Not yet rolled, die1 gets default number: 1
Line 5: Not yet rolled, die2 gets default number: 1

Line 6: After first rolling die1: 4
Line 7: After first rolling die2: 5
Line 8: The sum of the numbers rolled by the dice is: 9

Line 6-2: After second rolling die1: 2
Line 7-2: After second rolling die2: 4

Line 9: After rolling again (third time), 
die1 (die1.roll()): 5
die2 (die2.roll()): 4
the sum of the numbers rolled [die1.getNum() + die2.getNum()] is: 8
the sum of the numbers rolled [die1.roll() + die2.roll()] is: 8

Line 9-2: After rolling again (third time)
die1 (die1.getNum()): 5
die2 (die2.getNum()): 4
the sum of the numbers rolled [die1.getNum() + die2.getNum()] is: 9

I was expecting Line 9 part would be the same result as Line 9-2 part, but the sum of the two numbers is not right. It is difficult for me to understand why. The problem may be related to die::roll() and die::getNum() functions in this case, but I am not sure.
Code:
int die::roll()
{
    num = rand() % 6 + 1;
    return num;
}

int die::getNum() const
{
    return num;
}

Also I am aware the rand() function here so each time the number will change. Can somebody elaborate it for me? Thanks a lot!

Last edited by yifangt; 08-28-2014 at 04:29 PM.. Reason: typos
 

9 More Discussions You Might Find Interesting

1. Programming

C++: scope, different files etc..

I'm having a problem getting this to work.. I got 3 files, start.C - Where i got my main() function Menu.C & Menu.h - Where I'm trying to use hash_map start.C #include <iostream> #include "Menu.h" using namespace std; int main() { /* test code here */ return 0; } Menu.h ... (1 Reply)
Discussion started by: J.P
1 Replies

2. Programming

scope

Each thread has a copy of auto variables within a function, but variables declared as static within a function are common to all threads. To circumvent this can static variables be placed outside the function. If so, will the scope of the variable be file only or will it be extern, and will each... (7 Replies)
Discussion started by: sundaresh
7 Replies

3. AIX

Scope of AIX

What is the scope of AIX as I am starting my career as a fresher in AIX administration?? (4 Replies)
Discussion started by: abhishek27
4 Replies

4. Shell Programming and Scripting

Variables scope.

Hi , I'm trying to change the variable value in a while loop , however its not working it seems that the problem with subshells while reading the file. #!/bin/sh FLAG=0; cat filename | while read data do FLAG=1; done echo $FLAG Should display 1 instead displays 0 (13 Replies)
Discussion started by: dinjo_jo
13 Replies

5. Shell Programming and Scripting

problem with shell variable's scope

Hi, I am stuck while developing a shell sub-routine which checks the log file for "success" or "failure". The subroutine reads the log file and checks for key word "success", if found it set the variable (found=1). It returns success or failure based on this variable. My problem is, I can... (2 Replies)
Discussion started by: cjjoy
2 Replies

6. Shell Programming and Scripting

variable scope

Hi, I want to know about the variable scope in shell script. How can we use the script argument inside the function? fn () { echo $1 ## I want this argument should be the main script argument and not the funtion argument. } also are there any local,global types in shell script? if... (3 Replies)
Discussion started by: shellwell
3 Replies

7. High Performance Computing

MPI_Bcast problem, bug?

Hi, I'm trying to define an MPI_datatype for a structure, then do message passing for this created datatype. However, when I tried to broadcast the initialized data from rank 0, I found that part of the received data at other ranks are not correct. Could you please help me take a look at my code... (0 Replies)
Discussion started by: qb13
0 Replies

8. Shell Programming and Scripting

Bourne Shell - Problem with while loop variable scope.

Hello I am having issues with a script I'm working on developing on a Solaris machine. The script is intended to find out how many times a particular user (by given userid) has logged into the local system for more than one hour today. Here is my while loop: last $user | grep -v 'sshd'... (7 Replies)
Discussion started by: DaveRich
7 Replies

9. Shell Programming and Scripting

PERL annoying scope problem

Hello, I met a problem with following code: #!/usr/bin/perl -w # test.pl use strict; use diagnostics; use DBI; my $dbh = DBI->connect( "DBI:mysql:BibleBook","yifangt","password") or die("Cannot connect: $DBI::errstr"); my $sql = qq(SELECT * FROM library WHERE isbn =... (2 Replies)
Discussion started by: yifangt
2 Replies
ROLLDICE(6)							   Games Manual 						       ROLLDICE(6)

NAME
rolldice - rolls virtual dice SYNOPSIS
rolldice [ options ] dice_string dice_string ... DESCRIPTION
rolldice rolls virtual dice. The dice strings passed on the command line contain information on the dice to roll in a format comparable to the format used in fantasy role playing games. OPTIONS
-h,--help returns the usage of diceroll -v,--version returns the version of diceroll -r,--random uses /dev/random for random number generating -u,--urandom uses /dev/urandom for random number generating (default) -s,--separate prints out the result of each individual die separately, as well as the operations and totals DICE STRING FORMAT
The dice string uses the following format: {#x}{#}d[#|%]{*#}{+/-#}{s#} The dice string doesn't have to be in the format that I outlined above, but this is the nicest order for me. It will try to parse any dif- ferent string containing the same sections in the best way it can, and will throw out anything that isn't one of the sections below. {#}d[#|%] This is the only required part of the string, and now really isn't even required, as it defaults to 1d6 if not found. The first number is the number of dice to roll, and the second number is the number of sides the dice have. The numbers rolled on each die are then added up and given as the result. Hence 3d6 means "roll three six-sided dice, add them together and return the result". If the first number is left out, then the number of dice defaults to 1. If the second number is not a number, but a percentage sign, then the number of sides becomes 100 (for a percentage roll). {#x} This number tells how many times to roll. For example, if you want to roll 3 6-sided dice 6 times, you would say 6x3d6. This would return six numbers, corresponding to the six different rolls. {*#} This number tells how many times to multiply the result of each roll. 3d6*100 would return a number in the range of 300-1800, since 3-18 is the range resulting from 3d6, which is then multipled by 100. {+/-#} This number is the modifier to be added or subtracted, depending on the sign, from each roll. 1d4+1 would result in a range from 2-5 (1-4 for the die, plus 1). This step is handled *after* the multiplication modifier. {s#} This number tells the program to drop the # lowest dice rolls. This step is handled *before* the multiplication modifier. in the order show above. For an extreme example, "3x4d6*5+1s2" would roll four six-sided dice, drop the lowest two, multiply the result by 5, add 1 to that, and repeat the process two more times, for a total of three results. DIAGNOSTICS
The following error messages may appear on STDERR: Failure in getting dice attributes Either memory could not be allocated while parsing the string passed to the rolldice program or an invalid dice string was entered. Failure in rolling dice Memory could not be allocated while actually rolling the dice. Unknown option Take a wild guess ;) BUGS
I haven't noticed any yet... rolldice is a quick hack to provide a virtual dice roller for FRPGs that I play. Any improvements are greatly welcome, as I am not the most experienced C coder... AUTHOR
Stevie Strickland <sstrickl@cc.gatech.edu> VERSION
1.10 - 25 Nov 2001 Linux 25 Nov 2001 ROLLDICE(6)
All times are GMT -4. The time now is 12:54 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy