c++, help with my random number generator

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1980 Views - Last Post: 15 May 2015 - 04:39 AM Rate Topic: -----

#1 jago the tiger   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 12-June 14

c++, help with my random number generator

Posted 11 May 2015 - 04:26 PM

I am a beginner at this so don't judge. c:
But I have started to use rand and stuff, so here is my program:
#include <iostream>
#include <fstream>
#include <istream>
#include <cstdlib>
#include <ctime>

using namespace std;
void getComNum();
void ask();
void test();

bool again;
int userNum;
int comNum;


int main (){
    void ask();
    void getComNum();
    void test();
    
   
};


void ask(){
    cout << "Hello, I am thinking of a number between 1 and 100.."<< endl;
    cout << "..what number do you think it is?? " << endl;
    
};


void test(){
    while(again==true)
    cin >> userNum;
    
    if(userNum > comNum || userNum < comNum){
        cout<<"try again "<<endl;
        again=true;
    }else{cout<<"You are correct"<<endl;}
}



void getComNum(){
    srand(time(0));
    comNum = 1+(rand()%100);
    
};


Tell me any problems or anything you notice I'm missing and tips.
THANK YOU "I'm new"

Is This A Good Question/Topic? 0
  • +

Replies To: c++, help with my random number generator

#2 infernorthor   User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: c++, help with my random number generator

Posted 11 May 2015 - 04:35 PM

Don't have the return type when calling a function.

Don't use global variables, have passed values or locals.

Assign variables before using them.
Was This Post Helpful? 1
  • +
  • -

#3 JackRV   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 51
  • Joined: 26-February 15

Re: c++, help with my random number generator

Posted 11 May 2015 - 04:42 PM

infernorthor brought up some very good points that all need to be addressed and I just have one more which is that in your while loop you start with
while(again==true)

So we need to start with again being equal to true at first so when we declare it we should declare it as
bool again = true;

Also if the user guesses correctly the while loop wont terminate because again is still equal to true so inside the else statement you need to add a
again = false;

Was This Post Helpful? 1
  • +
  • -

#4 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1868
  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: c++, help with my random number generator

Posted 11 May 2015 - 05:42 PM

Hi, what problems are you having?
Was This Post Helpful? 0
  • +
  • -

#5 jago the tiger   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 12-June 14

Re: c++, help with my random number generator

Posted 11 May 2015 - 06:55 PM

Thanks guys.
any tips unsparing and placing my code. To me it looks cludered.

View Postjago the tiger, on 11 May 2015 - 06:50 PM, said:

CORRECTION:
Thanks guys.
any tips on spacing and placing my code. To me it looks cludered.

Was This Post Helpful? 0
  • +
  • -

#6 infernorthor   User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: c++, help with my random number generator

Posted 11 May 2015 - 06:56 PM

please post updated code
Was This Post Helpful? 0
  • +
  • -

#7 jago the tiger   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 12-June 14

Re: c++, help with my random number generator

Posted 11 May 2015 - 07:25 PM

View Postinfernorthor, on 11 May 2015 - 06:56 PM, said:

please post updated code

SORRY, i am having problems with this code.
I wrote this crap in a lazy and stupid way, i wasn't really doing
my best. It is just plain sloppy. is it ok if i rewrite it then post it, unless you want me to figure out this junk.
Was This Post Helpful? 0
  • +
  • -

#8 infernorthor   User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: c++, help with my random number generator

Posted 11 May 2015 - 07:31 PM

Rewrite everything if you want, I don't care. It is just hard to comment on old code that people already offered advice, or not having specific problems or questions.
Was This Post Helpful? 0
  • +
  • -

#9 jago the tiger   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 12-June 14

Re: c++, help with my random number generator

Posted 11 May 2015 - 07:43 PM

whats the difference between of
#include <string>

and
#include <string.h>

Was This Post Helpful? 0
  • +
  • -

#10 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6968
  • View blog
  • Posts: 31,958
  • Joined: 10-May 07

Re: c++, help with my random number generator

Posted 11 May 2015 - 07:44 PM

First is C++, second is C.
Was This Post Helpful? 1
  • +
  • -

#11 jago the tiger   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 12-June 14

Re: c++, help with my random number generator

Posted 11 May 2015 - 07:49 PM

View Postno2pencil, on 11 May 2015 - 07:44 PM, said:

First is C++, second is C.

Thanks
Was This Post Helpful? 0
  • +
  • -

#12 jago the tiger   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 12-June 14

Re: c++, help with my random number generator

Posted 11 May 2015 - 08:06 PM

Allright I got this so far :
#include <iostream>
#include <fstream>
#include <istream>
#include <cstdlib>
#include <ctime>
#include <string.h>

using namespace std;
string ask();
int getUseNum ();
int randNum();


int main(){
    bool loop=true;
    
    if(loop==true){
        ask();
        getUseNum();
        randNum();
        
    }else{cout<<"!!YOU WIN, HORRRAAAYYY!! "<<endl;}
};

string ask(){
    string pick = "I'm thinking of a number between 1 and 100 ";
    string pick1 = "what do you think it is?";
    cout << pick,pick1;
    return pick, pick1;
}

int getUseNum (){
    int userNum;
    cin>>userNum;
    
    return userNum;
}

int randNum(){
    srand(time(0));
    int randNum =(rand() % 101);
    
    return randNum;
}

Still not done, I still need to add code that test if the players number is true.
Tell me if there anything i can add to advance it so far.
I also am not good at using the actual functions and return types, so advice on that will be nice.
Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: c++, help with my random number generator

Posted 11 May 2015 - 08:52 PM

Since you don't seem to be using C-strings you don't need the depreciated C header <string.h> and if you do need this header you should use the C++ version of this header <cstring>. But since you are using C++ strings you should include the header for that class <string>.

Next look at this snippet:
    return pick, pick1;

You can only return one item from a function using the return statement, if you need to return more think about using reference parameters or return a structure/class.

Look at this snippet:
   if(loop==true){

Are you aware that an if() statement is not a loop construct as indicated by the name of your variable? And when dealing with Boolean variables you really don't need to compare to true, if(loop) would be all you need.

Lastly srand() should only be called once, usually early in main().


Jim
Was This Post Helpful? 1
  • +
  • -

#14 jago the tiger   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 12-June 14

Re: c++, help with my random number generator

Posted 11 May 2015 - 09:54 PM

HOLD UP, am I tripping or isn't this sapose to run!?!?
#include <iostream>
#include <string>

using namespace std;

int main(){
    for(int x; x > 3; x++)
    {
        cout << "checking if my brain is working. " <<endl;
    }
}



Its not running.
Was This Post Helpful? 0
  • +
  • -

#15 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: c++, help with my random number generator

Posted 11 May 2015 - 09:58 PM

Perhaps you need to initialize x in your for() loop initialization section?

Jim
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2