Archived

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

ssj2

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

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
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
It closes because like Pipo said you need to add system("PAUSE") to the end. This will add a "Press any key to continue..." prompt at the end of your program. To make the program ask again after you enter a 0, you need tu use a loop. I would do something like this:


int sides = 0
std::cout << "select the number of sides you want to roll!";

while (sides <= 0)
{
std::cin >> sides;
if(sides > no)
{
...
}
else if(sides <= no)
{
std::cout << "try again buddy!:/n";
}
}
system("PAUSE");


[edited by - Wrudyn on May 1, 2003 4:06:01 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Betrayer_of_Code
Ummm...Why dont you add "using namespace std;" so you dont have to add std:: before all you cin''s and cout''s? It takes a heck of a lot less time

You shouldn''t use the line

using namespace std;

When you do that all function, classes, and variables that are within std namespace are now avaialable. You will eventually run into the same problem that was resolved by putting the standard in the std namespace! Your just opening up the doors to global name polution.

Get into the habit of doing this

using std::string;

Share this post


Link to post
Share on other sites
He means conflicts between code in the std namespace and code that is in other namespaces, or not in a namespace at all.

Really,
using namespace std 
defeats the whole point of using namespaces in the first place.

[edited by - MDI on May 2, 2003 3:45:09 PM]

Share this post


Link to post
Share on other sites