LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 10-25-2006, 02:37 PM   #1
Alkibiades
LQ Newbie
 
Registered: Dec 2005
Posts: 21

Rep: Reputation: 15
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
 
Old 10-25-2006, 05:05 PM   #2
magnus.therning
LQ Newbie
 
Registered: Jul 2006
Location: Cambridge
Distribution: Debian
Posts: 20

Rep: Reputation: 0
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.
 
Old 10-25-2006, 09:03 PM   #3
daihard
Member
 
Registered: Jul 2003
Location: Seattle, WA
Distribution: Kubuntu 12.04
Posts: 913

Rep: Reputation: 32
I agree with Magnus. What does the code to ask for the user input look like, at least?
 
Old 10-26-2006, 12:53 AM   #4
Alkibiades
LQ Newbie
 
Registered: Dec 2005
Posts: 21

Original Poster
Rep: Reputation: 15
Post

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";   
          }
    }
}
 
Old 10-26-2006, 12:58 AM   #5
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
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.
 
Old 10-26-2006, 01:49 AM   #6
Alkibiades
LQ Newbie
 
Registered: Dec 2005
Posts: 21

Original Poster
Rep: Reputation: 15
I'll try. But then, why does it run in Windows?

Thank you, Alkibiades

Last edited by Alkibiades; 10-26-2006 at 01:50 AM.
 
Old 10-26-2006, 02:16 AM   #7
daihard
Member
 
Registered: Jul 2003
Location: Seattle, WA
Distribution: Kubuntu 12.04
Posts: 913

Rep: Reputation: 32
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.
 
Old 10-26-2006, 05:47 AM   #8
reddazz
Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 73
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.
 
Old 10-26-2006, 08:30 AM   #9
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
well those variables are defined somewhere. probably

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

is defining them. how is bool_terminate defined in there?
 
Old 10-26-2006, 01:19 PM   #10
mjones490
Member
 
Registered: Sep 2005
Distribution: LFS
Posts: 60

Rep: Reputation: 22
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.
 
Old 10-26-2006, 03:25 PM   #11
Alkibiades
LQ Newbie
 
Registered: Dec 2005
Posts: 21

Original Poster
Rep: Reputation: 15
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

Last edited by Alkibiades; 10-26-2006 at 03:56 PM.
 
Old 10-26-2006, 03:28 PM   #12
Alkibiades
LQ Newbie
 
Registered: Dec 2005
Posts: 21

Original Poster
Rep: Reputation: 15
Question

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]

Last edited by Alkibiades; 10-26-2006 at 03:31 PM.
 
Old 10-26-2006, 04:21 PM   #13
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

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

so it was compiling without having bool_terminate declared?
 
Old 10-27-2006, 09:11 AM   #14
Alkibiades
LQ Newbie
 
Registered: Dec 2005
Posts: 21

Original Poster
Rep: Reputation: 15
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?
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.
 
Old 10-27-2006, 09:29 AM   #15
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
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 ?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
problem in running shell script on linux '.sh' ricka343 Programming 4 06-20-2006 02:08 AM
Having Problem with Running a Program in RH linux 8.0 cheenaik Linux - Software 18 06-03-2006 07:41 AM
Running shell script within a C or C++ program Quantum0726 Programming 2 06-15-2005 09:14 PM
running a program from shell script Suinatsa Programming 10 04-14-2005 11:25 AM
How to determine whether a PROCESS is running, by a SHELL program? yuzuohong Linux - General 4 01-21-2003 08:41 PM


All times are GMT -5. The time now is 04:12 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration