so cplusplus.com is losing my connection and won't email back yada yada, so untill thats resolved I'm transferring my learning to this site, now for the program.

any way its just a simple "choose the math operation program" then it performs that operation, but I wanted to add a part after returning the answer, and go back to main() but it wont compile and I'm not sure what the error ' means... so here it is, ask if you need to see more

int addition()
{
    double a1;
    double a2;
    string s2;
    system("CLS");
    cout << "This will add two numbers\n";
    cout << "Please enter the first number:";
    cin >> a1;
    cout << "Please enter the second numeber:";
    cin >> a2;
    double adding= (a1 + a2);
    cout << "The sum is ";
    cout << adding;
    cout << endl;
    system("CLS");
    cout << "Would you like to perform another operation?\n";
    cout << "Press y or n";
    cin  >> s2;
    if((s2=="Y") || (s2=="y"))
    {
        return main();
    }
    else
    {
        return 0;
    }
}

also its nothing above the return main() line, I can put other things in there like

cout << "something";

or really anything but its the

return main();

that gives the error

Recommended Answers

All 83 Replies

Are you trying to go to the main function?

yeah exactly, it works in a statement I have earlier....it's like and error checking so that if they mistype something it says they mistyped and goes back to main, but god forbid I use it somewhere else in the program

You shouldn't call main like that. Basically, you don't go back to the main() that you came from, you start a "new" main when you call it.

I'm confused as to why you are needing to call main anyway if the function is going to return right after that.

Also, since your function isn't really returning anything, it could be void.

When returning something, it must have the value you intend to return. I've never seen the attempt in trying to return the main function, but who am I? Make sure your main function is returning a value...normally main either returns an int value or it is flagged as void, in which it returns nothing. who knows?

well at least from my dumb perspective(completly serious, im a newbie) you can call a function and then it will take it from the top with that code, so if the main() function says like "choose the operation" and im trying to ask if they want to do another operation at the end of whatever was just completed how would i jump back to main() or would i need to just add more code after it? i could post the whole thing if im being too redundant

If that's the case, then move the prompt code out into main and use it in a do/while loop

do
{
 cout<<"Addition function"<<endl;
 addition();
 cout<<"Enter Y or y to continue"<<endl;
//get the character from the user
 } while(character is Y or y);

(some pseudocode in there, but you get the idea)

yeah but I have addition, subtraction, multiplication and division. plus calling main with the return main() worked in a lot of other programs so im just at a loss for words but again if any one needs the annotated source file i can put it up easily

Mainly how this type of programming goes is:

//Controlling Function or Class declaration
CController controller;

void main( void )
{
  //Starting Point...Many times initial values are entered
  controller.RunProgram( /* Possible Global Variables */ );
}

It is possible to design your program to return to the main function
but mainly to revisit the global variables for comparison or modification.
Mostly coders will encapsulate their variable in the controlling class.
Once that controller object is exited the program will end.

Put it this way, on the scale of this program, you're not likely to run out of stack, but on a more substantial project it's more risky. I would guess that it could possibly fail to work if you change compilers or operating systems or any number of things. It's also good to learn good habits early on so you can do it the proper way.

Post the entire source if you need help fitting the parts in somewhere.

Put the code up

dont crack a rib from laughing at me, hah

#include<iostream>
#include<conio.h>

using namespace std;
//so this is where i have just the interface, its not really "variables"
//like the one poster mentioned
//also i havent got to class yet...
int main()
{
    //strings that will be used in the program
    string s1;
    string s2;
    cout << "What math operation would you like to perform?\n";
    cin  >> s1;
    
    
    if((s1=="Division") || s1==("division") || (s1=="/"))
    {
    return division();   //these work
    }
    if((s1=="Addition") || (s1=="addition") || (s1=="+"))
    {
    return addition(); //same
    }
    if((s1== "Subtraction") || (s1=="subtraction") || (s1=="-"))
    {
    return subtraction(); //same
    }
    if((s1== "Multiplication" || s1=="multiplication") || (s1=="*"))
    {
    return multiplication();
    }
    else
    {
    cout << "It seems you have mistyped something, try again\n";
    system("pause");
    system("CLS");
    return main(); //this works too? wtf...
    }
}


int addition()
{
    double a1;
    double a2;
    string s2;
    system("CLS");
    cout << "This will add two numbers\n";
    cout << "Please enter the first number:";
    cin >> a1;
    cout << "Please enter the second numeber:";
    cin >> a2;
    double adding= (a1 + a2);
    cout << "The sum is ";
    cout << adding;
    cout << endl; //above code obviously just adds 2 numbers
    system("CLS");
    cout << "Would you like to perform another operation?\n";
    cout << "Press y or n";
    cin  >> s2;
    if((s2=="Y") || (s2=="y")) 
    {
        return main();//this one doesnt want to work?
    }
    else
    {
        return 0;
    }
}

