Calling Functions in C++

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

hyler

Thread Starter
Joined
Jul 30, 2001
Messages
126
In BASIC, I remember a way in programs with multiple functions to be able to start one function from a command line within another, I believe it was called the "call" command. Is there anything like this in C++? I have a program that could use it. IE:

#include "stdafx.h"
#include <iostream.h>

int main()
{
WHAT COMMAND WOULD GO HERE TO START THE COMMANDS WITHIN beginprog?
}

bool beginprog()
{
int tries =1; //define integer variable
while (tries<4) { //start while loop

cout<<"Do you want to procede (y or n) ?\n"; //write question
char answer=0; //define character variavle
cin>>answer; //asks user to input answer

switch (answer)
{
case 'y':
return true;
case 'n':
return false;
default:
cout<<"Sorry, I don't understand that.\n";
tries=tries+1;
}
}
cout<<"I'll take that for a no.\n";
return false;
}
 

Guy

Joined
Feb 19, 1999
Messages
260
Code:
int main()
{
   bool status = beginprog();
   return (int)status;
}
or simply...
Code:
int main()
{
   beginprog();
   return 0;
}
 

hyler

Thread Starter
Joined
Jul 30, 2001
Messages
126
Code:
OK, I've organized it with functions but there are quite a few things wrong with it and I can't seem to catch them.

#include <iostream.h>

	int main()
	{

	cout<<"*************************************************************************";	
	cout<<"*******     **     ** ******     ** *** ** ****     **     **     **     *******";
	cout<<"******* ****** *** ** ****** ****** *** ** **** *** **** **** *** ** *** *******";
	cout<<"**   ** ******     ** ****** ****** *** ** ****     **** **** *** **     **   **";
	cout<<"******* ****** *** ** ****** ****** *** ** **** *** **** **** *** ** ** ********";
	cout<<"*******     ** *** **     **     **     **   ** *** **** ****     ** *** *******";
	cout<<"**************************************************************************";	
	cout<<"**********Enter 1 to Add, 2 to Subtract, 3 to Multiply, or 4 to Divide**********";
	cout<<endl;
start:
	   		cout<<"Choose the number of your operation then press Enter: ";
			int choice;
			cin>>choice;
			if (choice == 1)
				bool status = addition();
					return (int)status;
						goto start;
			if (choice == 2)
				bool status = subtraction();
					return (int)status;	
						goto start;
			if (choice == 3)
				bool status = multiplication();
					return (int)status;	
						goto start;
			if (choice == 4)
				bool status = division();
					 return (int)status;	
						goto start;
			
			if (choice >4)
				goto start;
	}
			bool adding()
		{
			cout<<"Addition-";
				cout<<"Enter first number: ";
			int first;
				cin>>first;
					cout<<"Enter second number: ";
			int second;
				cin>>second;
			cout<<"Answer: ";
				cout<<first + second;
					cout<< endl;
		}
		bool subtraction()
		{
			cout<<"Subtraction-";
				cout<<"Enter first number: ";
			int first1;
				cin>>first1;
					cout<<"Enter second number: ";
			int second1;
				cin>>second1;
					cout<<"Answer: ";
						cout<<first1 - second1 ;
							cout<< endl;
			}
			bool multiplication()
			{
			cout<<"Multiplication-";
				cout<<"Enter first number: ";
			int first2;
				cin>>first2;
					cout<<"Enter second number: ";
			int second2;
				cin>>second2;
					cout<<"Answer: ";
						cout<<first2 * second2 ;
							cout<< endl;
		}
		bool division()
		{
			cout<<"Division-";
				cout<<"Enter first number: ";
			int first3;
				cin>>first3;
					cout<<"Enter second number: ";
			int second3;
				cin>>second3;
					cout<<"Answer: ";
						cout<<first3 / second3;
							cout<< endl;		 
		}			


Errors:
--------------------Configuration: CALC - Win32 Debug--------------------
Compiling...
calc.cpp
H:\COMP PROGRAMMING\C++ CHAPTERS\CALC\calc.cpp(20) : error C2065: 'addition' : undeclared identifier
H:\COMP PROGRAMMING\C++ CHAPTERS\CALC\calc.cpp(20) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
H:\COMP PROGRAMMING\C++ CHAPTERS\CALC\calc.cpp(21) : error C2065: 'status' : undeclared identifier
H:\COMP PROGRAMMING\C++ CHAPTERS\CALC\calc.cpp(24) : error C2065: 'subtraction' : undeclared identifier
H:\COMP PROGRAMMING\C++ CHAPTERS\CALC\calc.cpp(24) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
H:\COMP PROGRAMMING\C++ CHAPTERS\CALC\calc.cpp(28) : error C2065: 'multiplication' : undeclared identifier
H:\COMP PROGRAMMING\C++ CHAPTERS\CALC\calc.cpp(28) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
H:\COMP PROGRAMMING\C++ CHAPTERS\CALC\calc.cpp(32) : error C2065: 'division' : undeclared identifier
H:\COMP PROGRAMMING\C++ CHAPTERS\CALC\calc.cpp(32) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
H:\COMP PROGRAMMING\C++ CHAPTERS\CALC\calc.cpp(53) : error C2373: 'subtraction' : redefinition; different type modifiers
H:\COMP PROGRAMMING\C++ CHAPTERS\CALC\calc.cpp(66) : error C2373: 'multiplication' : redefinition; different type modifiers
H:\COMP PROGRAMMING\C++ CHAPTERS\CALC\calc.cpp(79) : error C2373: 'division' : redefinition; different type modifiers
Error executing cl.exe.

