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 ERROR
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
#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().
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?
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?
#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'.