0

So I'm trying to write into a variable in two different set arguments using this code, but it's not allowing me to enter the value for the 2nd cin.

#include<iostream>
using namespace std;

int main()
{
int T1,T2,T3,D,car_type;
double total;

cout<<"Please enter the values for T1 T2 T3 D: ";
cin>>T1,T2,T3,D;
cout<<endl;
cout<<"Please Choose a Car where 1 = Honda, 2 = Toyota, 3 = Mercedes: ";
cin>>car_type;
if (car_type = 1)
{
    total = T1*D;
    cout<<"The cost of a full tank for a Honda is "<<total<<" Dirhams.."<<endl;
}/* else
if (car_type = 2)
{
    total = T2*D;
    cout<<"The cost of a full tank for a Toyota is "<<total<<" Dirhams.."<<endl;
} else
if (car_type = 3)
{
    total = T3*D;
    cout<<"The cost of a full tank for a Mercedes is "<<total<<" Dirhams.."<<endl;
} */
}

Can you please tell me where I'm going wrong?

Rikesh
  • 26,156
  • 14
  • 79
  • 87
user3025371
  • 37
  • 1
  • 5

2 Answers2

6

In C++, the comma can be present in both declarations (variable declarations, parameter lists) and expressions. In expressions, it's an operator that evaluates the operands and returns the last one.

Basically

cin>>T1,T2,T3,D;

evaluates T1,T2,T3 and D and then returns D, which means, in your particular case, it's equivalent to

cin>>D;

To correctly chain operator>> on cin, do:

cin >> T1 >> T2 >> T3 >> D;
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
0
cin>>T1,T2,T3,D;

is not doing what you expext it to do. To understand that read @Luchian Grigore's answer. This is what your code should be

cin>>T1>>T2>>T3>>D;

To accept multiple values using cin, use the above code ( That is, separate the variables using >> and not using , ).

Also change

if (car_type = 1)

to

if (car_type == 1)

= is an assignment operator, in your code it assigns the value 1 to car_type. To check for equality use ==.

Arun A S
  • 6,421
  • 4
  • 29
  • 43