Fstream Help

Hi, so I need help, with this kind of "library"(in the physical use of the word)
that i'm trying to do.
I need to store "books", keep them stored till the user asks to delete them. I think that's the gist of it. Here's the code, my bad if you find it crude.

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <time.h>

using namespace std;

struct Numbers
{
string asgard;
string loki;
}lo;

int goon;
int oreo = 0;
int nine;
int air;


int main()
{
char envy[100];
char fear[100];
char ill[100];
int bob = 0;
int sun = 0;
int moon = 1;

string line;
string Names[50];
string num("0123456789");

while (sun < moon)
{

cout << "Choices to make: \n";


cout << "Option 1: View Books\n";
cout << "Option 2: Add Books.\n";
cout << "Option 3: Delete all files.\n";
cout << "Option 4: End Program.\n";

cout << "Option: ";

cin >> lo.asgard;

while (lo.asgard.find_first_not_of(num) != string::npos)
{
cout << "Only numbers.\n";
cin >> lo.asgard;
}
stringstream(lo.asgard) >> air;

system("cls");

std::ifstream ifs;

switch (air)
{
case 1:

if (oreo == 0)
{
cout << "No Books \n";
}

else
bob = 0;

while (goon < oreo)
{
goon++;
string line;
ifstream Books(Names[bob]);
bob++;

if (Books.is_open())
{
while (getline(Books, line))
{
cout << line << '\n';
}
Books.close();
}
cout << "\n\n";
}
system("pause");
system("cls");
break;
case 2:
cout << "Add Books\n";

cout << "How many books do you want to add?" << endl;
cin >> lo.asgard;

while (lo.asgard.find_first_not_of(num) != string::npos)
{
cout << "Only numbers.\n";
cin >> lo.asgard;
}
stringstream(lo.asgard) >> nine;

bob = 0;

while (bob < nine)
{

oreo++;

cout << "Title: ";
cin >> envy;
Names[bob] = envy;
bob++;

cout << "Author: ";
cin >> ill;

cout << "Subject: ";
cin >> fear;

ofstream Jooks(envy);

if (Jooks.is_open())
{
if (Jooks.is_open())
{
Jooks << "Title: " << envy << "\n";
Jooks << "Author: " << ill << "\n";
Jooks << "Subject: " << fear << "\n";
Jooks.close();
}
}
cout << "\n\n";
}
break;
case 3:

cout << "Do you want to delete the books? (yes or no)" << endl;
for (int z = 1; z < 2;)
{
cin >> lo.loki;
if (lo.loki == "yes")
{
goon = 0;
bob = 0;

while(goon < oreo)
{
char sol[100];
strcpy_s(sol, Names[bob].c_str());
bob++;
sun++;
remove(sol);
cout << sol;
}
oreo = 0;
cout << "Books Deleted\n";

system("pause");
system("cls");
break;

}
else if (lo.loki == "no")
{
break;
}
else
{
cout << "Only yes or no please. ";
}

}
system("cls");
break;

case 4:


return 0;

}
}
}
Hello Bengiepr23,

Welcome to the forum.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

So are you asking where you should write to the file or how to do it?

You may think your variable names are cute or funny, but it makes it very hard for someone like me to read and figure out. C and C++ allows 256 characters for a variable name. Do not be afraid to use a descriptive name the says what the variable does. Just a thought.

While I am here it is best to stay away from "system" anything. Not only are your system call specific to Windows not everyone can use them. See http://www.cplusplus.com/forum/beginner/1988/ for some ideas.

Hope that helps,

Andy
Topic archived. No new replies allowed.