-
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. :confused: 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;
}
-
-
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.
-
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().
-
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();
}
-
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.
-
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.
-
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.
-
:p 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 :(
-
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();
}