hi guys , just wondering why doesnt case 4 run in the program. Can you please help me by pointing at the problem and what is the correct structure.
case 3:cout << "please insert a number" << endl;
cin >> n;
cout << endl;
if (n <= 1)
{
cout << "please input a number that is greater than 1" << endl;
cin >> n;
}
isprime( n);
break;
case 4 : cout << "please insert a number " << endl;
cin >> a;
cout << "please insert a second number" << endl;
cin >> b;
system("pause");
return 0;
}
void hello()
{
cout << "hello World" << endl;
}
void printBetween(int a, int b)
{
cout << endl;
if (a <= b)
{
for (int i = a; i <= b; i = i + 1)
{
cout << i << endl;
}
}
else if (b <= a)
{
for (int i = b; i <= a; i = i + 1)
{
cout << i << endl;
}
}
}
bool isprime(int n)
{
for (int i = 2; i <= n / 2; i++)
{
if (n % i == 0)
}
cout << endl;
if (isprime(n))
{
cout << n << " is a prime number" << endl;
}
else
{
cout << n << " is not a prime number" << endl;
}
case 3:cout << "please insert a number" << endl;
cin >> n;
cout << endl;
if (n <= 1)
{
cout << "please input a number that is greater than 1" << endl;
cin >> n;
}
isprime( n);
break;
case 4 : cout << "please insert a number " << endl;
cin >> a;
cout << "please insert a second number" << endl;
cin >> b;
system("pause");
return 0;
}
void hello()
{
cout << "hello World" << endl;
}
void printBetween(int a, int b)
{
cout << endl;
if (a <= b)
{
for (int i = a; i <= b; i = i + 1)
{
cout << i << endl;
}
}
elseif (b <= a)
{
for (int i = b; i <= a; i = i + 1)
{
cout << i << endl;
}
}
}
bool isprime(int n)
{
for (int i = 2; i <= n / 2; i++)
{
if (n % i == 0)
}
cout << endl;
if (isprime(n))
{
cout << n << " is a prime number" << endl;
}
else
{
cout << n << " is not a prime number" << endl;
}
}
indenting your code will make it easier to read but it looks like your switch statement has a return in it. It should not return anything unless it is a function and every case should return something including default.
Here is a structure you can follow which may help:
Int main()
{
cout << "How many players? No more than 4 and no less than 2! << endl;
int numPlayers;
cin >> numPlayers;
switch(numPlayers)
{
case 2: // basically means if numPlayer = 1
//Whatever you want to happen.
cout << "There are 2 players." << endl;
break;
case 3: //if numPlayers = 3
cout << "There are 3 players." << endl;
break;
case 4: //if numPlayers = 4
cout << "There are 4 players." << endl;
break;
default: //If numPlayers doesnt match any of the possible cases.
std::cout << " There can only be 2-4 players! " << std::endl;
}
}
Case 4 does work, but you do not do anything except input two numbers, pause and then exit the program. I think you might want to call the "printBetween" function and change the "return 0" to "break". Notice what kingkush has said.
Hope that helps,
Andy
Edit: after seeing the menu the function to call in case 4 would be "leastCommonDenominator" which you do not have.
//A menu of functions that the user is able to choose from
#include <iostream>
usingnamespace std;
void hello();
void printBetween(int, int);
int main()
{
int choice, a, b, n;
bool isprime(int n);
int leastCommonDenominator(int a, int b);
cout << " Menu Of Functions" << endl;
cout << "1. Void hello" << endl;
//
cout << "2. Void printBetween" << endl;
//
cout << "3. Bool isPrime" << endl;
//
cout << "4. Int leastCommonDenominator " << endl;
//
cout << "5. Void squaredOpposite " << endl;
//
cout << " Enter your choice ( only menu number is acceptable): " << endl;
cin >> choice;
// User's choice of operator
switch (choice)
{
case 1:
hello();
break;
case 2:
cout << "please insert a number" << endl;
cin >> a;
cout << "please insert second number" << endl;
cin >> b;
printBetween(a, b);
break;
case 3:cout << "please insert a number" << endl;
cin >> n;
cout << endl;
if (n <= 1)
{
cout << "please input a number that is greater than 1" << endl;
cin >> n;
}
isprime( n);
break;
case 4 : cout << "please insert a number " << endl;
cin >> a;
cout << "please insert a second number" << endl;
cin >> b;
system("pause");
return 0;
}
void hello()
{
cout << "hello World" << endl;
}
void printBetween(int a, int b)
{
cout << endl;
if (a <= b)
{
for (int i = a; i <= b; i = i + 1)
{
cout << i << endl;
}
}
elseif (b <= a)
{
for (int i = b; i <= a; i = i + 1)
{
cout << i << endl;
}
}
}
bool isprime(int n)
{
for (int i = 2; i <= n / 2; i++)
{
if (n % i == 0)
}
cout << endl;
if (isprime(n))
{
cout << n << " is a prime number" << endl;
}
else
{
cout << n << " is not a prime number" << endl;
}
}
If line 13: int leastCommonDenominator(int a, int b); if a function declaration it should be outside of main with the other declarations. I made some small changes but...
FOR THE LOVE OF GOD INDENT YOUR CODE HBLJKNJLHBGKHBLJKN:JH
You are missing a closing "}" on main and the isprime function has an improperly placed semicolon on the first if statement. And is prime is a recursive function with no way out, so it will over run the stack at some point.
Im doing almost the exact same problem how did you do case 5? this is the step i'm missing
: void squaredOpposite(double &n)
Sets the contents of the argument to be the squared value of n, with the opposite sign compared to n
E.G. input of 3 would result in output of -9
n is being passed as a reference variable, meaning changes made to it in the function will be reflected in the argument that was passed into the function call
-Asks the user if they would like to return to the menu