Thread: Please check my C++

  1. #46
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    cin.ignore(boost::integer_traits<int>::const_max);
    Or alternatively, if you don't have boost
    cin.ignore(std::numeric_limits<int>::max());
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #47
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    cin.ignore(boost::integer_traits<int>::const_max);
    Or alternatively, if you don't have boost
    cin.ignore(std::numeric_limits<int>::max());
    lol! this is Chinese to me, anyway!, one day will understand boost.. or templates if this is what it is! for now will stick to cin.ignore() .....

  3. #48
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ya, but it is cin.ignore.
    I will be happy to explain it to you, if it's Chinese still.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #49
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Moving on!

    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

    Code:
    
    #include <iostream>
    #include <algorithm>
    #include <conio.h>
    #include "Menu.h"
    
    using namespace std;
    
    Menu::Menu() {};
    
    void Menu::titleDescription()
    {
    	clrscr();
    	printf("\t\t\t\t&#201;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#187;\n");
    	printf("\t\t\t\t&#186; FLEET DETAILS &#186;\n");
    	printf("\t\t\t\t&#200;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#188;\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&#201;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#187;\n");
    	printf("\t\t\t\t&#186; PROGRAM LIMITATIONS &#186;\n");
    	printf("\t\t\t\t&#200;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#188;\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&#201;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#187;\n");
    		printf("     \t\t&#186;          FLEET DETAILS        &#186;\n");
    		printf("     \t\t\314&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;\271\n");
    		printf("     \t\t&#186;            Options            &#186;\n");
    		printf("     \t\t&#186;                               &#186;\n");
    		printf("     \t\t&#186;1. Description of Program      &#186;\n");
    		printf("     \t\t&#186;                               &#186;\n");
    		printf("     \t\t&#186;2. Read Fleet from File        &#186;\n");
    		printf("     \t\t&#186;                               &#186;\n");
    		printf("     \t\t&#186;3. Display List of cars        &#186;\n");
    		printf("     \t\t&#186;                               &#186;\n");
    		printf("     \t\t&#186;4. Search Car by Model & Make  &#186;\n");
    		printf("     \t\t&#186;                               &#186;\n");
    		printf("     \t\t&#186;5. Add new car details         &#186;\n");
    		printf("     \t\t&#186;                               &#186;\n");
    		printf("     \t\t&#186;6. Update Fleet File           &#186;\n");
    		printf("     \t\t&#186;                               &#186;\n");
    		printf("     \t\t&#186;7. Edit Flee File              &#186;\n");
    		printf("     \t\t&#186;                               &#186;\n");
    		printf("     \t\t&#186;8. Edit Flee File              &#186;\n");
    		printf("     \t\t&#200;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#188;\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);
    }
    Q. this line
    Code:
    CarFleetIterator iterate=fleet.begin();
    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?

    here'sshowFLeetList()

    Code:
    // ---------------------------------------------------------------
    // Implementing function to display fleet of cars
    
    void Menu::showFleetList(CarFleetIterator iter, bool searched)
    {
    	sortAlpha();
    	
    	clrscr();
    	printf("\t\t\t\t&#201;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#187;\n");
    	printf("\t\t\t\t&#186; FLEET DETAILS &#186;\n");
    	printf("\t\t\t\t&#200;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#205;&#188;\n");
    
    	// **************** HEADINGS *************** 
    	printf("\n\n&#37;-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);
    		}
    	}
    }
    Last edited by csonx_p; 07-10-2008 at 08:09 AM.

  5. #50
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    Note that your << and >> operators render the read and write members useless.
    Why? Because you can do this:

    std::ofstream out("my file.txt");
    Car car;
    out << car;
    std::ifstream in("my file.txt");
    in >> car;

    It doesn't matter if it's the keyboard of a file, because both derive from std::i/ostream, it works. It's a good example of polymorphism.
    I'm busy looking at this closely and have questions...
    1. Overloaded operators accept the stream as arguments and enum, how can you pass object Car to them?
    2. Your examples illustrates only for writing/reading from files, how to i use these for keyboard & screen?

  6. #51
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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!]
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #52
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    I'm busy looking at this closely and have questions...
    1. Overloaded operators accept the stream as arguments and enum, how can you pass object Car to them?
    2. Your examples illustrates only for writing/reading from files, how to i use these for keyboard & screen?
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #53
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    (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.
    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"...

  9. #54
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by csonx_p View Post
    I suppose one can't overload the same operator more than once, stand for corrections...
    Of course you can. It's called overloading. It's available for both functions as well as operators.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #55
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Of course you can. It's called overloading. It's available for both functions as well as operators.
    As long as the return type or parameters are different types that is. You can't have two functions like this:
    Code:
    int foo(int x)
    {
       return x + 2;
    }
    
    ...
    int foo(int x)
    {
       return x * 2;
    }
    You can have:
    Code:
    int foo(int x)
    {
       return x + 2;
    }
    
    float foo(int x)
    {
        return x * 2;
    }

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #56
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    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!]
    1 problem though.. I use a boolean to decide whether to file or to screen... How do i pass this to the overloaded operator? Note that to file has less fields than to file....

  12. #57
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #58
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    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.
    i see... will be tricky to handle the extra fields though

  14. #59
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #60
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    i see... will be tricky to handle the extra fields though
    I don't really see the point of why you need the "extra fields"... Can you explain why inputting from keyboard should be different [in which fields are being filled in, that is] than reading from a file?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07:07 PM
  3. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 01:52 AM
  4. A way to check for Win98 or WinXP
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-31-2002, 11:06 AM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2002, 11:21 PM