Add and print multiple char in C

I'm creating a Dice Game, where the user rolls 3 dice and gets some random outputs (up to integer 6). My next step is to add those 3 values obtained and get its sum. How do I achieve this? Any suggestion would be helpful.

Here's my source code:

//Roll-a-dice Game! int main(){ char input1; char input2; char input3; int i; int diceRoll; int sumDice = (int)(input1-'0') + (int)(input2-'0') + (int)(input3-'0'); printf("User's First Input: (Press any key to continue) "); scanf("\n %c", &input1); for(i=0;i<1;i++){ diceRoll = (rand()%6) + 1; printf("%d\n\n", diceRoll); } printf("User's Second Input: (Press any key to continue) "); scanf("\n %c", &input2); for(i=0;i<1;i++){ diceRoll = (rand()%6) + 1; printf("%d\n\n", diceRoll); } printf("User's Third Input: (Press any key to continue) "); scanf("\n %c", &input3); for(i=0;i<1;i++){ diceRoll = (rand()%6) + 1; printf("%d\n\n", diceRoll); } printf("Sum of observations: %d", sumDice); return 0; }

The sumDice function doesn't output the correct answer and I think the problem lies somewhere in this particular function.

-------------Problems Reply------------

Judging by this statment:

where the user rolls 3 dice and gets some random outputs (up to integer 6). My next step is to add those 3 values obtained and get its sum

I assume you want this:

first initialize sumDice to 0:

int sumDice=0;

then, in every for loop add this after diceRoll=rand()... line:


and remove the last sumDice= (int)(input-'0')... line.

The logic behind int sumDice = (int)(input1-'0') + (int)(input2-'0') + (int)(input3-'0'); makes sense, but you must use it after assigning values to input1, input2 and input3.

You are getting the wrong answer because when you initialize sumDice at the beginning, the input variables are not initialized, so they have random values stored in them.

Here is where you must put it :

int sumDice = (int)(input1-'0') + (int)(input2-'0') + (int)(input3-'0');
printf("Sum of observations: %d", sumDice);

Category:c# Views:5 Time:2018-04-13
Tags: int scanf char add