CALC.exe - 8 error(s), 4 warning(s)
 

Guy

Joined
Feb 19, 1999
Messages
260
1. if you want to write your functions after main() you have to prototype them so the main function knows they exist:
Code:
bool addition();

int main() {
....
}

bool addition() {
...
}
2. You use adding() for your function, but addition() within main

3. You must have everything that you want to happen after a successful if to be in braces:
Code:
if(choice == 1)
{
   bool status = divide();
   return (int)status;
}
4. You do not need the goto start; statements after the returns since the program will never get to them.

5. You are not returning a bool value from your functions


Hope this helps.
 

hyler

Thread Starter
Joined
Jul 30, 2001
Messages
126
C++ Is annoying. All I had to do to get rid of over half of my errors and warnings was to take the functions other than main and put them on top of the code.
 

hyler

Thread Starter
Joined
Jul 30, 2001
Messages
126
All right, now that it works, it's just a matter of getting it to work the way I want it to. Here is my newly revised code, now how would I go about returning to the Int Main function once one of my Bool functions is done? It just shuts down after they finish.

Code:
#include <iostream.h>

			bool addition()
		{
			cout<<"Addition-";
				cout<<"Enter first number: ";
			int	first;
				cin>>first;
					cout<<"Enter second number: ";
			int second;
				cin>>second;
			cout<<"Answer: ";
				cout<<first + second;
					cout<< endl;
		return 0;
		}
		bool subtraction()
		{
			cout<<"Subtraction-";
				cout<<"Enter first number: ";
			int first1;
				cin>>first1;
					cout<<"Enter second number: ";
			int second1;
				cin>>second1;
					cout<<"Answer: ";
						cout<<first1 - second1 ;
							cout<< endl;
		return 0;	
		}
			bool multiplication()
			{
			cout<<"Multiplication-";
				cout<<"Enter first number: ";
			int first2;
				cin>>first2;
					cout<<"Enter second number: ";
			int second2;
				cin>>second2;
					cout<<"Answer: ";
						cout<<first2 * second2 ;
							cout<< endl;
		return 0;
			}
		bool division()
		{
			cout<<"Division-";
				cout<<"Enter first number: ";
			int first3;
				cin>>first3;
					cout<<"Enter second number: ";
			int second3;
				cin>>second3;
					cout<<"Answer: ";
						cout<<first3 / second3;
							cout<< endl;		 
		return 0;
		
		}			
	int main()
	{

	cout<<"********************************************************************************";	
	cout<<"*******     **     ** ******     ** *** ** ****     **     **     **     *******";
	cout<<"******* ****** *** ** ****** ****** *** ** **** *** **** **** *** ** *** *******";
	cout<<"**   ** ******     ** ****** ****** *** ** ****     **** **** *** **     **   **";
	cout<<"******* ****** *** ** ****** ****** *** ** **** *** **** **** *** ** ** ********";
	cout<<"*******     ** *** **     **     **     **   ** *** **** ****     ** *** *******";
	cout<<"********************************************************************************";	
	cout<<"**********Enter 1 to Add, 2 to Subtract, 3 to Multiply, or 4 to Divide**********";
	cout<<endl;
start:
	   		cout<<"Choose the number of your operation then press Enter: ";
			int choice;
			cin>>choice;
			if (choice == 1)
			{
				bool status = addition();
					return (int)status;
						goto start;
						}
			if (choice == 2)
			{
				bool status = subtraction();
					return (int)status;	
						goto start;
			}
			if (choice == 3)
			{
				bool status = multiplication();
					return (int)status;	
						goto start;
			}
			if (choice == 4)
			{
				bool status = division();
					 return (int)status;	
						goto start;
			}
			if (choice >4)
			{
				goto start;
			}		
	}
 
Joined
Oct 9, 2001
Messages
27,087
The reason it ends is because of this:

PHP:
return (int)status;
Using a return in your main ends the program.

If you want it to continue, you probably want to output status to the screen (cout) and then goto start so another calculation can be performed...
 

hyler

Thread Starter
Joined
Jul 30, 2001
Messages
126
No I don't, they were just in the first version of my code as testing purposes. If you look at the code I have recently posted, there is not one GOTO in the program. No if only I could write a GOTO type command that would let you go from one function to another.
 
Joined
Oct 9, 2001
Messages
27,087
You do have goto's...

Code:
if (choice == 4) {
  bool status = division();
  return (int)status;	
  goto start;
}
 

hyler

Thread Starter
Joined
Jul 30, 2001
Messages
126
Sorry, my mistake. I had posted that before I replaced the GOTO commands with loops. I could have swore I posted it after. Oh well.
 
Joined
Oct 9, 2001
Messages
27,087
Then post the new stuff.

But either way, a return statement in your main function ends the program :)
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Top