# Introductory C++ course ... Mac-ified?

Discussion in 'Mac Programming' started by ifjake, Sep 6, 2006.

1. ### ifjake macrumors 6502a

Joined:
Jan 19, 2004
#1
I'm in this introductory C++ course at college. We're using Turbo C++ (the old DOS version) to write and compile our assignments. I was wondering if there's a way to make is so I can write a C++ assignment on my Mac pretty much exactly the way I would on the PCs in the lab, able to build and test and debug from my laptop, only having to change things such as file paths and whatever else of the OS specific nature before handing in my assignment (we only have to hand in our source code and a copy of the results, printed out on paper). Below is our introductory program. I didn't write this, but I have to compile it and such, and I think this is pretty much standard for the semester of what I can expect.

Code:
```/*	Name: ME
COSC: 150
Program: prog1
Due Date: Sept. 7, 2006		*/

/*	Program Trip:
This program calculates the total cost and miles per gallon
of a vehicle, based on the miles traveled, fuel consumed,
fuel cost per gallon, and operating cost per mile.

Input: The total cost, miles traveled, total fuel consumed,
unit cost of the fuel, and operating cost per mile.

Output: The miles per gallon, total cost of the trip,
and the cost per mile.  */

/*	Variable Dictionary:

Miles: Total Miles Traveled
Fuel: Total gallons used
UnitFeulCost: Feul cost per gallon
UnitOperCost: Operating cost per mile
MPG: Miles per gallon
TFeulCost: Total feul cost
TOperCost: Total operating cost
TTripCost: Total trip cost
CostPerMile: Cost per mile	*/

#include<iostream.h>
#include<fstream.h>
#include<conio.h>
void main()
{
clrscr();
double
Miles, Fuel, UnitFuelCost, UnitOperCost, MPG, TFuelCost, TOperCost,
TTripCost, CostPerMile;
ofstream outfile;
outfile.open("c:result.dat");
//Read in the total miles and fuel

cout<<"enter the miles traveled and the gallons of fuel used "<<endl;
cin>>Miles>>Fuel;

//Calculate miles per gallon
MPG = Miles / Fuel;

//Read in the fuel cost per gallon
//and operating cost per mile.
cout<<"enter the fuel cost and operating cost "<<endl;
cin>>UnitFuelCost>>UnitOperCost;

//Calculate the total fuel cost, operating
//cost, trip cost, and cost per mile.
TFuelCost = Fuel * UnitFuelCost;
TOperCost = UnitOperCost * Miles;
TTripCost = TFuelCost + TOperCost;
CostPerMile = TTripCost / Miles;

//Print the outputs
outfile<<"\n Miles Per Gallon:  "<<MPG;
outfile<<"\n Total Trip Cost:  \$ "<<TTripCost;
outfile<<"\n Cost Per Mile:  \$ "<<CostPerMile;
outfile.close();
getch();
}```
Certain things I know will have to change, like the outfile.open("c:result.dat"); statement so that OS X doesn't freak out, and I'm hoping there are some kind of equivallent standard headerfiles like the one's I've included. If all else fails, maybe there's a DOS emulator out there somewhere that could somehow run Turbo C++ and somehow ... work. So if there are any ideas out there it would be very much appreciated.

2. ### MattDell macrumors 6502

Joined:
Sep 2, 2006
Location:
London, UK
#2
Have you tried X Code yet? Should have come with your Mac.

-Matt

3. ### szymczyk macrumors regular

Joined:
Mar 5, 2006
#3
Install the Xcode Tools. They should come with your copy of Mac OS X, either as a separate disc or as part of the OS X DVD. If you have broadband, you can download the latest version of the Xcode Tools by signing up for a free ADC membership at Apple's developer site.

After installing the Xcode Tools, you can get started writing code. For your course you can either create a C++ Tool project in Xcode, or compile your programs from the Terminal application using gcc. Both options will let you write standard C++ programs, which covers anything you would have to write in an introductory C++ course.

Your major problem is the fact that conio.h contains platform-specific code and is not supported on Mac OS X. You'll have to find replacements for the functions that are part of conio.h. You could use cin() and cout() on your Mac for testing purposes on your Mac to make sure your code works, then add the conio.h functionality later.

If you have an Intel Mac, Parallels provides emulation so you could run DOS and Turbo C++ if you need to. If you have a PowerPC Mac, Virtual PC provides DOS emulation.

4. ### mwpeters8182 macrumors 6502

