Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

ssj2

I dont know why this dosent work?????

This topic is 5625 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok first Im VERY new to c++. Now ive been working on a simple DOS die rolling program (basicly you would type in a number and it would give you one between one and the numer you typed) it took forever for me to get it to compile. When I finly got it to do that it ddent even do what I wanted it to do. I hope you can help me. #include <iostream> #include <stdlib.h> int main(int argc, char *argv[]) { int roll = 1; int no = 0; int sides = 1; std::cout << "select the number of sides you want to roll!"; std::cin >> sides; if(sides > no) { roll = rand() % sides; std::cout << roll; } else if(sides = no) { std::cout << "try again buddy!:/n"; } return 0; } And there is the code please help me.

Share this post


Link to post
Share on other sites
Advertisement
it''s a tiny -but common- mistake:

replace this:

}
else if(sides = no)
{

by this:

}
else if(sides == no)
{

Share this post


Link to post
Share on other sites
Well... it compiled well, but you have one mistake:
"else if(sides = no)" must be "else if(sides == no)"

For anything else it work fine.

If God with me, Who against me?

Share this post


Link to post
Share on other sites
It should be:


else if(sides == no)
    std::cout << "try again buddy!:/n";


Notice the equality operator (==) is used and not the assignment operator (=). Don't worry; this is a very common mistake that people make. Also you have used a forward slash for the newline character. This might just be a typo, but, for if you didn't know, it should be back slashes for escape sequences. So it should be:

std::cout << "try again buddy! \n";

Edit: Grrr ... fast people.

[ Google || Start Here || ACCU || MSDN || STL || GameCoding || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on May 1, 2003 3:29:57 PM]

Share this post


Link to post
Share on other sites
hmm it still dosent do it right i whant the output like this

select the number of sides you want to roll!
(type your number)
(if you typed 0)
try again buddy!
(restart program)
(if you typed anything above 0)
(random number between 1 and what you typed)
(end program)

[edited by - ssj2 on May 1, 2003 3:35:25 PM]

Share this post


Link to post
Share on other sites
oh hold on its working (kinda)
umm only problem is the thing closes as soon as its done and it never comes out radom



[edited by - ssj2 on May 1, 2003 3:44:57 PM]

Share this post


Link to post
Share on other sites
I see a few things that may be the reason you are not getting the results you expect:

Your random number will be in the range of 0-(sides-1). I assume this is not the behavior you want, so try this:
roll = rand() % sides + 1;

Additionally, you will generate the same number each time you run it if you keep inputing the same value for sides.
The random generator uses a value "seed" to create a "random" number(you use srand( int ) to set it). What you can do to fix this is to feed it the current time as a seed, thereby creating a pseudo-random number each time the program runs.
Add:

#include <time.h> // time functions library

Add beore calling rand():

int SEED = (int)(time(0));
srand( SEED );

This will make your numbers different each time.
Also, and this is only a suggestion, instead of using:

else if(sides == no)
{
you could use only:
else
{
and when the user type in a 0 or LESS, it will catch the error.

I hope this helps and good luck.




[edited by - cecelski on May 1, 2003 3:48:06 PM]

Share this post


Link to post
Share on other sites
Try this:


#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
    int roll = 1;
    int no = 0;
    int sides = 1;

    std::cout << "select the number of sides you want to roll!";
    std::cin >> sides;

    srand( unsigned (time(0)) );

    if (sides > no)
    {
        roll = rand() % sides + 1;
        std::cout << roll;
    }
    else if (sides == 0)
        std::cout << "try again buddy!\n";

    return 0;
}


If, for example, sides is a assigned 10, the result of rand() % sides will be between 0 and 9 inclusively. By adding 1 to this, it will get a number between 1 and 10, so we assign roll the result of:

rand() % sides + 1

For more information on rand() and the srand() function that I called, see here.

[ Google || Start Here || ACCU || MSDN || STL || GameCoding || BarrysWorld || E-Mail Me ]

Share this post


Link to post
Share on other sites
ok now why does it close when its done and how can i make it restart if somone types 0 or less

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!