I am writing a program that calculates factorials. I have everything fixed except I can't seem to get case ('Y'), case ('y'), in the switch to start the program over. If you have another number you'd like to calculate then you say y and it starts the program back over again. Any hints? I get an error C2440: 'return' : cannot convert from 'int (__cdecl *)(void)' to 'int'

Thanks. Here is my program so face:

#include <iostream>
using namespace std;

int factorial(int n)
{
 int res = 1,i; 
  for (i=1;i<=n;i++)
 {
   res *= i;
  }
  return res;
}
int dfact(int n)
{
       int i;double res=1.0;
       for(i=n;i>=1;i-=2)
       {
       res *=i;
       }
       return res;
}
int main( )
{
int n;
  cout << "Enter the number=";
  cin >> n;
  cout << n << "! = " <<factorial(n) << endl; //factorial output
  cout << n << "!! = " <<dfact(n) << endl; //double factorial output
int num;
cout << "Whould you like to enter a new number? (Yy/Nn)" << endl;
    cin >> num;
switch(num) {
case ('Y'):
case ('y'):
    return main;
    break;
case ('N'):
case ('n'):
    break;
}
  system ("pause");
  return 0;
}

Recommended Answers

All 3 Replies

enclose the whole code from main in a loop where the loop will break if the condition where the user enters n or N is entered
example:

int main(void){
    char counter;
    do{
    //rest of the code from main here
    //ask the user if he wants to continue  and save the value entered to counter
    }while(counter != 'n' || counter != 'N')
}

I fixed what you said and now I am getting these errors

1>Lab6.cpp(45): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>Lab6.cpp(45): error C2365: 'system' : redefinition; previous definition was 'function'
1>          c:\Program Files\Microsoft Visual Studio 10.0\VC\include\stdlib.h(520) : see declaration of 'system'
1>Lab6.cpp(45): error C2440: 'initializing' : cannot convert from 'const char [6]' to 'int'
1>          There is no context in which this conversion is possible
1>Lab6.cpp(46): error C2059: syntax error : 'return'




#include <iostream>
using namespace std;

int factorial(int n)
{
 int res = 1,i; 
  for (i=1;i<=n;i++)
 {
   res *= i;
  }
  return res;
}
int dfact(int n)
{
       int i;double res=1.0;
       for(i=n;i>=1;i-=2)
       {
       res *=i;
       }
       return res;
}
int main(void){
    char counter;
    do
{
int n;
  cout << "Enter the number=";
  cin >> n;
  cout << n << "! = " <<factorial(n) << endl; //factorial output
  cout << n << "!! = " <<dfact(n) << endl; //double factorial output
int num;
cout << "Whould you like to enter a new number? (Yy/Nn)" << endl;
    cin >> num;
switch(num) {
case ('Y'):
case ('y'):
    break;
case ('N'):
case ('n'):
    break;
}
}
while(counter != 'n' || counter != 'N');
}
system ("pause");
  return 0;

I fixed it so that there are no errors but now it doesn't matter if I say Y or N the loop then continues forever.

#include <iostream>
using namespace std;

int factorial(int n)
{
 int res = 1,i; 
  for (i=1;i<=n;i++)
 {
   res *= i;
  }
  return res;
}
int dfact(int n)
{
       int i;double res=1.0;
       for(i=n;i>=1;i-=2)
       {
       res *=i;
       }
       return res;
}
int main(void){
    char counter;
    do
{
int n;
  cout << "Enter the number=";
  cin >> n;
  cout << n << "! = " <<factorial(n) << endl; //factorial output
  cout << n << "!! = " <<dfact(n) << endl; //double factorial output
int num;
cout << "Whould you like to enter a new number? (Yy/Nn)" << endl;
    cin >> num;
switch(num) {
case ('Y'):
case ('y'):
    break;
case ('N'):
case ('n'):
    break;
}
}
while(counter != 'n' || counter != 'N');

system ("pause");
  return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.