# Thread: C++ programming.

1. ## 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?

2. ## 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.

3. ## 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?

4. ## Re: C++ programming.

#include <iostream>
using namespace std;
int main () {
int counter =0;
int x;
int total1,total2,total3,total4,total5,total6=0;
cout<<"Please input your results: ";
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.

5. ## 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.

6. ## 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.

7. ## 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)
{
cout<<"Please input your results: ";
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?

8. ## 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

9. ## Re: C++ programming.

how can i find these ones and twos?

10. ## Re: C++ programming.

You already have them.
The number of ones is total1.

11. ## 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?

12. ## 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.

13. ## Re: C++ programming.

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;

14. ## Re: C++ programming.

opss my mistake ....thanks
btw whats your name?

15. ## Re: C++ programming.

Originally Posted by Riana
opss my mistake ....thanks
btw whats your name?
Ah, I like to keep that a secret on such a public forum.

Page 1 of 2 12 Last