0

I need help with that if statement. Tt works the first time when there's a perfect square or a double-digit number, but after the first time it keeps doing it for the other numbers too, is there a way to make this not happen?

Thank you

#include<iostream>
#include<time.h>
#include<stdlib.h>
#include<math.h> 

using namespace std;

int dice()
{

   return rand()%6+1;
}


string check(int tempspot, string &message)
{

if(tempspot == 11 || tempspot == 22 || tempspot == 33 || tempspot == 44 || tempspot == 55 || tempspot == 66 || tempspot == 77 || tempspot == 88 || tempspot == 99) {

    message = "go to jail";
    return message;
}

if(sqrt(tempspot) * sqrt(tempspot) == tempspot) {
    message = "perfect square";
    return message;
}
else
    return "";



}

int main()
{

int sum=0;
string message;
srand(time(NULL));




cout.precision(ios::right);
cout<<"Rolls  ";
cout.precision(ios::left);
cout.width(10);
cout<<"Temp-Spot";
cout.precision(ios::right);
cout.width(10);
cout<<"Prize";
cout.width(10);
cout<<"Message";


do  {


cout<<"\n"<<dice();
cout.precision(ios::left);


sum +=dice();
cout.width(10);
cout<<sum<<"\n";
cout.width(25);

check(sum, message);

if(message == "perfect square") {
    cout<<"+10";
    sum += 10;
}

else if(message == "go to jail") {
    cout<<"10";
    sum = 10;
}


cout.width(20);
cout<<message;

 }while(sum<=100);





return 0;




}
  • 1
    What are the rules for this? – drescherjm May 02 '17 at 21:39
  • ***but after the first time it keeps doing it for the other numbers too*** No idea what you mean by that. – drescherjm May 02 '17 at 21:40
  • Debugger. Use a debugger. A debugger allows you to single step through your code *watching* values of variables. Please edit your post with the result from your debugging session. – Thomas Matthews May 02 '17 at 21:41
  • Maybe in check you have to empty message instead of just returning ""? – drescherjm May 02 '17 at 21:41
  • `if` is not a loop.. – Jesper Juhl May 02 '17 at 21:46
  • Your check is flawed to begin with: `if(sqrt(tempspot) * sqrt(tempspot) == tempspot)` -- This is not guaranteed to give you the results you expect. The `sqrt` function is a floating point function, and floating point is inexact. Comparing floating point for equality as you're doing is going to surprise you. In other words: `sqrt(2) * sqrt(2)` may not equal `2`. – PaulMcKenzie May 02 '17 at 21:46
  • What is an "if loop"? And, please, sentences in English are not delimited with commas. – Lightness Races in Orbit May 02 '17 at 21:50

3 Answers3

1

The problem is in your check function.

if(sqrt(tempspot) * sqrt(tempspot) == tempspot) is always true
Therefore when the condition of being equal to 11,22,... is not met the program will always return perfect square.

Tiago Cunha
  • 169
  • 8
  • `if(sqrt(tempspot) * sqrt(tempspot) == tempspot)` -- Hate to do this, but this is not guaranteed to be true at all. Floating point is not exact. – PaulMcKenzie May 02 '17 at 21:45
  • The function's receive an integer as an argument. You would be right if it would accept any other kind. – Tiago Cunha May 02 '17 at 21:50
  • Ah you're right! Ofc the floating point matters. Thank you for the correction. – Tiago Cunha May 02 '17 at 22:03
  • Actually, the results usually come out right, but you could get into trouble [as shown with this question](http://stackoverflow.com/questions/25678481/why-does-pown-2-return-24-when-n-5-with-my-compiler-and-os). The question asks about `pow` doing strange things, but the same situation can happen with `sqrt`. – PaulMcKenzie May 02 '17 at 22:06
0

your problem is that you're passing "message" into the function as a reference. That reference was only ever changed if one of the conditions were true, it wasn't being set to "" in case they were all false. If both checks were false you were doing return ""; but the return value is discarded so it wasn't relevant.

One quick fix is to make sure a blank message is stored in the default case. You could optionally get rid of the return value in that case, since you only need pass by reference OR getting a return value, you don't need both.

void check(int tempspot, string &message)
{

    if(tempspot == 11 || tempspot == 22 || tempspot == 33 || tempspot == 44 || tempspot == 55 || tempspot == 66 || tempspot == 77 || tempspot == 88 || tempspot == 99) {

        message = "go to jail";
    }

    else if(sqrt(tempspot) * sqrt(tempspot) == tempspot) {
        message = "perfect square";
    }
    else
    { 
        message = ""; // this line was missing
    }
}

Another better idea might be to remove the pass by reference altogether and use the return value. You only need one or the other, and using the return value is clearer on the client side of the function:

string check(int tempspot)
{
    string message; // string for message is now local to function

    if(tempspot == 11 || tempspot == 22 || tempspot == 33 || tempspot == 44 || tempspot == 55 || tempspot == 66 || tempspot == 77 || tempspot == 88 || tempspot == 99) {

        message = "go to jail";
    }

    else if(sqrt(tempspot) * sqrt(tempspot) == tempspot) {
        message = "perfect square";
    }
    else
    { 
        message = "";
    }

    return message; // only return once in this spot, cleaner
}

Usage changes to:

message = check(num);
Jason Lang
  • 1,079
  • 9
  • 17
0

Since I could not comment on existing post due to low reputation, I'm posting here in an effort to resolve the issue Tiago pointed out.

A perfect square is an integer that can be expressed as the product of two equal integers. For example, 100 is a perfect square because it is equal to 10x10. If N is an any integer, then NxN is a perfect square.

Thereby, to check for perfect squares, checking if the result of an sqrt(tempspot) call is integer should be enough. Try typecasting the sqrt() call to integer (checking (int)sqrt(tempspot) == sqrt(tempspot)

should be enough and give the expected result)

ioakeimo
  • 99
  • 7