473,395 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How can I fix this custom dice roll program to work correctly?

I need to write a program that will prompt the user to input a number-sided dice and a number of rolls, and then it will output all of the rolls in a sequence. The program I have written will get the number of rolls correct, but something goes wrong with the number-sided dice. It will output numbers in the 100s for a 6-sided dice. Here's my code, does anyone know how to fix this? (My 'y' integer is the one for the number sided die).
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h> 
  2. #include <time.h> 
  3. int rolldie(); 
  4.  
  5. int main(){ 
  6. int x, n;
  7. double y;
  8. printf ("What sided dice do you want to roll?... %d");
  9. scanf ("%d", &y);
  10. printf("How Many Rolls do You Want?... %d");
  11. scanf("%d", &n);    
  12. srand(time(0));
  13. for(x=0;x<n;x++) 
  14. printf("%d ",rolldie()); 
  15. return 0; 
  16. int rolldie(){ 
  17. return 1 + rand() %'y' ; 
  18. }
  19.  
Oct 12 '14 #1
1 1194
donbock
2,426 Expert 2GB
I'm surprised you're not getting compiler errors and warnings.
  1. The printf calls on lines 8 and 10 each include %d, but do not pass a corresponding argument.
  2. Line 9 uses %d to scanf a value for y, but y is a double. %d is only for ints.
  3. Line 18 in rolldie() references y, but y is not defined within the scope of that function.
  4. Line 18 says "... % y", but the % modulo operator only works properly with integer operands.
  5. lLine 18 says 'y' (note the single quotes). This is the character constant lower-case-y, not the value of a variable.
Oct 12 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Andrew Mogford | last post by:
Hi, We have moved a web site from a development server running Windows 2000 and IIS 5 to a Windows 2003 server running IIS 6.0 Now the asp pages do not work correctly. For example,...
11
by: Bore Biko | last post by:
Dear, I have function that transform time from secconds to string formated as dd:hh:mm:ss (days:hours:minutes:seconds), but it doesent work correctly... Here s code compiled with gcc it goes...
22
by: semedao | last post by:
Hi , I am using asyc sockets p2p connection between 2 clients. when I debug step by step the both sides , i'ts work ok. when I run it , in somepoint (same location in the code) when I want to...
2
by: PleegWat | last post by:
Hi, I'm using this function: function die_quietly( $text='', $title='', $file='', $line='', $sql='' ) { global $wowdb, $roster_conf, $wordings, $roster_menu; // die_quitely died quietly...
13
by: hn.ft.pris | last post by:
Hi: I have the following simple program: #include<iostream> using namespace std; int main(int argc, char* argv){ const double L = 1.234; const int T = static_cast<const int>(L); int arr;
2
by: sunnydude | last post by:
Anybody know how to write a 3 dice rolling program like this Sample output Welcome to the dice roller! First of all, please enter a seed for the random number generator. This should be a...
0
by: ahren | last post by:
Why won't this actionscript program work? http://pastebin.com/xMmzfmmq http://pastebin.com/yMAKu5SH I think this code is from before i fixed some of it, but even when i fixed some, it would...
7
by: Metalman | last post by:
Hello all So what am trying to do is write a program that will simulate the rolling of a single dice using rand() and time.h. 1)The user will input the number of times he wants the dice to be...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.