int multiplication()
{
    double a1;
    double a2;
    system("CLS");
    cout << "This will multiply two numbers\n";
    cout << "Please enter the first number:";
    cin >> a1;
    cout << "Please enter the second numeber:";
    cin >> a2;
    double multi= (a1 * a2);
    cout << "The product is ";
    cout << multi;
    cout << endl;
    
    system("pause");
    return 0;
}

int subtraction()
{
    double a1;
    double a2;
    system("CLS");
    cout << "This will subtract two numbers\n";
    cout << "Please enter the first number:";
    cin >> a1;
    cout << "Please enter the second numeber:";
    cin >> a2;
    double subtracting= (a1 - a2);
    cout << "The difference is ";
    cout << subtracting;
    cout << endl;
    
    system("pause");
    return 0;
}

int division()
{
    double a1;
    double a2;
    system("CLS");
    cout << "This will find the mean of two numbers\n";
    cout << "Please enter the first number:";
    cin >> a1;
    cout << "Please enter the second numeber:";
    cin >> a2;
    double division= (a1/a2);
    cout << "The quotient is ";
    cout << division;
    cout << endl;
    
    system("pause");
    return 0;
}

im not sure this went up the first time i tried to post it

#include<iostream>
#include<conio.h>

using namespace std;
//so this is where i have just the interface, its not really "variables"
//like the one poster mentioned
//also i havent got to class yet...
int main()
{
    //strings that will be used in the program
    string s1;
    string s2;
    cout << "What math operation would you like to perform?\n";
    cin  >> s1;
    
    
    if((s1=="Division") || s1==("division") || (s1=="/"))
    {
    return division();   //these work
    }
    if((s1=="Addition") || (s1=="addition") || (s1=="+"))
    {
    return addition(); //same
    }
    if((s1== "Subtraction") || (s1=="subtraction") || (s1=="-"))
    {
    return subtraction(); //same
    }
    if((s1== "Multiplication" || s1=="multiplication") || (s1=="*"))
    {
    return multiplication();
    }
    else
    {
    cout << "It seems you have mistyped something, try again\n";
    system("pause");
    system("CLS");
    return main(); //this works too? wtf...
    }
}


int addition()
{
    double a1;
    double a2;
    string s2;
    system("CLS");
    cout << "This will add two numbers\n";
    cout << "Please enter the first number:";
    cin >> a1;
    cout << "Please enter the second numeber:";
    cin >> a2;
    double adding= (a1 + a2);
    cout << "The sum is ";
    cout << adding;
    cout << endl; //above code obviously just adds 2 numbers
    system("CLS");
    cout << "Would you like to perform another operation?\n";
    cout << "Press y or n";
    cin  >> s2;
    if((s2=="Y") || (s2=="y")) 
    {
        return main();//this one doesnt want to work?
    }
    else
    {
        return 0;
    }
}

int multiplication()
{
    double a1;
    double a2;
    system("CLS");
    cout << "This will multiply two numbers\n";
    cout << "Please enter the first number:";
    cin >> a1;
    cout << "Please enter the second numeber:";
    cin >> a2;
    double multi= (a1 * a2);
    cout << "The product is ";
    cout << multi;
    cout << endl;
    
    system("pause");
    return 0;
}

int subtraction()
{
    double a1;
    double a2;
    system("CLS");
    cout << "This will subtract two numbers\n";
    cout << "Please enter the first number:";
    cin >> a1;
    cout << "Please enter the second numeber:";
    cin >> a2;
    double subtracting= (a1 - a2);
    cout << "The difference is ";
    cout << subtracting;
    cout << endl;
    
    system("pause");
    return 0;
}

int division()
{
    double a1;
    double a2;
    system("CLS");
    cout << "This will find the mean of two numbers\n";
    cout << "Please enter the first number:";
    cin >> a1;
    cout << "Please enter the second numeber:";
    cin >> a2;
    double division= (a1/a2);
    cout << "The quotient is ";
    cout << division;
    cout << endl;
    
    system("pause");
    return 0;
}

Just because it works doesn't make it right. A car will work if you push it to the top of a hill and let go of the brake. Doesn't mean it should be done that way.

Get rid of the returns on line 19,23,27,and 31. You don't need them. On 38, main should return 0 (or another error code, but don't worry about that).

Wrap lines 13-37 in a do/while loop, and move lines 59-61 to within that loop. Get rid of the rest of that if statement, etc., leave the return 0, but again, there's no need to return anything to main from the function so it might as well be void (plus you're not using the return values in main anyway).

#include <string>
#include <iostream>
using namespace std;

class Calculator
{
  Calculator( void ){}
  ~virtual Calculator( void ){}

  float Add( float one, float two ){ return one + two; }
  float Subtract( float one, float two ){ return one - two; }
  float Multiply( float one, float two ){ return one * two; }
  float Divide( float one, float two )
  {
    if( one )
      return one / two;
  }
};

