cin.ignore(boost::integer_traits<int>::const_max);
Or alternatively, if you don't have boost
cin.ignore(std::numeric_limits<int>::max());
Printable View
cin.ignore(boost::integer_traits<int>::const_max);
Or alternatively, if you don't have boost
cin.ignore(std::numeric_limits<int>::max());
Ya, but it is cin.ignore.
I will be happy to explain it to you, if it's Chinese still.
Alright, am getting somewhere with my code... Haven't shown you the file where things happen... Going to show you one function as yet, the rest later....
Menu.cpp
Q. this lineCode:
#include <iostream>
#include <algorithm>
#include <conio.h>
#include "Menu.h"
using namespace std;
Menu::Menu() {};
void Menu::titleDescription()
{
clrscr();
printf("\t\t\t\tÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»\n");
printf("\t\t\t\tº FLEET DETAILS º\n");
printf("\t\t\t\tÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ\n");
printf("\n\nThis program requests a file name in which a fleet of cars is saved.\n\n");
printf("A menu which allows the user an option to view the fleet, "
"\nsearch for a car, add new car, and update file is provided.\n\n");
pause();
clrscr();
programLimits();
}
// ---------------------------------------------------------------
// Implementing the user instructions function
void Menu::programLimits()
{
clrscr();
printf("\t\t\t\tÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»\n");
printf("\t\t\t\tº PROGRAM LIMITATIONS º\n");
printf("\t\t\t\tÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ\n");
printf("\n\nINPUTS:\n\n");
printf("FileName: FleetData.txt\n");
printf("Engine Capacity: (i.e.) 1.8L must be 1800\n");
printf("Dates: day, month, year\n");
printf("Tank capacity: Value 1 for filled, and 0 for unfilled\n\n");
pause();
}
void Menu::clrscr()
{
system("cls");
}
void Menu::pause()
{
puts("\n\n");
system("pause");
}
// ---------------------------------------------------------------
// Run will handle the user choice and run desired operation
void Menu::run()
{
bool again = true, found = false;
CarFleetIterator iterate=fleet.begin();
do
{
clrscr();
printf("\n \t\tÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»\n");
printf(" \t\tº FLEET DETAILS º\n");
printf(" \t\t\314ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\271\n");
printf(" \t\tº Options º\n");
printf(" \t\tº º\n");
printf(" \t\tº1. Description of Program º\n");
printf(" \t\tº º\n");
printf(" \t\tº2. Read Fleet from File º\n");
printf(" \t\tº º\n");
printf(" \t\tº3. Display List of cars º\n");
printf(" \t\tº º\n");
printf(" \t\tº4. Search Car by Model & Make º\n");
printf(" \t\tº º\n");
printf(" \t\tº5. Add new car details º\n");
printf(" \t\tº º\n");
printf(" \t\tº6. Update Fleet File º\n");
printf(" \t\tº º\n");
printf(" \t\tº7. Edit Flee File º\n");
printf(" \t\tº º\n");
printf(" \t\tº8. Edit Flee File º\n");
printf(" \t\tÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ\n\n");
printf("\n\n\t\tPlease enter the NUMBER of your choice: ");
switch(_getch())
{
case DESCRIBE: titleDescription();
break;
case READF: readFleetFromFile();
pause();
break;
case DISPLAY: showFleetList(iterate, found);
pause();
break;
case SEARCHF: { // Search by make & model
puts("\n\n\t\tNot yet in operation!!");
if( (iterate = searchCarDetails(found)) == fleet.end() )
{
printf("\n\n\tTry searching again!");
}
else
if(found)
{
showFleetList(iterate, found); // Display found record
}
pause();
break;
}
case ADDCAR: AddNewCar();
pause();
break;
case UPDATEF: updateFleetFile(); // Save changes to text file
pause();
break;
case EDITF: EditFleet();
pause();
break;
case EXITP:
clrscr();
exit(1);
default: again = false;
break;
}
}
while (again);
}
doesn't seem to do the right job. After option 2 (READF), fleet gets initialized with all data from file... But, iterate doesn't seem to carry the data.... Cause the moment i press option 3 (DISPLAY), no proper data is passed... am i doing anything wrong?Code:CarFleetIterator iterate=fleet.begin();
here'sshowFLeetList()
Code:// ---------------------------------------------------------------
// Implementing function to display fleet of cars
void Menu::showFleetList(CarFleetIterator iter, bool searched)
{
sortAlpha();
clrscr();
printf("\t\t\t\tÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»\n");
printf("\t\t\t\tº FLEET DETAILS º\n");
printf("\t\t\t\tÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ\n");
// **************** HEADINGS ***************
printf("\n\n%-17s%-12s%-13s%-12s%-8s\n\n", "Car Make/Model", "Year Model", "Engine Size",
"Return Date", "Fuel Tank" );
// **************** HEADINGS ***************
// Display one one record if fleet was searched
if (iter!=fleet.end() && searched)
{
iter->write(cout, true);
}
else
{
// Display fleet details to the screen
for(iter=fleet.begin(); iter!=fleet.end(); iter++)
{
iter->write(cout, true);
}
}
}
1)
By overloading operator << and >> for Car.
ostream& operator << (ostream& rLhs, Car& rRhs);
istream& operator >> (istream& rLhs, Car& rRhs);
[Some may require some const somewhere, but I'm not sure where to avoid ambiguities.]
2)
Car car;
cout << car;
cin >> car;
[Remember, the keyboard and screen are streams, too, so they work just the same as files!]
There is no difference between the "keyboard/screen" and a file in this aspect. They are just different devices at the low level device driver, but the cin is a stream just like a ifstream("myfile.txt") would be.
You would have to write a operator >> for a car object. It would look very similar to your read function, using the operator >> on the individual fields (and you can either keep the tankstatus operator, or write the code to read it as an integer and then transfer it to the enum inline in that function.
Note that in either solution (operator >>() or read()), you would have to put some sort of whitespace between each element of the car.
--
Mats
This is where my mind is battling... Currently the overloaded '<<' is handling this (TankStaus). I suppose one can't overload the same operator more than once, stand for corrections... So you suggesting i should replace the current overloading with what's on read & write? Except that to read & write from Car members i will be using a . operator i.e "fout << car.make"...
As long as the return type or parameters are different types that is. You can't have two functions like this:
You can have:Code:int foo(int x)
{
return x + 2;
}
...
int foo(int x)
{
return x * 2;
}
Code:int foo(int x)
{
return x + 2;
}
float foo(int x)
{
return x * 2;
}
--
Mats
You can't pass a boolean to the operator.
But the thing is that the class is an object - it should accept whatever you throw at it.
It's your responsibility as the user of the class to feed it correct data and make it work correctly.
In other words, your code can check if it's the keyboard or not or such, then feed the proper argument to the operator >> or << of the class.
Maybe, and maybe not.
A good rule of C++ that I always use is to abstract (hide reusable code inside a user friendly interface) and encapsulate (encapsulate functionality inside classes with a good public interface).
Doing this makes it easy to create polymorphic, powerful code that takes little effort to use correctly.
Try it. If nothing else, it's fun to experiment with.