Hi plz explain m a Beginner in c++

class car
{
int millage;
int cost;
public:
void get()
{
cin>>millage>>cost;// THE INPUT I ENTERED HERE IS NOT GETTING STORED IN MILLAGE I ALSO TRIED //cin>>this->millage>>this->cost; BUT NO USE I GOT //SAME O/P;
}
void disp()
{
cout<<"millage"<<millage<<"\n"<<"cost"<<cost;//Y SOME JUNK VALUES ARE GETTING PRINTED
}
};
void main()
{
car zen;
zen.get();
zen.disp();
}

output:
35
25000

millage=8532 //JUNK ERROR
cost=7654 //JUNK ERRO
R
PLZ HELP ME !

i MY SELF TRIED TO SORT OUT THE ABOVE CODING AND GOT CORRECT MILLAGE AND COST PRINTED.

I MODIFIED CODE AS
void get()
{
int m,n;
cin>>m>>n;
millage=m;cost=n;
}

BUT PLZ SOMEONE EXPLAIN Y THIS IS WORKING EXACTLY

THANK U !
regards Vandhana!.
Have you tried:
1
2
cin >> millage;
cost = millage;
?

(Im also a noob, just trying to help)
Hi vandhanarm,

the functionality of your code works fine, although main should return an int:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

using std::cout; using std::cin;
using std::endl;

class car
{
  int millage;
  int cost;
public:
  void get()
  {
    cin >> millage >> cost;
  }

  void disp()
  {
    cout << "millage " << millage
         << "\n" << "cost " << cost << endl;
  }
};

int main()
{
  car zen;
  zen.get();
  zen.disp();
  return 0;
}



90 89
millage 90
cost 89


However, I would utilise istream and ostream objects as parameters and return values of your accessor and read functions to increase I/O flexibility. Also, you could add a constructor for car that allows you to initialise objects directly by, e.g., having it call get().

Hope that helps.
You could overload the input stream operator and output stream operator and then use std::cin and std::cout directly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>

class car {
	int milage;
	int cost;
	
public:
	car(void) milage(0), cost(0) {
	}
	friend std::istream &operator >> (std::istream &in, car &c) {
		in>>c.milage>>c.cost;
		return in;
	}
	friend std::ostream &operator << (std::ostream &out, const car &c) {
		out << "millage " << c.millage
			<< "\n" << "cost " << c.cost << endl;
		return out;
	}
};

int main()
{
  car zen;
  std::cin>>zen;
  std::cout<<zen;

  return 0;
}
 
 
Last edited on
Hi dangrr888 and jloundy .

Thank you so much.I got idea about how to rectify the probs on seeing both of your reply.It was useful to me.

But plz can any one explain me.

why i cant able to directly use data members in cin
class car
{ int millage; int cost;
public:
void get()
{
cin>>millage>>cost; //y cant i wuld't get this properly initialized.
}};
//why cin>>millage>>cost ; statement is not storing values in millage and cost properly?

and why i have to use third variable to initialize millage and cost?

void get()
{
int a,b;
cin>>a>>b;
millage=a;cost=b;//why this way of initialization is proper storing values in millage and cost
}
plz explain
i)whether i cant able to initialize object details through cin at all?
ii)or since it is declared private member its not working with cin?

Thanks & regards
Vandhana.

for your kind information



I am executing my code in turbo c++ (16 bit compiler). not in 32 bit compiler.
void get()
{
cin>>millage>>cost;// THE INPUT I ENTERED HERE IS NOT GETTING STORED IN MILLAGE I ALSO TRIED //cin>>this->millage>>this->cost; BUT NO USE I GOT //SAME O/P;
}

I would simply rewrite the function as so..
1
2
3
4
5
void get(int mile, int price)
{
	mileage=mile;
	cost=price;
}


This way would be much clearer. Usually when you set values of data members you pass the values as parameters. For instance, say we have the following declaration:
 
car myCar;


To set the value of mileage and cost, we just have to call the function get:
 
myCar.get(33454, 23000);


After this statement, mileage==33454 and cost==23000.
hi, #include<iostream.h>
class car
{ int millage; int cost;
public:
void get()
{
cin>>millage>>cost;
}};
void main()
{
car alto; alto.get();
}
i need explanation why i get this warning . i have got much ideas to rectify it but i need explanation y it is showing this warning.
Plz any one explain me.

cpp8:Temporary used for parameter 1 in call to istream::operator>>(int &);
cpp9:Temporary used for parameter 1 in call to istream::operator>>(int &);
cpp8:Temporary used for parameter 1 in call to istream::operator>>(int &);
cpp9:Temporary used for parameter 1 in call to istream::operator>>(int &); this is the waring i got .what is this warning about? and why it is showing?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<iostream.h>
#include<conio.h>
class car
{
	int mileage;
	int cost;
	public:
	void input()
	{
		cout<<"Enter mileage : ";cin>>mileage;
		cout<<"Enter cost    : ";cin>>cost;
	}
	void output()
	{
		cout<<"Mileage is    : "<<mileage<<endl;
		cout<<"Cost is       : "<<cost;
	}
};
void main()
{
	clrscr();
	car alto;
	alto.input();
	alto.output();
	getch();
}


Hey vandhanarm...actually i think get is an inbuilt function for iostream and i suggest you change the function like mine as i have shown...
Also please post your coding in tags..
Your problem sounds like as if 'millage' is already defined as something else. The compiler seems to generate a temporary variable when using 'millage'.
wow Mr.rambo1177
Thank you so much am so happy on seeing its working .
Topic archived. No new replies allowed.