Jun 25, 2011 at 5:37am Jun 25, 2011 at 5:37am UTC
using this;
#include <iostream>
using namespace std;
#include <windows.h>
void gotoxy (int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
int main ()
{
int charge,count;
double hour;
double a,b,c,d,e;
cout << "CAR 1"<<endl;
cin >> a;
cout << "CAR 2"<<endl;
cin >> b;
cout << "CAR 3"<<endl;
cin >> c;
cout << "CAR 4"<<endl;
cin >> d;
cout << "CAR 5"<<endl;
cin >> e;
count = 1;
while (count <=5 ){
if ( count == 5)
hour = a;
if ( count == 4)
hour = b;
if ( count == 3)
hour = c;
if ( count == 2)
hour = d;
if ( count == 1)
hour = e;
if ( hour <= 3)
charge = 40.0;
if (hour > 19)
charge = 880;
if (hour > 3)
charge = 40.0 + ((hour-3) *10);
count = count + 1;
}
gotoxy (0,11);
cout << "CAR \t\t HOUR \t\t CHARGE"<<endl;
gotoxy (0,12);
cout << "1";
gotoxy (18,12);cout << a << "\t\t " << charge;
gotoxy (0,13);
cout << "2";
gotoxy (18,13);cout << b << "\t\t " << charge;
gotoxy (0,14);
cout << "3";
gotoxy (18,14);cout << c << "\t\t " << charge;
gotoxy (0,15);
cout << "4";
gotoxy (18,15);cout << d << "\t\t " << charge;
gotoxy (0,16);
cout << "5";
gotoxy (18,16);cout << e << "\t\t " << charge;
}
why does the charge value is the same as the other value ?
where does my code goes wrong ? i'm a little bit stuck here,
thank so much for answering :)
Jun 25, 2011 at 5:57am Jun 25, 2011 at 5:57am UTC
In your while loop you just keep overwriting "charge" with value for the next car, instead of printing or storing it somewhere.
I suggest you use a for loop for that and arrays, it will simply that code quite a bit.
Jun 25, 2011 at 10:37am Jun 25, 2011 at 10:37am UTC
#include <iostream>
usingstd :: cout;
usingstd :: cin;
usingstd :: endl;
usingstd :: fixed;
#include<iomanip>
usingstd :: setw;
using std :: setprecision;
int main ()
{
int count;
int x;
double charge;
double a;
double b;
double c;
double d;
double e;
cout<< “Enter the number of hours : “ ;
cout<< “CAR 1 : “ ;
cin>> a ;
cout<< “CAR 2 : “ ;
cin>>b ;
cout<< “CAR 3 : “ ;
cin>>c ;
cout<< “CAR 4 : “ ;
cin>>d ;
cout<< “CAR 5 : “ ;
cin>>e ;
cout<< setw950 << “CAR “ <<setw (10) << “HOURS” <<setw (15) << “CHARGE \n”;
cout<< fixed <<setprecision (2);
count = 1 ;
while (count <=5){
if ( count == 5)
x = e;
if ( count == 4)
x = d;
if ( count ==3)
x = c;
if ( count == 2)
x = b;
if ( count == 1)
x = a;
if ( x<=3)
charge =40.0
if (x=24)
charge = 880.0
if (x>3)
charge = 40.0 + ((x-3) *10)
cout<<setw (5) << count <<setw(10) << x <<setw (15) << “Php” << charge << “\n” ;
count = count + 1 ;
}
return 0;
}
in this code , the charge is not overwriting the 'charge' .. what is the difference in the two ?
i just simply change the setprecision to the gotoxy syntax..