c++ code help (noob)

hi guys im new to programming but i would like to know if you guys can help me with some code. im trying to write a program that asks for details of a car and saves those details per car(object). then i want it to make and open a .txt file and save those details. i also want to learn how to reopen the files and make the program read them. so how do i create a new object then define the strings i have in my Car class oh and i was just messing around trying to see what i can do with that exp array. but plz guise any help would be appreciated im a huge noob because im JUST starting. (: in my cars. h i have
#include<iostream>
#include<string>
#include<string.h>
using namespace std;

class Car
{
public:
int price;
int autovin;
int year;
string make;
string model;
string exterior;//exterior color
string interior;//interior color
string condition;//the car's condition
int toint();
string tostring();





};
in my main.cpp i have this
#include<iostream>
#include<string>
#include"Cars.h"
#include<string.h>
using namespace std;


int main()
{

Car one();
Car.tostring;










};
in my cars.cpp i have
#include"Cars.h"
#include<iostream>
#include<string>
#include<string.h>
using namespace std;


int Car::toint()
{
cout << "Please enter the price you want for the car." <<endl;
cin >> price;
cout << "Please enter the Autovin number." <<endl;
cin >> autovin;
cout << "Please enter your car's year." <<endl;
return (price, year, autovin);


}

string Car::tostring()
{

cout << "Please enter the make of the car." <<endl;
cin >> make;
cout << "Please enter the model." <<endl;
cin >> model;
cout << "Please enter the exterior color of the car." <<endl;
cin >> exterior;
cout << "Please enter the interior color of the car." <<endl;
cin >> interior;
cout << "Please enter the cars condition." <<endl;
cin >> condition;
string exp [5] = {make, model, exterior, interior, condition};
cout << exp <<endl;
return (make, model, exterior, interior, condition);


}
Please do not put using namespace std; in your header. It's very bad form; people include headers without reading the contents (since that's what they're for) and if a header has a using namespace std; in it, the user won't even know, and it could cause them problems.

then i want it to make and open a .txt file and save those details

http://www.cplusplus.com/doc/tutorial/files/

Hi all!

And next time, please put the code in code form (select the code and press the <> button). That makes it way easier to read.
http://www.cprogramming.com/tutorial/lesson10.html
And that will be useful.
Good luck!
With namespace std, if you are just using cout & cin you can have

1
2
using std::cout
using std::cin


at the front of your file.

Is this all of your code? I don't see a constructor for Car object.

In Main:

1
2
Car one();
Car.tostring;


The first staement create a new object of type Car. The () means it's calling the the default contructor with no arguements. You don't have a default contructor (unless you haven't shown it to us). So the program doesn't actualy do anything. Usually we create new objects with the new operator. This will return a pointer, which you use to access it's members.

If you don't do this, then you access public members via the object. So the second statement above should be :

 
one.tostring;


Constructors are used to initialise variables. A good way of doing things is to have a constructor call an init fuction, then have other functions that process or calculate the info. This brings me to the names of your functions - they are misleading. To me and a lot of other people ToInt implies conversion to an Integer. A better choice might be SetIntValues and SetStringValues, both of which are called from the Init function in the constructor.

Usually we have public get & set functions to work with private member variables.

I suggest you read up about constructors, overloading, private, public, protected variables.

Hope this helps, let us know how you get on.


Topic archived. No new replies allowed.