LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-21-2009, 05:22 PM   #1
jus71n742
Member
 
Registered: Apr 2008
Distribution: Ubuntu 8.04 64bit/ 32bit, FreeBSD 8
Posts: 185

Rep: Reputation: 30
Question Calling other programs to run in C++


I am trying to basically prompt the user for which part of an equation they are wanting to find. This is just a simple interest program. I have it written straight out in one larger file but I am playing with splitting programs up to make them more manageable I suppose? mainly just playing around. However when I compile the programs I get the following error:
Code:
Linux $ g++ simpint_main.cpp simpint_interest.cpp simpint_principle.cpp simpint_rate.cpp  simpint_time.cpp 
simpint_main.cpp: In function ‘int main()’:
simpint_main.cpp:19: error: ‘simpint_interest’ was not declared in this scope
simpint_main.cpp:21: error: ‘simpint_principle’ was not declared in this scope
simpint_main.cpp:23: error: ‘simpint_rate’ was not declared in this scope
simpint_main.cpp:25: error: ‘simpint_time’ was not declared in this scope
I used system after reading a couple articles that say this is the way to go. I am not sure but I am pretty sure I am not setting this up correctly.
Code:
#include<iostream>
#include<iomanip>
#include<cstdlib>

using namespace std;

int main()
{
    int p, t, count;
    double r, i;
    char interest, principle, rate, time;
    cout<<"Welcome to the simple interest program.  What part of the simple interest equation are you trying to solve for? ";
    cin >> interest;
    system(simpint_interest);
    cin >> principle;
    system(simpint_principle);
    cin>>rate;
    system(simpint_rate);
    cin>>time;
    system(simpint_time);
}
I have also tried putting the programs in double quotes, with .cpp, single quotes, and with dir in front of it. all gave the above error, excluding double quotes, which compiled but executed by just printing the file names it was supposed to call.

Thanks for any help.
 
Old 08-21-2009, 05:51 PM   #2
shahab_sh
LQ Newbie
 
Registered: Aug 2009
Location: Brighton England (But I'm Persian)
Distribution: Parsix
Posts: 1

Rep: Reputation: 0
hi,
It definitely should be in double quotes. but could you explain more what those commands are supposed to do? I mean simpint_interest, simpint_principle,...

Thanks,
Shahab.
 
Old 08-21-2009, 06:09 PM   #3
jus71n742
Member
 
Registered: Apr 2008
Distribution: Ubuntu 8.04 64bit/ 32bit, FreeBSD 8
Posts: 185

Original Poster
Rep: Reputation: 30
They are referring to the programs that I am attempting to call. Hence why I noted that I placed .cpp after them.

When I place the .cpp after them ...it compiles, put when I run the program and chose which one I want to call, it will simply print the names of the programs

ook ... so I really don't need system () to do this do I? Or do I need to compile the sources to .exe or .o?

Last edited by jus71n742; 08-21-2009 at 06:40 PM.
 
Old 08-22-2009, 08:54 AM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
You seem to be confusing calling another program with calling a function. Either is possible in C++, but you need to know which you want to do.

When calling another program:

That program must be compiled and linked to an executable binary (all of which can be done in one g++ command). I don't think that can be done in the same g++ command in which you compile the calling program.

The called program must have its own main() function. I suspect you didn't do that, because in the g++ command you quoted, you mixed the calling and called programs, which would have resulted in extra error messages if the called program had its own main().

When calling a function:

You could compile the function to a .o file before compiling/linking the calling program, but it is simpler to compile everything together with one g++ command.

You need to predeclare the function before calling it. In a serious project, that would be done in a .h file that must be included by the calling program. But in a simple project you could accomplish that just by listing the called .cpp files before the calling one when compiling everything with just one g++ command.

If the called .cpp is a function, rather than a program, it must not have its own main().
 
Old 08-22-2009, 09:32 AM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by johnsfine View Post
You seem to be confusing calling another program with calling a function. Either is possible in C++, but you need to know which you want to do.

When calling another program:

That program must be compiled and linked to an executable binary (all of which can be done in one g++ command). I don't think that can be done in the same g++ command in which you compile the calling program.

The called program must have its own main() function. I suspect you didn't do that, because in the g++ command you quoted, you mixed the calling and called programs, which would have resulted in extra error messages if the called program had its own main().

When calling a function:

You could compile the function to a .o file before compiling/linking the calling program, but it is simpler to compile everything together with one g++ command.

You need to predeclare the function before calling it. In a serious project, that would be done in a .h file that must be included by the calling program. But in a simple project you could accomplish that just by listing the called .cpp files before the calling one when compiling everything with just one g++ command.

If the called .cpp is a function, rather than a program, it must not have its own main().
Well, one can also call other programs if the latter are DLLs (.so files in most UNIXish systems) - in such a case the other programs do not have 'main'.

And such a case the other programs are not strictly saying programs, they do not even have a separate PID, they are just dynamically linked (and unlinked) pieces of code.
 
Old 08-22-2009, 12:20 PM   #6
jus71n742
Member
 
Registered: Apr 2008
Distribution: Ubuntu 8.04 64bit/ 32bit, FreeBSD 8
Posts: 185

Original Poster
Rep: Reputation: 30
So in order to call an actual program I must first set them all up as independent programs with a main correct? If I wanted to go that route.
But I should define a the function in a .h (usually) in order to call the functions in the other programs? If I chose that route.

So say I take the first option. I should go back and put everything in its own main, then use system (like I have above) and call the complete program and compile them how?
Code:
$g++ simpint_main.cpp other.cpp other.cpp
or something else?
I am pretty new to this but I am liking it the more I learn and work with it. esp compiling the code into what I want.
 
Old 08-22-2009, 07:03 PM   #7
jus71n742
Member
 
Registered: Apr 2008
Distribution: Ubuntu 8.04 64bit/ 32bit, FreeBSD 8
Posts: 185

Original Poster
Rep: Reputation: 30
Ok, working on this I got an idea and I am wondering if I am heading the right direction. I took out all the different .h files and created one larger .h file to hold all the declarations. then use system ("file.cpp") to run the programs. Only thing I am unsure of is making the file.cpp into an executable binary so that system can run it.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling external programs to FTP from C program Manish Programming 11 04-03-2005 10:53 PM
QT newbie - calling external programs squinn Programming 1 03-24-2005 10:23 AM
calling another script to be run. sonesay Programming 2 05-23-2004 06:08 AM
Calling C++ method from C programs? mojozoox Programming 2 01-14-2004 03:58 AM
wine doesn't pass arguments correctly when calling programs jonchapman Linux - Software 0 10-08-2003 01:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration