Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[C programming] A random program.


dthnote801
 Share

Recommended Posts

Basically what this does is take your input, and loops until the random number is larger than your guess.  Its very buggy, and the first program I coded in C.

Any suggestions?

```
#include int main(void){

    int input;
    srand(time(NULL));

    printf("Enter your number\n");
    scanf("%d", &input);

    do
    {
      printf("%d\n", rand());
                }
    while(rand() < input);

    getchar();

    }

```
Link to comment
Share on other sites

Shilotta Jackson says
Someone asked me your comments on this program (they are learning C) http://www.touchofdeathforums.com/smf/index.php/topic,70420.msg757696.html#new
Michael S says
Wellt he only possible issues i see are the possible issues with scanf that that other guy pointed out and the fact that thier using rand() twice so what they rint out wont bee the same as what they just tested against int he loop
Teh should set it to a variable and test adn print that instead.
Shilotta Jackson says
Okay thankx
Link to comment
Share on other sites

@NnsNightshade:

> Shilotta Jackson says
> Someone asked me your comments on this program (they are learning C) http://www.touchofdeathforums.com/smf/index.php/topic,70420.msg757696.html#new
> Michael S says
> Wellt he only possible issues i see are the possible issues with scanf that that other guy pointed out and the fact that thier using rand() twice so what they rint out wont bee the same as what they just tested against int he loop
> Teh should set it to a variable and test adn print that instead.
> Shilotta Jackson says
> Okay thankx

I see, I'll try what he suggested thanks, and also try to fix the scanf.
Link to comment
Share on other sites

Some quick things.

