There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Software Development
Tag Cloud
audio blue screen boot bsod computer cpu crash dell desktop driver drivers error excel external hard drive firefox firewall format freezes freezing hard drive hardware hijackthis internet internet explorer itunes laptop malware motherboard mouse network networking outlook outlook 2007 power printer problem ram router screen slow sound trojan usb virus vista vista 32-bit windows windows xp winxp wireless
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Calling Functions in C++


Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
hyler's Avatar
Senior Member with 120 posts.
 
Join Date: Jul 2001
26-Sep-2003, 12:18 PM #1
Calling Functions in C++
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's Avatar
Guy Guy is offline
Senior Member with 260 posts.
 
Join Date: Feb 1999
Location: abby, BC, canada
26-Sep-2003, 08:37 PM #2
Code:
int main()
{
   bool status = beginprog();
   return (int)status;
}
or simply...
Code:
int main()
{
   beginprog();
   return 0;
}
hyler's Avatar
Senior Member with 120 posts.
 
Join Date: Jul 2001
09-Oct-2003, 11:51 AM #3
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's Avatar
Guy Guy is offline
Senior Member with 260 posts.
 
Join Date: Feb 1999
Location: abby, BC, canada
09-Oct-2003, 03:35 PM #4
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.
__________________
There are only 10 types of people in the world; Those who understand binary, and those who dont.
hyler's Avatar
Senior Member with 120 posts.
 
Join Date: Jul 2001
13-Oct-2003, 12:49 PM #5
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's Avatar
Senior Member with 120 posts.
 
Join Date: Jul 2001
13-Oct-2003, 01:04 PM #6
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;
			}		
	}
hyler's Avatar
Senior Member with 120 posts.
 
Join Date: Jul 2001
14-Oct-2003, 11:34 AM #7
bump
bump bump bump bump
bump bump
Gibble's Avatar
Distinguished Member with 27,137 posts.
 
Join Date: Oct 2001
Location: Striking or Scoring
Experience: The Alpha and Omega
14-Oct-2003, 11:36 AM #8
*ACK* You have goto's in your program...!!!
Gibble's Avatar
Distinguished Member with 27,137 posts.
 
Join Date: Oct 2001
Location: Striking or Scoring
Experience: The Alpha and Omega
14-Oct-2003, 11:40 AM #9
The reason it ends is because of this:

PHP Code:
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's Avatar
Senior Member with 120 posts.
 
Join Date: Jul 2001
14-Oct-2003, 11:41 AM #10
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.
Gibble's Avatar
Distinguished Member with 27,137 posts.
 
Join Date: Oct 2001
Location: Striking or Scoring
Experience: The Alpha and Omega
14-Oct-2003, 11:45 AM #11
You do have goto's...

Code:
if (choice == 4) {
  bool status = division();
  return (int)status;	
  goto start;
}
hyler's Avatar
Senior Member with 120 posts.
 
Join Date: Jul 2001
14-Oct-2003, 11:47 AM #12
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.
Gibble's Avatar
Distinguished Member with 27,137 posts.
 
Join Date: Oct 2001
Location: Striking or Scoring
Experience: The Alpha and Omega
14-Oct-2003, 12:00 PM #13
Then post the new stuff.

But either way, a return statement in your main function ends the program
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 02:51 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.