I am trying to make a program that generates a random number and asks the user to input the random number generated by the computer. Anyone know how to do this?
This is my program:

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

using namespace std;

int main()
{
    int one;
    time_t seconds;
    time (&seconds) ;
    srand((unsigned int) seconds);
    cout<< rand() << endl;
    cout<<"Enter this number: ";
    cin>> one;
    if (one == seconds) {
    cout<<"\nSparta\n";
    }

P.S.
This is part of a larger project and I couldn't figure this out. Any suggestions would be appreciated.
P.P.S.
I think the problem has something to do with the way I am generating a random number... Just a thought

Hey, try this:

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

using namespace std;

int main()
{
    int one;
    int ran_num;

    time_t seconds;
    time (&seconds) ;
    srand((unsigned int) seconds);
    ran_num = rand();
    cout << ran_num << endl;

    cout<<"Enter this number: ";
    cin>> one;
    if (one == ran_num) {
    cout<<"\nSparta\n";
    }else{
        cout << "Not the right number:(!";
    }
}

Store the random number inside a variable, then you can compare it!

commented: Don't just fix code for someone. *Help* them fix the code themselves -3

You are generating the random number correctly, just not using it correctly.

1) You don't need all that preamble to set up seconds and call srand(). It's easier (and less confusing) if you simply replace lines 10-12 with srand(time(NULL)). Generating seconds is obviously confusing you.

2) You output a random number but didn't store it so you have nothing to compare your input with.

commented: Thank you for 'leading me to the answer'. I do remember things much better when someone lets me do some thinking on my own. +0

[deleted]

commented: I was trying to *lead* him to the answer, not *give* him the answer. He learns better when *he* comes up with the answer. -3

Harsh negative feedback, if I'm honest. People learn differently.

commented: And have you proven he learns better by being given the answer? +0

@WaltP

Go on. Lead him to the answer.

commented: He just did :P +0

WaltP once told me in this thread that, calls to srand() should be ideally placed at top of main() and only once. I think you should try this approach too.

I think you should try this approach too.

Where do you imagine he used it? Look at his code again... :rolleyes:

commented: I think he writes before he reads +10

Thank you phorce and WaltP for the help. My project is now working correctly. It is a missile launch simulator I made for one of my friends (for a laugh). This is my working program. Any additions or suggestions?

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

using namespace std;

int main()
{
    char a = 'a';
    char one23;
    int one;
    int two;
    int three;
    int launch;
    int one_1 = 1776;
    int two_1 = 1999;
    int three_1 = 2012;
    int launch_1 = 138974;
    int code;
    int ran_num;
    srand(time(NULL));
    ran_num = rand();
    time_t currentT = time(0);
    cout<< ctime(&currentT) << endl;
    cout<<"Preparing signal to launch computer...\n\nEnter one\n";
    cin>> one;
    if (one == one_1) {
        cout<<"Enter two\n";
        cout<< rand() << endl;
        cin>> two;
        if (two == two_1) {
            cout<<"Enter three\n";
            cin>> three;
            if (three == three_1) {
                cout<<"Enter launch code\n";
                cin>> launch;
                if (launch == launch_1) {
                    cout<<"Preparing nuclear weapon for launch...\n";
                    cout<<"\nWould you like position A, B, or C\n";
                    cin>> one23;
                    if (one23 == a) {
                        cout<<"Type the second launch code to launch the missile\n";
                        cout << ran_num << endl;
                        cin>> code;
                        if (code == ran_num) {
                            cout<<"Downloading needed files...";
                            getchar();
                            getchar();
                            cout<<"Done";
                            getchar();
                            cout<<"Preparing launch sequence...";
                            getchar();
                            cout<<"Done";
                            getchar();
                            cout<<"Launching missile...";
                            getchar();
                            cout<<"Missle launched";
                            getchar();
                            cout<<"Missile will arive at location A in 17m31s.";
                            getchar();

                        }
                    }
                }
            }
        }
    }
    if (one != one_1||two != two_1||three != three_1||launch != launch_1||code != ran_num) {
        cout<<"Launch Failed";
    }
}

Hey @coolikedat99 - Without getting negative feedback. I would look at using a more OO (Object-Oriented) approach, for me, there's quite a lot of code in main, probably more than there needs to be. Also, your use if IF statements could be replaced with SWITCH statements if and when needed.

P.S. There are a lot of variables.. Like:

int one;
    int two;
    int three;
    int launch;
    int one_1 = 1776;
    int two_1 = 1999;
    int three_1 = 2012;
    int launch_1 = 138974;

Couldn't these be in some kind of an array?

P.S. There are a lot of variables.. Like:

>     int one;
>     int two;
>     int three;
>     int launch;
>     int one_1 = 1776;
>     int two_1 = 1999;
>     int three_1 = 2012;
>     int launch_1 = 138974;

Couldn't these be in some kind of an array?

or if you do not understand arrays, you can declare them on the same line... it looks cleaner.

e.g. Instead of

int a;
int aa;
int aaa;
int aaaa;

you can write int a, aa, aaa, aaaa;

You can even declare variables - int a, aa = 25, aaa, aaaa = 10;

you can declare them on the same line... it looks cleaner

I disagree, but that's just programmer's preference...

commented: I don't like the way it looks either (that is, putting it on the same line) +0

Also, why are these:

int one_1 = 1776;
int two_1 = 1999;
int three_1 = 2012;
int launch_1 = 138974;

not constants?

Also, if this is now solved, please mark as solved.

I use this algorithm for random numbers

#include <iostream>
#include <math.h>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
  int any,any2;

  cout<<"Enter a random number  : ";
  cin>>any;

     srand(time(0));
     any2=1+(rand()%6);
    cout <<"The random number is : "<<any2 << endl;
    if(any==any2){
    //your oode
    }
    else {//your oode
    }
}
commented: And what new informations was given by this post? -3
notes

use :

#include <ctime>
 .....

srand(time(0));
.....

any2=1+(rand()%6);//for numbers between 1 t0 6 (u can change 6 to get a nmber between              //a range )
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.