Related post

  • add and remove multiple classes in jquery 2011-08-25

    I'm trying to add and remove multiple classes on a text field by clicking different radio buttons. I'm not able to remove the unwanted classes while switching between different radio buttons. My code for this is: //For 1st radio button if(actionUrl =

  • Merging with Word from an Access query and printing multiple documents 2012-08-06

    Below is the VBA code from my Access form. I am reading the results of a query, merging with Word, and printing the word document. I need to loop through the query until all the records are read and the word documents printed, then close all the Word

  • How do I add and remove multiple "belongs_to" instances to and from a "has_many" instance? 2010-05-19

    I currently have two models: Campaigns and Videos. Videos belongs to Campaigns and a Campaign has many Videos. In my Campaign form I want to be able to add Videos that have no parent and also be able to remove Videos that belong to the selected campa

  • Mail Berge and Printing multiple labels 2012-10-07

    I have a list of about 200 contacts. Using AVERY 8371 Business card paper, I would like to be able to print a whole page of labels for each contact. I would to be able to merge the whole list of contacts at once and not have to select and print each

  • How to add and print two photos on one page 2013-09-03

    I want to print 2 photos on one paper, but I cannot figure out how to add the other photo to the page. Anyone know, or could tell me how to add a second photo to the page? Thank-you. Dustin- --------------Solutions------------- Hi, Which operating sy

  • PHP and MySQL: Store and print hex char 2010-09-10

    I'm trying to store an hexadecimal value (\xC1) in MySql Database. When I print that with PHP, i get: xC1lcool instead of the right word. INSERT INTO veiculos VALUES (1, 'Ford', 'Ka RoCam Flex', 25850.00, 1998, 1999, 'Vermelho', 15000, '\xC1lcool;Gas

  • opening and printing multiple crystal reports from a windows app 2011-09-09

    I am going to be building a windows forms application that will essentially queue up and print off several crystal reports. Really at this point, I just need some direction of a way to go forward with this. What is the best way to open a crystal repo

  • How can i add and remove multiple pages in horizontal viewpager for android? 2011-11-11

    here i have one application in which i used viewpager. i want to add and remove views from pageadapter dynamically with programming. now i add only 4 views static to this PagerAdapter. private ArrayList<LinearLayout> views; public MyPagerAdapte

  • Ruby match and print multiple strings in data 2012-03-04

    I have a hashes.xml on a location on the web. I want to parse it for the fields name='hash'><string>78235A2449BA7188CBF95F7DD2D40A36</string>, the file has many fields with this pattern (the MD5 hash is only an example, they differ in

  • Match and print multiple lines with sed 2012-02-22

    I'm trying to match the following: A pattern begins with /* and ends with */\n(override\s+)public, tried doing something like this: sed -n '/\/\*/,/\*\/\n(override\s+)public/p' file And here I am asking you how to write that properly :) If it's possi

  • Add and remove multiple views 2015-03-01

    Currently I am trying to add multiple instances of the same view to a viewgroup, I want to give my users the ability to remove these added views if necessary, but I am not sure how to reference each view individually. How can I keep track of each vie

  • Force page break in HTML/ASP when opening and printing multiple records 2011-01-20

    So I have a repeater control that lists a bunch of information for each staff after another. Problem is when I try to print this list I have staff records starting out in the middle of the page. I would like to solve this issue by forcin

  • How to add and remove (multiple) image from canvas.? 2012-01-06

    I am new to canvas (HTML5) i have to design a paint application on canvas. There is a feature like dynamic addition of the selected image at canvas (on mouse-movements) and functionality to delete and drag that added image (Same for Text addition). N

  • Extracting and Printing multiple elements from different vectors in R 2014-12-29

    I have a Data Frame with over 20 columns and am trying to create a new object which returns the maximum value of one vector and also returns the value in the same row of a different vector/variable. I have been reading through many many pages/sites o

  • JavaScript CSS how to add and remove multiple CSS classes to an element 2010-01-01

    How can assign multiple css classes to an html element through javascript without using any libraries? --------------Solutions------------- Try doing this... document.getElementById("MyElement").className += " MyClass"; Got this here... guaranteed to

  • trying to convert and print multiple master pages 2015-01-08

    I created a booklet, making copies of a double master page as I went. Now, I am trying to change it to PDF file, when I do, all it converts is one page (single, not double page like I have it set up). I'm not sure if I'm saving it wrong or what. I've

  • Getting and printing chars in C? 2009-09-10

    How do I get and print a char from a user? This want do it... #include <stdio.h> int main() { float number1; char mychar = ' '; printf("Number?\n"); scanf("%f", &number1); printf("Character?\n"); scanf("%c", &mychar); printf("You typed

  • Print Multiple Content Header Types 2011-03-04

    I am trying to convert a pdf into an image using Imagick and print multiple pages. The problem I am running into is using the header( "Content-Type: image/jpeg" ); for each page. It will only print the first page. When finished the image is set to a

  • Printing multiple duplex documents in Word 2007 problem 2013-10-02

    Hello. I have a Dell Inspiron 9400 laptop running Vista Business, SP1. I cannot print multiple copies of a duplex document in Word 2007 on an HP LaserJet 2200DN, connected via a JetDirect card. What I do is select the duplex function, select multiple

  • How to print multiple pages in 2013-09-13


  • I am unable to print multiple pages of emails. 2013-12-30

    When trying to print multiple email pages it will only print the first page. Tried several thing (including setting printer to all pages) but nothing has worked. The printer people (Brother) claim it is not the printer problem. Any help will be appre

  • how can I print multiple files in the order displayed 2015-01-02

    Hi! I use Explorer to select and print multiple files. They do not print in the order displayed, so I have to manually sort them after printing. I have deselected spooling so they should direct print. How can I print these files in the order they app

  • Can I print multiple Outlook calendars together (mine, my wife's and my son's) ? 2013-10-27

    I I have my calendar, my wife's calendar and our son's calendar in Outlook. I keep them as separate calendars so that I can view, update and print them separately. Soccer games. Frequent travel. Etc. Outlook allows me to VIEW them all at the same tim

  • Windows 7 printer spooler stopped and can't print re-install drivers or add a printer 2014-05-23

    Original title: Windows 7: Print spooler fails, can't print with printers, can't re-install drivers for printers, can't add a printer. Basically everything for printers is no more. This all started after the lates batch of May Windows 7 fixes were au

  • Store multiple chars in a long and recover them 2017-11-16

    The following code is used to compact multiple values in a long. The long is used as a key in a C++ unordered_map. It allows me to use the map with a number instead of a complex structure and ifs on each properties. The map searching to be as efficie

Copyright (C), All Rights Reserved.

processed in 0.087 (s). 11 q(s)