Modulus Issue - Lucky Number

Mar 16, 2017 at 9:50pm
Hey there, I'm making a program that calculates a person's lucky number in accordance with a formula given to me. I have tried a variety of things - but suffice it to say I have hit a wall. I either get the answer as -1 no matter the input, or an answer inconsistent with sample data. With a sample input (choosing the second menu option first) of 185 165 9, the lucky number returned should be 10.

(I also think in this version of the code there's an issue with a double variable and the modulus, but after I stopped getting that error I got a completely wrong number returned)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  #include <iostream>
#include <iomanip>
#include <string>
#include <math.h>

using namespace std;

/** Function declaration section**/

void displayMenu();
void greetUser(string userName);
int luckyNumber(int height, int weight, int birthMonth);
double squareRoot(double number);


int main()
{
   	int choice;
    do
    {
        displayMenu();
        cout << "Enter your choice (1-4): \n";
        cin >> choice;
       if (choice != 4)
       {
          //Switch-Case statements follow
          switch (choice)
          {
			 case 1:
                      {
                          string name;
                          cout << "What's your name: ";
                          cin >> name;
                          greetUser(name);
                      }
			          break;
			 case 2:
                     {
                        int height, weight, birthMonth;
                        cout << "Enter your height (in cms), weight (in lbs) and your birthMonth (1-12), each followed by a space: \n";
                        cin >> height >> weight >> birthMonth;
                        luckyNumber(height,weight,birthMonth);
                      }
	                  break;
			 case 3:
                     {
                         double number;
                         cout << "Enter a floating point number whose square-root you would like to know: ";
                         cin >> number;
                         double numberSquareRoot;
                         numberSquareRoot= squareRoot(number);
                         cout << fixed << showpoint << setprecision(3);
                         cout << "\nSquare root of " << number << " is: " << numberSquareRoot << endl;
                     }
                     break;
          }
	    }
	} while (choice != 4);

	return 0;
}

void displayMenu()
{
	cout << "*************-Lab7-*************\n";
	cout << "1.  Display User Greeting\n";
	cout << "2.  Compute Lucky Number\n";
	cout << "3.  Compute Square Root\n";
	cout << "4.  Quit\n\n";
}
void greetUser(string userName)
{
    cout << "Welcome, "<< userName<<endl;
}
int luckyNumber(int height, int weight, int birthMonth)
{
    int lucky, num1;
    double interrim;
    interrim = ((pow(birthMonth,2)/8)+(pow(weight,6)/(height*2.2)));
    num1 = int (interrim);
    lucky = ((num1%366)%10)+1;
    cout<< "Your lucky number is "<<lucky<<".\n";
    return lucky;
}

double squareRoot(double number)
{
    double numberSquareRoot;
    numberSquareRoot= sqrt(number);
    return numberSquareRoot;
}
Last edited on Mar 17, 2017 at 5:33pm
Mar 16, 2017 at 10:03pm
why did you feel compelled to wrap sqrt ??

the correct header is <cmath>. math.h is C.

That aside,
can you printout sizeof(int) for me?
I have a theory. My theory is that pow(weight,6) wont fit in an integer on your system.

Last edited on Mar 16, 2017 at 10:04pm
Mar 16, 2017 at 10:33pm
Okay, for this portion of code:
1
2
3
4
5
6
7
8
9
10
11
12
int luckyNumber(int height, int weight, int birthMonth)
{
    int lucky, num1;
    double interrim;
    interrim = ((pow(birthMonth,2)/8)+(pow(weight,6)/(height*2.2)));
    cout << "Weight to the sixth power: " <<fixed<< pow(weight,6)<<endl;
    cout <<"sizeof(int) = " <<sizeof(int)<< endl;
    num1 = int (interrim);
    lucky = ((num1%366)%10)+1;
    cout<< "Your lucky number is "<<lucky<<".\n";
    return lucky;
}


The output is:
Weight to the sixth power: 20179187015625.000000
sizeof(int) = 4
Your lucky number is -1.
Mar 16, 2017 at 11:16pm
replace all int with int64_t. This is the issue.
you can see this by printing num1, then printing it again after the change.
Last edited on Mar 16, 2017 at 11:16pm
Mar 16, 2017 at 11:34pm
that means the cast also, by the way.
int64_t(interrim);

Mar 16, 2017 at 11:40pm
Do I need to include any other headers to use int64_t?
Mar 16, 2017 at 11:55pm
Do I need to include any other headers to use int64_t?

For these sorts of questions, you should look at a reference.

I prefer this one
http://en.cppreference.com/w/

The answer is yes; you need to include <cstdint>.
Mar 17, 2017 at 1:27am
> Do I need to include any other headers to use int64_t?

As far as possible, avoid using fixed width integer types.
(ie. don't use them unless there is an externally imposed constraint on the program that requires their use.)

The range of long long is guaranteed to be at least as much as that of std::int64_t; so use that instead.

long long is a standard integer type, which is always available; there is no requirement that an implementation must provide a std::int64_t. std::int_fast64_t (and std::intmax_t) would be available everywhere; in most cases, either would be a better choice than std::int64_t.
Mar 17, 2017 at 1:46am
heh, welcome to c++ where there are 87 int types that represent maybe 10 actual int types :)
Last edited on Mar 17, 2017 at 1:47am
Mar 17, 2017 at 5:32pm
Okay, here is the revised section of the code. In the switch case I also changed the variable types.
1
2
3
4
5
6
7
8
9
10
long long luckyNumber(long long height, long long weight, long long birthMonth)
{
    long long lucky, num1;
    double interrim;
    interrim = ((pow(birthMonth,2)/8)+(pow(weight,6)/(height*2.2)));
    static_cast<long long>(interrim);
    lucky = ((num1%366)%10)+1;
    cout<< "Your lucky number is "<<lucky<<".\n";
    return lucky;
}


However, with the sample input, I still received an incorrect output of lucky=1. It is supposed to equal 10.
Mar 17, 2017 at 6:27pm
num1 is uninitialized now.

should be num1 = staticmess<etc
Last edited on Mar 17, 2017 at 6:30pm
Topic archived. No new replies allowed.