Tech Support Guy banner
Status
Not open for further replies.
1 - 13 of 13 Posts

·
Registered
Joined
·
126 Posts
Discussion Starter · #1 ·
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;
}
 

·
Registered
Joined
·
260 Posts
Code:
int main()
{
   bool status = beginprog();
   return (int)status;
}
or simply...
Code:
int main()
{
   beginprog();
   return 0;
}
 

·
Registered
Joined
·
126 Posts
Discussion Starter · #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)
 

·
Registered
Joined
·
260 Posts
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.
 

·
Registered
Joined
·
126 Posts
Discussion Starter · #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.
 

·
Registered
Joined
·
126 Posts
Discussion Starter · #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;
			}		
	}
 

·
Registered
Joined
·
27,087 Posts
*ACK* You have goto's in your program...!!!
 

·
Registered
Joined
·
27,087 Posts
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...
 

·
Registered
Joined
·
126 Posts
Discussion Starter · #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.
 

·
Registered
Joined
·
27,087 Posts
You do have goto's...

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

·
Registered
Joined
·
27,087 Posts
Then post the new stuff.

But either way, a return statement in your main function ends the program :)
 
1 - 13 of 13 Posts
Status
Not open for further replies.
Top