Joined:
Apr 16, 2003
Location:
Boston, MA
#4
The only things really specific to DOS in there are the conio.h file, which enables the clrscr() function, and the path structure. Otherwise, it's standard C++, and should run on the mac.

I'd install the XCode tools, but just use a standard text editor (TextWrangler, SubEthaEdit , and Smultron are my favorites) and the command line (g++ -o [outputfile] [sourcefile]) to compile.

5. ### Soulstorm macrumors 68000

Joined:
Feb 1, 2005
#5
No, it won't. The guy needs to change the headers to the newer versions.

ifjake, C++ has changed a lot since the compiler you are using came out. You must remove the .h ending from the headers you are using.

6. ### pip11 macrumors member

Joined:
Apr 29, 2005
#6
I did basically the same thing several years ago in a high school Intro C++ course. We were using Turbo C++ v3 (so old that it is considered abandonware) with the same clrscr() and getch() functions. My programs compiled fine with Xcode with a stub version of conio.h that I wrote, which defined getch and clrscr to do nothing. Without using more complex libraries (readline, I believe), there is no way to get the same functionality of those functions.

g++ (compiler used by Xcode) will accept ".h" #include statements. In fact, when I tested it, g++ threw errors on ofstream if I didn't put in the .h. Also, main() must return an int, or gcc will give you an error.

It's been 5 years since I last used Turbo C--I can't believe anyone is still using it anymore (unless they just have very old systems and are afraid of a command line). As I remember, it ran terribly on NT/2K/XP anyway.

7. ### briantology macrumors 6502

Joined:
Jun 5, 2006
#7
I don't know if this answers anything, but this is my setup. I'm in a C programming foundations class. I use either Terminal or X11 on my Mac. I can login to my campus network (using comp and turing, ???). From there I can program just as I do in the lab, except for the lack of the toolbar (present on the SSH client in my lab). So I have Fugu, which basically acts as the toolbar (allowing you to transfer files from your directories in SSH to your desktop and vice versa). As far as any filetypes and other such stuff I have yet to learn, I haven't had any problems yet. But I've only written and compiled simple programs including alegbra and such.

8. ### mwpeters8182 macrumors 6502

Joined:
Apr 16, 2003
Location:
Boston, MA
#8
It'll work with the .h ending there.

9. ### ifjake thread starter macrumors 6502a

Joined:
Jan 19, 2004
#9
Yeah thanks guys, I've found out most of what you're saying by trying it myself. After a botched attempt of using a DOS emulator and version 2 of C++ Turbo (I got it to install and work, but some libraries were missing or something when I went to compile. Probably conio.h again. I may try version 3 from that link, pip11), I installed Xcode and just used that.
... yep, conio.h doesn't exist on Mac (though that stub header file with dummy functions sounds like a great idea, I just need to figure out how to do that. Right now I put the conio.h include declaration and conio.h specific functions into comments before building. Kinda a pain).
... using iostream (without the h) means instead of cout and cin I have to use std::cout and std::cin or something like that, which then would have to be changed back before I turn it in and such. Not that much fun. iostream.h and fstream.h worked fine, I just got a warning that they were old. I'm even able to leave the outfile.open statement the same. In the debug folder "c:results.dat" is just saved as a file called "c/results.dat".

I can't get void main() to work right though. I put return 0; at the end but that didn't catch for some reason. When I changed it to int main() then the return statement catched. This is a two line change and not that bad if that's just how it is.

The goal for me is to get it working so I hardly have to change anything in the code before turning in, while still getting it to compile so that I can work on it from the comfort of my own Mac. Going from Mac to Windows is sometimes tough, but from Mac to DOS is just unsettling.

pip11 or anyone else, if you could show me how to make that conio.h dummy file and get it to link up (is it #include"conio.h" or still #include<conio.h>?) that would be awesome. Thanks to all who helped.

10. ### iMeowbot macrumors G3

Joined:
Aug 30, 2003
#10
It may even be better for the purposes of that course to stick with the older *.h style, because the sample code seems to be missing other (not-so-) new features like namespaces too.

11. ### mwpeters8182 macrumors 6502

Joined:
Apr 16, 2003
Location:
Boston, MA
#11
Just remove the conio.h include, and get rid of the call to clrscr() and getch(). those are the only two things that depend on it, I think.

12. ### ifjake thread starter macrumors 6502a

Joined:
Jan 19, 2004
#12
Yeah I did that. I'm interested in making a "fake" header file with those functions so that I don't have to.