float GetNumberEntry( void );

void main( void )
{
  Calculator calc();

  string sResponse = "";

  bool bExit = false;
  do
  {
    system( "cls" );
    cout << "Enter a Operation: "
    getline( cin, sResponse );
    if( sResponse.length() )
    {
       char check = ' ';
       check = sResponse.GetAt( 0 );
       switch( check )
       {
        case 'A':
        case 'a': cout << calc.Add( GetNumberEntry(), GetNumberEntry() ) << endl;
        break;

        case 'S':
        case 's': cout << calc.Subtract( GetNumberEntry(), GetNumberEntry() <<      endl;
        break;

        case 'M':
        case 'm': cout << calc.Multiply( GetNumberEntry(), GetNumberEntry() ) << endl;
        break;

        case 'D':
        case 'd': cout << calc.Divide( GetNumberEntry(), GetNumberEntry() ) << endl;
        break;

        case 'X':
        case 'x': bExit = true;
        break;
        };
     }
  }while( !bExit );
}

float GetNumberEntry( void )
{
  string sInput = "";
  cout << "Enter a Number: ";
  getline( cin, sInput );
  return ( atof( sInput.c_str() );
}

That's a nice effort PMorphy, but you shouldn't give away code (I think that's a bit down the road for the OP anyway).

The above might have some syntax errors, but this is the main idea.
Let me know if it helps.

Fair enough jonsca... My first day here. Never been on a C++ blog b4, just got excited :)

Fair enough jonsca... My first day here. Never been on a C++ blog b4, just got excited :)

Not a huge deal. It just prevents the OP from learning if they copy.

yeah it is a little over my head but let me try the loop, are you sure your getting my drift on how i want it to work though? like you type the word of the operation you want to perform, then it solves it, then it asks if you want to do another one, and it can either close or return to asking you "what would you like to perform?"

Yes, the do/while will work for that. Look for an example of one in your text. If the user hits y/Y, it goes around the loop again and reads in another operation. If not, the program ends right there.

Edit: PMorphy's code has a good example of a do/while, so try to see how it works.

okay so i tried wrapping it in a loop but now its giving me errors with the division return

int main()
{
    //strings that will be used in the program
    string s1;
    string s2;
    do
    {    
    cout << "What math operation would you like to perform?\n";
    cin  >> s1;
    
    
    if((s1=="Division") || s1==("division") || (s1=="/")) //error here
    {
        return division();   //these work
    }
    if((s1=="Addition") || (s1=="addition") || (s1=="+"))
    {
        return addition(); //same
    }
    if((s1== "Subtraction") || (s1=="subtraction") || (s1=="-"))
    {
        return subtraction(); //same
    }
    if((s1== "Multiplication" || s1=="multiplication") || (s1=="*"))
    {
        return multiplication();
    }
    else
    {
    cout << "It seems you have mistyped something, try again\n";
    system("pause");
    system("CLS");
    return main(); //this works too? wtf...
    }while((s2!="Y") || (s1!="y")
}

What is the error that it is giving you with line 12?

14,18,22,26 - You still don't need to have "return" there.

33 you can dump, but we've already talked about that.

Your else clause is missing a closing bracket, and if you want it to be associated with all of the above if statements, you need to make the middle ones else if.

if( )
{

}
else if()
{

}
else if()
{

}
else
{

}

Not a huge deal. It just prevents the OP from learning if they copy.

@PMorphy - I reread what I wrote and it sounded like I was being flippant. I'm sure you've already surmised that it is taken very seriously. I said what I did because I didn't want you to think that you'd be banished for all eternity for it.
(via my legal staff)

else
    {//see here
    cout << "It seems you have mistyped something, try again\n";
    system("pause");
    system("CLS");
    return main(); //this works too? wtf...
    }/*<<--not missing a closing brace*/while((s2!="Y") || (s1!="y")

i also did the if else on the rest of them, its still giving me an error, and like a said earlier that error is ' i have no idea what ' means

else
    {//see here
    cout << "It seems you have mistyped something, try again\n";
    system("pause");
    system("CLS");
    return main(); //this works too? wtf...
    }/*<<--not missing a closing brace*/while((s2!="Y") || (s1!="y")

^ is supposed to be the closing brace for the do/while loop, as it's got the while statement right next to it. You need another to close off the else. Try that, and if the error is still there, please copy/paste the error message. The error messages can be slightly cryptic, but they are not indistinguishable.

hey yeah you were right, sorry for being stupid, but heres a screencap of the error attached

i think you have missunderstood the use of return....

Return statement
The return statement stops execution and returns to the calling function. When a return statement is executed, the function is terminated immediately at that point, regardless of whether it's in the middle of a loop, etc.

so how do you propose i go to these functions if i shouldnt be using return...also is that why i couldnt put a second set of returns under the first set, because with the if statement they worked fine, but once i put in a new if statement inside of it, the returns started throwing errors

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.