ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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,...
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?
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().
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.