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?
#include <iostream>
usingnamespace 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;
constint SENTINEL = - 9999;
int main()
{
int number;
int theSum, theProd, theMod;
constint 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;
}
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