I've noticed that there is no "return 0;" at the end of your main function.  If you haven't learned about C-functions yet, that's an acceptable mistake, but I'd recommend ending all of your programs (within main()) with a "return 0;".  This also tells me you're probably using Microsoft's compiler, which doesn't have the C99 standard with it, so you're better off looking into something like [DevC++](http://www.bloodshed.net/devcpp.html) if you're considering cross-platform code.

With scanf, for something like this it should be fine (unless you want to read in the input into a character buffer, then parse/intemperate it, etc).  Currently (atleast on GCC) if you enter a string instead of a number, it will probably just set the value of 'input' to zero, or a negative number (though I think something like cin/cout would crash it though, since those are streams; but that's C++).  If you want to have some error checking, you can do something like:
```
int result = scanf("%d", &input);
if (result != 1)
{
    puts("Invalid input");
    return -1;
}

```
This is because scanf returns a value, based on it's success.  If it successfully reads one thing in (which is what you're trying to do), it will return '1'.  If it returns 0, that usually means scanf fails.  Should stop people from inputting a string.

As for your do-while loop, rand() usually returns a very big number (much bigger than what most users will input).  Almost every time, you'll end up only doing the loop once (unless you enter for input something really big).

PS: I don't actually use scanf much anymore, as I mainly just handle input through buffers, etc, so if I'm missing something, please let me know.
Link to comment
Share on other sites

@Anŧhøñy:

> Some quick things.
>
> I've noticed that there is no "return 0;" at the end of your main function.  If you haven't learned about C-functions yet, that's an acceptable mistake, but I'd recommend ending all of your programs (within main()) with a "return 0;".  This also tells me you're probably using Microsoft's compiler, which doesn't have the C99 standard with it, so you're better off looking into something like [DevC++](http://www.bloodshed.net/devcpp.html) if you're considering cross-platform code.

This is what I'm using at the moment, I'm not sure if I'm going to switch, it seems like a good enough compiler.

@Anŧhøñy:

> With scanf, for something like this it should be fine (unless you want to read in the input into a character buffer, then parse/intemperate it, etc).  Currently (atleast on GCC) if you enter a string instead of a number, it will probably just set the value of 'input' to zero, or a negative number (though I think something like cin/cout would crash it though, since those are streams; but that's C++).  If you want to have some error checking, you can do something like:
> ```
> int result = scanf("%d", &input);
> if (result != 1)
> {
>     puts("Invalid input");
>     return -1;
> }
>
> ```
> This is because scanf returns a value, based on it's success.  If it successfully reads one thing in (which is what you're trying to do), it will return '1'.  If it returns 0, that usually means scanf fails.  Should stop people from inputting a string.
>
> As for your do-while loop, rand() usually returns a very big number (much bigger than what most users will input).  Almost every time, you'll end up only doing the loop once (unless you enter for input something really big).
>
> PS: I don't actually use scanf much anymore, as I mainly just handle input through buffers, etc, so if I'm missing something, please let me know.

For the do-while loop, I'll probably add a % 8 or 9, not sure…
I also noticed that it exits the program irregardless to the input number being larger than the random number..

Edit: as for the error-checking I might add it, or not, I'm not sure at this point.
Link to comment
Share on other sites

@Varethien:

> This is what I'm using at the moment, I'm not sure if I'm going to switch, it seems like a good enough compiler.

If you're referring to microsoft's, it's a good compiler on windows, it just enforces bad standards and stuff (like allowing you to not return 0 at the end of a program).  I usually develop my code under gcc (or mingw with DevC++, on windows), then compile it for release under microsoft's (since their compiler-generated code is faster on windows than devc++'s).  Though you don't have to deal with things like stdint, etc, atm, so you should be fine with the current compiler for learning.  Just be sure to keep the standards in mind, since MVC++ won't enforce them.

@Varethien:

> For the do-while loop, I'll probably add a % 8 or 9, not sure…
> I also noticed that it exits the program irregardless to the input number being larger than the random number..

Here is an interesting site that provides some random number generators (looks like it's using C): http://www.geekpedia.com/tutorial39_Random-Number-Generation.html
You may find some interesting functions in there that can help you with capping a max number for rand().

Also, the rand() that is getting printed to your screen, is a different number than the rand() that it's checking input against.  This is because every time you call rand(), it [usually] returns a different number.  As another member said, you may want to put it in a variable, to use it twice, like so:
```
int randomNumber;
do
{
    randomNumber = rand();
    printf("%d\n", randomNumber);
}while(randomNumber < input);

```This will ensure you're testing the same number in the printf.
You can also verify that scanf is reading your input correctly, you could print that out in the do loop (or before it), by doing something like:
printf("Entered: %d  – Random: %d", input, randomnumber);

@Varethien:

> Edit: as for the error-checking I might add it, or not, I'm not sure at this point.

That's up to you; this is just something you can use if you're worried that someone might put invalid data into scanf().
Link to comment
Share on other sites

@Anŧhøñy:

> If you're referring to microsoft's, it's a good compiler on windows, it just enforces bad standards and stuff (like allowing you to not return 0 at the end of a program).  I usually develop my code under gcc (or mingw with DevC++, on windows), then compile it for release under microsoft's (since their compiler-generated code is faster on windows than devc++'s).  Though you don't have to deal with things like stdint, etc, atm, so you should be fine with the current compiler for learning.  Just be sure to keep the standards in mind, since MVC++ won't enforce them.

I meant I'm using DevC++ I get sidetracked when posting messages so I tend to leave out information…

@Anŧhøñy:

> Here is an interesting site that provides some random number generators (looks like it's using C): http://www.geekpedia.com/tutorial39_Random-Number-Generation.html
> You may find some interesting functions in there that can help you with capping a max number for rand().

I'll check it out, I could use any information I can get at this point. (I'm only about half a day into learning this.)
@Anŧhøñy:

> Also, the rand() that is getting printed to your screen, is a different number than the rand() that it's checking input against.  This is because every time you call rand(), it [usually] returns a different number.  As another member said, you may want to put it in a variable, to use it twice, like so:
> ```
> int randomNumber;
> do
> {
>     randomNumber = rand();
>     printf("%d\n", randomNumber);
> }while(randomNumber < input);
>
> ```

I was wondering whether or not it'd be correct to do that, in the first place that's how I set it but I really wasn't sure.

@Anŧhøñy:

> This will ensure you're testing the same number in the printf.
> You can also verify that scanf is reading your input correctly, you could print that out in the do loop (or before it), by doing something like:
> printf("Entered: %d  – Random: %d", input, randomnumber);
> That's up to you; this is just something you can use if you're worried that someone might put invalid data into scanf().

I'm not too worried about it at this time because this code was to help me retain the information about it better instead of just reading about it, and then moving on.
Link to comment
Share on other sites

  • 3 weeks later...
For the purpose of learning using scanf will suffice, you should use gets() as you learn to program better as you can do error checking and what not.
Pretty much everywhere will teach you to initially use scanf() because it's easy, you don't want to be bogged down with lots to remember when you're first starting out, Soul was just being pedantic.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...