0

New guy is here again and i got stuck again as usual. (Dunno what my question title should be but its definitely related to function i think) I wanted to make the user roll a dice and max out when they reach 20. But the thing is my function obj.currentRoll();is not adding up the previous rolls as i intended it to. Basically i wanted it to store the value value=value+roll; for consecutive turns so that later on i could use an if statement like if (value>max){return 0} or sth like that. I was able to do it in a easier way without using seperate class or functions but I hoped to achieve the same result this way too and failed. Any suggestions?

  #include <iostream>
    #include "myClass.h"
    #include <string>
    #include <cstdlib>
    #include <ctime>


int main()
{
    srand(time(0));
    std::string rollCh;
    const int max=20;


    std::cout<<"Your lucky number for the day is " <<1+(rand()%30)<<"\n";

    std::cout<<"Roll the dice? (Y/N)"<< "\n";
    std::string ch;
    std::cin>>ch;
    if(ch=="Y"||ch=="y"){
        myClass obj;
        do
        {

        std::cout<<"Rolling...\n"<<"\n";
        std::cout<<"You rolled "; obj.funcRoll();std::cout<<"!!!" <<"\n";
        std::cout<<"Double checking your roll...yes it is ";obj.funcRoll();
        obj.currentRoll();

        std::cout<<"\n\n Roll again? (Y/N)"<<"\n";
        std::cin>>rollCh;
        }
while (rollCh=="Y"||rollCh=="y");

    }

    return 0;
}

myClass.h

#ifndef MYCLASS_H
#define MYCLASS_H


class myClass
{
    public:
        myClass();
        void funcRoll();
        int roll;
        int value;
        void currentRoll();

    };

#endif // MYCLASS_H

myClass.cpp

#include "myClass.h"
#include <iostream>
#include <cstdlib>
#include <ctime>

myClass::myClass()
{}

void myClass:: funcRoll(){
srand(time(0));
roll=1+(rand()%6);
std::cout<<roll;
}

void myClass:: currentRoll(){
value=0;
value=value+roll;
std::cout<<"\n You have rolled "<< value<<" so far";
}

Easier way which i did first

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

int main()
{
    int max=20;
    int value=0;
    int roll;

    std::string ch;

    do
    {

        srand(time(0));
        roll=1+(rand()%6);
      std::cout<<"\nYou rolled "<<roll<<"\n";
      value=value+roll;
      std::cout<<"Your total value = " << value<<"\n";
      if (value<max){
      std::cout<<"continue? \n\n"<<"\n";
      std::cin>>ch;}
else {
    std::cout<<"You have maxed out. Congrats!"<<"\n";
    return 0;
}
      }
      while (ch=="y"||ch=="Y");
    return 0;
}
Sanrai
  • 5
  • 2
  • 3
    In your `currentRoll()` function you are setting the value to `0` each time. Instead use the class constructor to set it to `0`. – Ron Jul 15 '17 at 05:21
  • i tried setting value = 0 in public: before but got some error i think – Sanrai Jul 15 '17 at 05:25
  • Offish topic: `srand(time(0));` is best called once. It resets the random number generator and if called more than once a second (the resolution of `time`) the generator will be reset with the same seed and generate the same random sequence. Visible result is bands of the same number generated over and over. – user4581301 Jul 15 '17 at 05:26
  • @Ron oh in constructor? like myClass(){value=0;} ? – Sanrai Jul 15 '17 at 05:28
  • 2
    debug debug debug – vsoftco Jul 15 '17 at 05:28
  • @Sanrai, unless you have c++11 or more recent enables you can't initialize and define on the same line in a class definition. In previous standards you should `myClass():value(0){}` – user4581301 Jul 15 '17 at 05:28
  • @user4581301 oh so in my above program should i use it only in main.cpp? for srand(time()0 – Sanrai Jul 15 '17 at 05:29

1 Answers1

0

Move the value = 0; statement from your currentRoll() function to your constructor:

myClass::myClass() : value(0){}

Replace std::string types with char. Live example on cpp.sh.

Ron
  • 14,674
  • 4
  • 34
  • 47
  • Yay it works now. Thnx!! I am ashamed of my mistakes i have made so far. All so silly mistakes :( This live example is cool. :) – Sanrai Jul 15 '17 at 05:44
  • @Sanrai Also take a look at one of these [C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and another online C++ compiler called [Coliru](http://coliru.stacked-crooked.com/). – Ron Jul 15 '17 at 05:46
  • Thanks i will definitely read some of those books. :) Been only following videos on net so far so yea i got to start from the basic. – Sanrai Jul 15 '17 at 05:52