We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,983 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

If (int == random number)

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

7
Contributors
15
Replies
3 Days
Discussion Span
8 Months Ago
Last Updated
16
Views
Question
Answered
coolikedat99
Newbie Poster
21 posts since Aug 2012
Reputation Points: -3
Solved Threads: 0
Skill Endorsements: 0

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!

phorce
Master Poster
738 posts since Jul 2011
Reputation Points: 63
Solved Threads: 91
Skill Endorsements: 16

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.

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37
Vish0203
Junior Poster
144 posts since Apr 2012
Reputation Points: -6
Solved Threads: 9
Skill Endorsements: 0

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

phorce
Master Poster
738 posts since Jul 2011
Reputation Points: 63
Solved Threads: 91
Skill Endorsements: 16

@WaltP

Go on. Lead him to the answer.

Vish0203
Junior Poster
144 posts since Apr 2012
Reputation Points: -6
Solved Threads: 9
Skill Endorsements: 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.

np complete
Posting Whiz
385 posts since Sep 2010
Reputation Points: 18
Solved Threads: 36
Skill Endorsements: 0

I think you should try this approach too.

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

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37

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";
    }
}
coolikedat99
Newbie Poster
21 posts since Aug 2012
Reputation Points: -3
Solved Threads: 0
Skill Endorsements: 0

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.

phorce
Master Poster
738 posts since Jul 2011
Reputation Points: 63
Solved Threads: 91
Skill Endorsements: 16

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?

phorce
Master Poster
738 posts since Jul 2011
Reputation Points: 63
Solved Threads: 91
Skill Endorsements: 16

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;

JameB
Junior Poster
166 posts since Mar 2009
Reputation Points: 76
Solved Threads: 15
Skill Endorsements: 1

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

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

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37

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.

phorce
Master Poster
738 posts since Jul 2011
Reputation Points: 63
Solved Threads: 91
Skill Endorsements: 16

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
    }
}
RainbowMatrix
Newbie Poster
22 posts since Aug 2012
Reputation Points: -4
Solved Threads: 4
Skill Endorsements: 0

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 )
RainbowMatrix
Newbie Poster
22 posts since Aug 2012
Reputation Points: -4
Solved Threads: 4
Skill Endorsements: 0
Question Answered as of 8 Months Ago by phorce, WaltP, Vish0203 and 3 others

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1113 seconds using 2.8MB