File I/O Help

This is a discussion on File I/O Help within the C++ Programming forums, part of the General Programming Boards category; I am making a simple program for my Dad's work. It is supposed to allow the user to add hardware(doors, ...

  1. #1
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719

    File I/O Help

    I am making a simple program for my Dad's work. It is supposed to allow the user to add hardware(doors, locks, etc.) to the different schools. The user inputs the hardware in the function addhardware, and it is supposed to save it under showlist, so when you go to it you have a list of all the hardware. I only did it with one of the schools, to test it. But it's not working. I know I did something very wrong. It's like I know what to do, but i cant do it. Here is the code:


    Code:
    #include <iostream>
    #include <windows.h>
    #include <fstream>
    
    using namespace std;
    
    void menu();
    void schools();
    void school1();
    void school2();
    void school3();
    void hardware();
    void addhardware();
    void deletehardware();
    void showlist();
    
    int main()
    {
        menu();
        cin.get();
        cin.ignore();
        return 0;
    }
    void menu()
    {
         int selection;
         system("CLS");
         cout << "-------------------------------[ Hardware ]--------------------------------" << endl << endl;
         cout << "1. List of Schools" << endl;
         cout << "2. Exit" << endl;
         cin >> selection;
         switch (selection){
                case 1:
                     schools();
                     break;
                case 2:
                     return;
                     break;
                default:
                        system("CLS");
                        cout << "Error: Incorrect Input!" << endl;
                        Sleep(1000);
                        menu();
                        break;
                        }
                        }
         void schools()
         {
              system("CLS");
              int choice;
              cout << "------------------------------[ List of Schools ]-------------------------------" << endl << endl;
              cout << "1.  High School" << endl;
              cout << "2.  High School" << endl;
              cout << "3.  Middle School" << endl;
              cin >> choice;
              switch (choice){
                     case 1:
                          school1();
                          break;
                     case 2:
                          school2();
                          break;
                     case 3:
                          school3();
                          break;
                     default:
                             system("CLS");
                             cout << "Error: Incorrect Input!" << endl;
                             Sleep(1000);
                             schools();
                             break;
                              }
                               }
              void school1()
              {
                   int selection;
                   system("CLS");
                   cout << "1. Hardware" << endl;
                   cout << "2. Go Back" << endl;
                   cin >> selection;
                   switch (selection){
                          case 1:
                               hardware();
                               break;
                          case 2:
                               schools();
                               break;
                          default:
                                  system("CLS");
                                  cout << "Error: Incorrect Input!" << endl;
                                  Sleep(1000);
                                  school1();
                                  break;
                                  }
                                  }
             void school2()
              {
              int selection;
              system("CLS");
              cout << "1.Hardware" << endl;
              cout << "2.Go Back" << endl;
              }
              void school3()
              {
                   system("CLS");
                   cout << "1.Hardware" << endl;
                   cout << "2.Go Back" << endl;
                   }
                   
            void hardware()
             {
                       int selection;
                       system("CLS");
                       cout << "---------------------------------[ Hardware ]-----------------------------------" << endl << endl;
                       cout << "1. Add Hardware" << endl;
                       cout << "2. Delete Hardware" << endl;
                       cout << "3. Show Hardware" << endl;
                       cout << "4. Go Back" << endl;
                       cin >> selection;
                       switch (selection){
                              case 1:
                                   addhardware();
                                   break;
                              case 2:
                                   deletehardware();
                                   break;
                              case 3:
                                   showlist();
                                   break;
                              case 4:
                                   schools();
                                   break;
                              default:
                                      system("CLS");
                                      cout << "Error: Incorrect Input!" << endl;
                                      Sleep(1000);
                                      hardware();
                                      break;
                                      }
                                      }
             
                 void addhardware()
                 {
                      system("CLS");
                      char addhardware[256];
                      cout << "--------------------------------[ Add Hardware ]--------------------------------" << endl << endl;
                      cout << "Add Hardware:";
                      cin >> addhardware;
                      ofstream file ("hardware.txt", ios::app);
                      file << addhardware << endl;
                      file.close();
                      ifstream fileb("hardware.txt");
                      fileb >> addhardware;
                      cout << addhardware << endl;
                     ifstream a_file ( "hardware.txt");
                     if ( !file.is_open()){
                        
                          }
                          else {
          
                          }
                          }
                 void deletehardware()
                           {
                                system("CLS");
                                cout << "------------------------------[ Delete Hardware ]-------------------------------" << endl << endl;
                                }
                 void showlist()
                 {
                      system("CLS");
                      cout << "------------------------------[ Hardware List ]---------------------------------" << endl << endl;
                      
                      }
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  2. #2
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Can anyone help???????
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You only waited an hour before bumping the question.

    You didn't say what didn't work, what input you used, and a bunch of other information.

    Sorry, I don't have any other help to offer.

  4. #4
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    My problem is that I can't get the information i want into the correct file. When the user goes to Add Hardware I want the input from the user to be saved into a folder. Then, when the user goes to show list i want it to show the input from user. What I want to do is show the information from the file, in the function showlist().
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  5. #5
    Banned
    Join Date
    Jun 2005
    Posts
    594
    WoW lol sorry your code is a mess, but im gonna try and help
    you but im gonna do that by the following :

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    void menu(void);
    void addhardware(void);
    
    int main()
    {
    	menu();
    	return 0;
    }
    
    void menu(void)
    {
    	int choice;
    	cout << "0.  Exit" << endl;
    	cout << "1.  Add New Hardware" << endl << endl << endl;
    	cout << "Choose an Option : ";
    	cin >> choice;
    	cin.ignore();
    	switch(choice)
    	{
    		case 0 :	break;	
    		case 1 :	addhardware();
    					break;
    		default:	cout << "No valid Choice!";
    					cin.get();
    					break;
    	}
    }
    void addhardware(void)
    {
    	string hardware;
    	ofstream fileout("hardware.txt", ios::app);
    	cout << "Name the hardware to add  : ";
    	getline(cin, hardware, '\n');
    	if(!fileout.fail())
    	{
    		fileout << hardware << endl;
    		cout << "The following HARDWARE has been added successfully!" << endl;
    		cout << "1.  " << hardware << endl;
    	}
    	fileout.close();
    	cout << "Press any key to return to menu!" << endl;
    	cin.get();
    	menu();
    }

  6. #6
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Why is my code messed up? Sorry if I don't know the correct "form", but making fun of someone's style is not right. The code works. It was more organized than yours. The code you posted wasn't even what I was looking for. I've only been learning for about a month, so know sh** my code is going to be messed up, deal with it.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I still don't understand exactly what your problem is, but if you are just looking for help implementing showlist, I would suggest opening the file with an ifstream and outputting the information in it one line at a time.

    Also, you open the file "hardware.txt" three times in the addhardware function. I assume the two ifstreams were for debugging, but you should remove those now.

  8. #8
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Woo dont cry i just said its a mess, and it is a mess,
    i didnt say it was messed up, and if it works,
    why are you posting a question about
    it not working?

    And if youd take your head out of your, and look
    at my code for a second if you can understand it at all,
    show you a way to get your hardware form the user
    and put it in a file.

  9. #9
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    LOL this is the second argument I've read on these boards about coding style. It's not too bad I suppose; you have to give credit for the fact he actually knows how to use the tab key, or a whole lotta spaces. I've seen worse.

    Sorry I can't really help you with your problem though
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  10. #10
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Maybe this perhaps?

    Code:
    void showlist()
                 {
                      system("CLS");
                      cout << "------------------------------[ Hardware List ]---------------------------------" << endl;
                      fstream file_pointer;
                      file_pointer.open("hardware.txt",ios::in);
                      char array[81];
                      do
                      {
                          file_pointer>>array;
                          cout<<array<<endl;
                      }while(file_pointer.peek()!=EOF);
                      file_pointer.close();
                          
                      
                      
                 }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-30-2004, 11:21 PM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 04:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21