LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Problem running C++ program in Linux Shell (http://www.linuxquestions.org/questions/programming-9/problem-running-c-program-in-linux-shell-495519/)

Alkibiades 10-25-2006 02:37 PM

Problem running C++ program in Linux Shell
 
Hello everyone,

I have a problem, and I wondered if someone here has a solution. I wrote a program in C++. It compiles OK with the g++ compiler - no errors or warnings. It runs okay on Windows XP. In my SuSE Linux Gnome terminal however, I get the following output:

jp@localhost:~/C++> g++ /home/jp/C++/Kalculus0.0.0.6.cpp -o Kalculus
jp@localhost:~/C++> ./Kalculus
Welcome to Kalculator version 0.0

Type help() for help.
Type quit() to quit the program.

jp@localhost:~/C++>

So it runs the first few lines of the code, and as soon as the user has to give some input, it returns to the Shell. Someone who knows why?

Thank you in advance,
Yours, Alkibiades

magnus.therning 10-25-2006 05:05 PM

Quote:

Originally Posted by Alkibiades
Hello everyone,

I have a problem, and I wondered if someone here has a solution. I wrote a program in C++. It compiles OK with the g++ compiler - no errors or warnings. It runs okay on Windows XP. In my SuSE Linux Gnome terminal however, I get the following output:

jp@localhost:~/C++> g++ /home/jp/C++/Kalculus0.0.0.6.cpp -o Kalculus
jp@localhost:~/C++> ./Kalculus
Welcome to Kalculator version 0.0

Type help() for help.
Type quit() to quit the program.

jp@localhost:~/C++>

So it runs the first few lines of the code, and as soon as the user has to give some input, it returns to the Shell. Someone who knows why?

Thank you in advance,
Yours, Alkibiades

It's difficult to know without seeing the code.

daihard 10-25-2006 09:03 PM

I agree with Magnus. What does the code to ask for the user input look like, at least?

Alkibiades 10-26-2006 12:53 AM

Sorry; here the code is. It is a calculator, so I left out the functions and the variable declarations; methinks they're not very interesting now. The function calculate() does the actual math(it still has to be written), and the help() function is supposed to give some information on the functions. However, the program does not even get to them, it seems, so I left them out. The code:

Code:

#include <iostream>
#include <cstdlib>
#include <cmath>
#include </gsl/cdf/gsl_cdf.h>

using namespace std;
int main()
{
    cout<<"Welcome to Kalculus version 0.0\n"
    <<"Type help for help.\n"
    <<"Type quit to quit the program.\n\n";
    while (bool_terminate == 0)
    {
          bool_processed = 0;
          getline(cin, str_command, '\n');
          if (str_command == "quit()" || str_command == "quit")
          {
                    bool_processed = 1;
                    bool_terminate = 1;
          }
          str_test = str_command.substr(0,4);
          if (str_test == "help")
          {
              bool_processed = 1;
              str_command = str_command.erase(0,4);
              help(str_command);
          }
          if (bool_processed == 0)
          {
                        calculate(str_command);
          }
          if (bool_processed == 0 && str_command != "")
          {
                        cout<<"'"<< str_command <<"' has an incorrect syntax.\n"
                        <<"Type 'help()' for more information on functions and their syntaxes.\n"; 
          }
    }
}


Nylex 10-26-2006 12:58 AM

Surely you need bool_terminate to be defined before the while loop. I'm surprised that compiles and doesn't complain about that variable not being defined before being used.

Alkibiades 10-26-2006 01:49 AM

I'll try. But then, why does it run in Windows?

Thank you, Alkibiades

daihard 10-26-2006 02:16 AM

Quote:

Originally Posted by Alkibiades
I'll try. But then, why does it run in Windows?

You said you had omitted variable declarations. There's no way your code as shown above would have been compiled, no matter what OS/compiler you use. :)

Now, my question is, did you define your own getline() routine? Under Linux, getline() is defined as follows:
Code:

ssize_t getline(char **lineptr, size_t *n, FILE *stream);
It does not match the way you used it in your code.

reddazz 10-26-2006 05:47 AM

Moved: This thread is more suitable in the Programming Forum and has been moved accordingly to help your thread/question get the exposure it deserves.

xhi 10-26-2006 08:30 AM

well those variables are defined somewhere. probably

#include </gsl/cdf/gsl_cdf.h>

is defining them. how is bool_terminate defined in there?

mjones490 10-26-2006 01:19 PM

Where ever it is you declare bool_teminate you need to initialize it with 0. In fact, you should make sure you initalize ALL non-user defined typed variables.

The problem is, when this while loop starts, we don't know what bool_terminate is set to unless we've initialized it to something. If you really wanted to find out what it is set to, do a cout << string (bool_terminate? "True", "False") << endl; right before the loop.

Alkibiades 10-26-2006 03:25 PM

Thank you very much, Mjones490 and Nylex, you were right; defining bool_terminate helps.

I have one other problem: I use some functions from the GNU Scientific Library, but g++ complains. I defined the header file:
Code:

#include "gsl/cdf/gsl_cdf.h"
The functions where I implement it are:
Code:

double func_normalcdf(double d_x, double d_mu, double d_sigma)
{
        return (gsl_cdf_gaussian_P(d_x, d_sigma) + d_mu);
}

double func_InvNorm(double d_opp, double d_mu, double d_sigma)
{
        return (gsl_cdf_gaussian_Pinv(d_opp,d_sigma) + d_mu);
}

I get these errors:
Quote:

/tmp/ccbegiBG.o: In function `func_InvNorm(double, double, double)':
Kalculus0.0.0.7.cpp:(.text+0x14cc): undefined reference to `gsl_cdf_gaussian_Pinv'
/tmp/ccbegiBG.o: In function `func_normalcdf(double, double, double)':
Kalculus0.0.0.7.cpp:(.text+0x150e): undefined reference to `gsl_cdf_gaussian_P'
collect2: ld returned 1 exit status
What does that mean?

Thanks!
Yours, Alkibiades

Alkibiades 10-26-2006 03:28 PM

Quote:

Originally Posted by daihard
Now, my question is, did you define your own getline() routine? Under Linux, getline() is defined as follows:
Code:

ssize_t getline(char **lineptr, size_t *n, FILE *stream);
It does not match the way you used it in your code.

Sorry, I don't get that. Can you give me an example of a valid Linux getline() function?

Thanks, Alkibiades

[Edit: BTW, it does run okay, except for the ziggurat functions, using the same code I displayed]

xhi 10-26-2006 04:21 PM

>Thank you very much, Mjones490 and Nylex, you were right; defining bool_terminate helps.

so it was compiling without having bool_terminate declared? :scratch:

Alkibiades 10-27-2006 09:11 AM

Quote:

Originally Posted by xhi
>Thank you very much, Mjones490 and Nylex, you were right; defining bool_terminate helps.

so it was compiling without having bool_terminate declared? :scratch:

No, it was compiling with a declared bool_terminate, but as bool_terminate was undefined, the loop stopped looping immediately and thence the program was terminated.

xhi 10-27-2006 09:29 AM

Quote:

Originally Posted by Alkibiades
No, it was compiling with a declared bool_terminate, but as bool_terminate was undefined, the loop stopped looping immediately and thence the program was terminated.

yeh. where was it declared at? is it declared in gsl_cdf.h ?


All times are GMT -5. The time now is 02:54 AM.