problem with conditional printing through prototype

I cannot get this to print with the conditions
if the number that is entered is < 10 it is supposed to find the product of the numbers from 1 to 10 i.e. The product from 1 to 7 is 5040

if the number that is entered is between 10 and 50 it is supposed to sum the numbers from 1 to that number i.e. The sum from 1 to 13 is 91

if the number is larger than 50 it is supposed to print the ramainder of the number divided by 7 i.e. The remainder of 68 / 7 is 5

and when -9999 is entered it is supposed to cancel

please help my printing work

also these are supposed to be functions to do sum, prod, and mod, am i correct?


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
93
94
  #include <iostream>
using namespace std;
int myPrompt (void);
int mySum (int number);
int myProd( int number);
int myMod( int number );
void myPrint( int mySum, int myProd, int myMod, int number);
int input=-1;
const int SENTINEL = - 9999;

int main()
{
	int number;
	int theSum, theProd, theMod;
	const int SENTINEL = - 9999;
	number = myPrompt();
	theSum =  mySum (number);
	theProd = myProd(number);
	theMod = myMod(number);
	myPrint(theSum, theProd, theMod, number);
	return 0;
}

int myPrompt (void)
{
	input = -1;
		while (input < 0 || input != SENTINEL)
		{
			cout << "Enter a non-negative integer (-9999 to end): " << endl;
			cin >> input;
		}
		return input;
}

int mySum (int input)
{
	if (input >= 10 || input <= 50)
		{
			int number;
			int startingNumber = 1;
			int result = 0;

			for (int i=startingNumber; i<=number; i++)
			{
				result = result += i;
			}
		return result;	
	}
	
}

int myProd( int input )
{
	if (input < 10)
		{
			int number;
			int startingNumber = 1;
			int product = 0;

			for (int i=startingNumber; i<=number; i++)
			{
				product = product *= i;
			}
		return product;	
		}
}

int myMod( int number )
{
	
	if ( input > 50)

		{
			int remainder = number % 7;
			return remainder;
		}
}

void myPrint(int mySum, int myProd, int myMod, int number)
{
	if (input >= 10 || input <= 50)
		{
			cout << "The sum from 1 to " << number << "is " << mySum << "." << endl;
		}
	if (input < 10)
		{
			cout << "The product from 1 to " << number << "is " << myProd << "." << endl;
		}
	if ( input > 50)
		{
			cout << "The remainder of " << number << " / " << myMod << "." << endl;
	}
	return;
}
Last edited on
try using the and ( && ) logical operator instead of or on some of your functions...

ex:
if the number that is entered is between 10 and 50
1
2
if ( number > 10 && number <50 )
   /* your code */


if i use or:
1
2
if ( number > 10 || number < 50 )
    /* your code */


what if i in the latter example i input 65 wouldn't it still run because number is greater than 10

and operator only returns true if one of it's operand is true, otherwise will return false, on the other hand or returns true if one of it's operand is true:

ex 65:
65 > 10 || 65 < 50 // this will evaluate to true since the first expression is true

65 > 10 && 65 < 50 //even the first expression is true the second is false, thus will return false and will not run


for your other errors:

you declare again SENTINEL inside main

in my propt your said that if the user enters -9999 then the function should cancel ? if so you should implement it

In your function definitions you have no error checking. In your mySum() what if the value of number is NOT GREATER THAN 10 AND LESS THAN 50, what should the function do, because otherwise you will get random data

Last edited on
Topic archived. No new replies allowed.