# C++ ? Compiling



## Da_iMac_Daddy (Sep 15, 2002)

OK so PB won't compile any C++ code... how would I compile it otherwise?


----------



## davidbrit2 (Sep 15, 2002)

The easy way is to use g++ from the Terminal. Use this syntax:
g++ inputfile -o outputfile


----------



## Da_iMac_Daddy (Sep 15, 2002)

Do you know a replacement for the header file lvp\string.h? apparently the book I'm using at school is out of date or something.


----------



## davidbrit2 (Sep 15, 2002)

Could you paste in the exact include statement? There's a string.h file, so I'm not certain why it isn't including properly...


----------



## Da_iMac_Daddy (Sep 16, 2002)

#include Carbon/Carbon.h>
#include iostream.h>
#include lvp/string.h>

int main()
{

String name;
cout<<"Please enter your name"<<endl;
cin>>name;

cout<<name<<" you are CRAZY!!!!";

return(0);
}


----------



## Da_iMac_Daddy (Sep 16, 2002)

I had to write the include statements like that to get them to show up on the board


----------



## Da_iMac_Daddy (Oct 13, 2002)

OK  well i thought you meant gcc that's not installed and g++ also isnt installed. When i try to compile using g++ it says it cant find it and suggests c++ but that doesn't compile the program correctly either. I've looked this program over a million times and i still can't find anything wrong with it. please help :-(


----------



## Minion (Oct 14, 2002)

PB compiles and builds  C++ code fine !! If you have PB installed, you MUST have gcc too... cc might not be a problem with such a simple code since it was installed in former versions of OSX instead of gcc. You should perhaps reinstall the developer tools...

To build a simple C++ application choose as "New Project" (in PB), a "C++ tool" then you will have a command-line executable.

To compile your code, #include \<string\> instead of \<lvp/string.h\> unless this file is a specific one but it is not in the include path gcc (or PB) looks at when it compiles (have a look at /usr/include directory).
You should also define "name" as a "string", not a "String".
Last thing, call "cout" like this: std::cout, std::cin... unless you call "using namespace std;" before the main.

I tried your code, it works well on my titanium box !

'hope this helps...
M.


----------



## Minion (Oct 14, 2002)

oups, i meant:

#include &lt;string&gt; 
instead of
#include &lt;lvp/string.h&gt;


----------



## Da_iMac_Daddy (Oct 14, 2002)

This is the code I'm trying to compile 
I don't have any online storage so I'll just put it all on here.

//---------------------------------------------------
/*
 *  function.cpp
 *  
 *this program demonstrates functions with arguments 
 *
 */

#include Carbon/Carbon.h
#include iostream.h
#include stdio.h


//square - returns a square of its argument
//	   doublevar - the value to be squared
//	   returns the square of doublevar
double square(double doublevar)
{
    retutrn doublevar * doublevar;
}


//sumSequence - add a sequence of numbers entered from 
//	  	the keyboard and squareduntil the 
//		user enters a negative number.
//		return - the summation of the square
//		of the numbers entered

int sumSequence(void)
{
        //loop forever
        int accumulator = 0;
        for (;
        {
            //fetch another number
            double doubleValue = 0;
            cout << "Enter next number: ";
            cin >> doubleValue;

            // if it's negative...

            if (doubleValue < 0)
            {
                //...then exit from the loop
                break;
            }

            //.. otherwise calculate the square
            int value = (int)square(doubleValue);
            //now add the square to the 
            //accumulator
            accumulator= accumulator+ value;
            }
}

int main(int arg, char* pszArgs[])
{
    cout<<"This porgram sums miltiple series \n"
        <<"of numbers. Terminate each sequence \n"
        <<"by entering a negative number. \n"
        <<"Terminate the series by entering two \n"
        <<"negative numbers in a row. \n"

    //continue to accumulate numbers...
    int accumulateddoubleValue;
    do
    {
        //sum a sequence of numbers entered from
        //the keyboard
        cout <<"\nEnter next sequence\n";
        accumulateddoubleValue = sumSequence();

        //now output the accumulated result
        cout <<"\nThe total is: "
             <<accumulateddoubleValue
             <<"\n";

        //...until the sum returned is 0
        }while (accumulateddoubleValue != 0);
        cout <<"Program terminated\n";
        return 0;

}


//---------------------------------------------

I can't see anything wrong with it. Also cc gives me errors when i try to use it. GCC isn't in my /usr/bin folder and I definatley can't compile from PB when writing c++ (I did new project C++ file). How do I compile from PB? I'll tinker around a bit and see what I can do.

Thanx for the help.


----------



## Da_iMac_Daddy (Oct 14, 2002)

haha the for loop came out as a smilie


----------

