# C++ programming.

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• Mar 3rd 2013, 02:04 PM
Riana
C++ programming.
Write a program that simulates tossing of a die.
Write a program that lets the user enter the results of 20 tosses. The output should be a summary of how many ones; twos and so on have appeared.

this is the question but i dint even know how to start and what does the tossing of die mean?
• Mar 3rd 2013, 02:09 PM
ILikeSerena
Re: C++ programming.
Hi again Riana! ;)

Tossing a die means throwing a 6-sided die and seeing which number comes up.

In C++ you can simulate it with:
Code:

`int toss = rand() % 6 + 1;`
Your job is to count the number of times the toss is 1.
Or 2, and so on.
• Mar 3rd 2013, 02:15 PM
Riana
Re: C++ programming.
ok it says rand was not declared?....and if i write this after this am i suppose to ask the user to enter any number...and if yes so dp i use switch?
• Mar 3rd 2013, 02:18 PM
Riana
Re: C++ programming.
#include <iostream>
using namespace std;
int main () {
int counter =0;
int x;
int total1,total2,total3,total4,total5,total6=0;
cin>>x;

while(counter<20)
{
switch(x)
{
case 1:
total1++;
break;
case 2:
total2++;
break;
case 3:
total3++;
break;
case 4:
total4++;
break;
case 5:
total5++;
break;
case 6:
total6++;
break;
}

counter++;

}
return 0;
}

this is my code so far ....this was wat i was able to sort out.
• Mar 3rd 2013, 02:19 PM
ILikeSerena
Re: C++ programming.
You can get rand() by including #include <cstdlib>.
But perhaps you're not supposed to use rand(), but simply ask the user for 20 numbers (between 1 and 6).

And yes, if you have the toss results, you can use a switch to count each toss result.
• Mar 3rd 2013, 02:21 PM
ILikeSerena
Re: C++ programming.
If believe you should put the "cin >> x" inside the while-loop.

And I guess you should print the totals at the end.
• Mar 3rd 2013, 02:30 PM
Riana
Re: C++ programming.
#include <iostream>
using namespace std;
int main () {
int counter =0;
int x;
int total1,total2,total3,total4,total5,total6=0;
int total=0;

while(counter<6)
{
cin>>x;
switch(x)
{
case 1:
total1++;
break;
case 2:
total2++;
break;
case 3:
total3++;
break;
case 4:
total4++;
break;
case 5:
total5++;
break;
case 6:
total6++;
break;
}

counter++;

}total=total+total1+total2+total3+total4+total5+to tal6;

cout<<"the total is "<<total<<endl;
return 0;
}

i did this....but the total have a negative sign in front of it.why?
• Mar 3rd 2013, 02:34 PM
ILikeSerena
Re: C++ programming.
When you declared
Code:

`int total1,total2,total3,total4,total5,total6=0;`
it was only the last one that was initialized to zero.
They should all be initialized to zero, otherwise they will contain an unpredictable value (possibly negative).

Btw, you output should consist of each total separately.
The output should look something like:
Code:

```Summary after 20 tosses. Number of ones: 3 Number of twos: 4 ... Number of sixes: 2```
• Mar 3rd 2013, 02:39 PM
Riana
Re: C++ programming.
how can i find these ones and twos?
• Mar 3rd 2013, 02:40 PM
ILikeSerena
Re: C++ programming.
The number of ones is total1.
• Mar 3rd 2013, 02:45 PM
Riana
Re: C++ programming.
cout<<"Summary after 6 tosses "<<total<<endl;
cout<<"The number of one's "<<total1<<endl;
cout<<"The number of two's "<<total2<<endl;
cout<<"The number of three's "<<total3<<endl;
cout<<"The number of four's "<<total4<<endl;
cout<<"The number of five's "<<total5<<endl;
cout<<"The number of six's "<<total6<<endl;

like this?
• Mar 3rd 2013, 02:47 PM
sali90
Re: C++ programming.
Are you all doing you java programs, do you get the idea of how to write a java progra,. cause when i was in my old school class during the computer exams i used to by heart my java programs.and also in my class no one had the idea of writting a java program cause it is so tough and i just hate when i dont know anything.whoever knows the java program they might be really lucky and a good student.
• Mar 3rd 2013, 02:48 PM
ILikeSerena
Re: C++ programming.
Quote:

Originally Posted by Riana
cout<<"Summary after 6 tosses "<<total<<endl;
cout<<"The number of one's "<<total1<<endl;
cout<<"The number of two's "<<total2<<endl;
cout<<"The number of three's "<<total3<<endl;
cout<<"The number of four's "<<total4<<endl;
cout<<"The number of five's "<<total5<<endl;
cout<<"The number of six's "<<total6<<endl;

like this?

Yep!

Although I'd replace the first line by
Code:

`cout << "Summary after " << total << " tosses" << endl;`
• Mar 3rd 2013, 02:50 PM
Riana
Re: C++ programming.
opss my mistake ....thanks