Det ser rigtigt ud
Det virker stadig ikke.. Nu når jeg ikke engang at skrive navnet på den fil jeg vil skrive til?
Hele main:
#include "includer.h"
#include "products.h"
#include "classes.h"
int main()
{
cout << "Welcome to my store!" << endl;
cout << "Please enter your name, or if administrator enter admin." << endl;
//Vector
vector<string> NewProduct;
//Vores Ofstream og Istreams.
ifstream readProducts("Products.txt");
//Laver ny class
Persons persons;
//Laver ny structure
Products products;
//Pointer til classen Persons
persons.name = new char[80];
string Name = persons.name;
getline(cin, Name);
//Admin delen
if(Name == "admin" || Name == "Admin")
{
cout << "Please enter password: ";
string password;
getline(cin, password);
if(password == "admin")
{
const char *p;
string file;
string opportunity;
system("CLS");
cout << "You are now logged in as admin!" << endl;
cout << "You have 3 opportunities." << endl << "1) New product" << endl << "2) Browse products" << endl << "3) Search for a product" << endl;
cout << "Please enter the number on which opportunity you want: ";
getline(cin, opportunity);
p = opportunity.c_str();
if(p == "1")
{
cout << "Please enter file you wil write to. Normal file is Products.txt" << endl;
getline(cin, file);
products.newProduct(file.c_str());
}
else if(p == "2")
{
cout << "Please enter file you will browse from. Normal file is Products.txt" << endl;
getline(cin, file);
products.browseProducts(file.c_str());
}
}
// ADMINISTRATOR!
}
else
{
system("CLS");
cout << "Welcome " << Name << endl;
}
}
Hele productsFunctions.cpp
#include "products.h"
#include "includer.h"
#include <windows.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <stdio.h>
using namespace std;
void Products::newProduct(const char* Filename)
{
system("CLS");
string again;
string productName;
float price;
string priceString;
do
{
cout << "Enter the name of the new product: ";
getline(cin, productName);
cout << endl << "Enter price of the new product: ";
getline(cin, priceString);
stringstream SS(priceString);
SS >> price;
ofstream writeProducts;
writeProducts.open(Filename, ios::out | ios::app);
if(writeProducts.is_open())
{
writeProducts << "Name: " << productName << endl;
writeProducts << "Price: " << price << endl;
writeProducts.close();
}
cout << endl << "Add more products?" << endl << "Yes" << endl << "No" << endl;
getline(cin, again);
} while(again == "yes" || again == "Yes");
}
void Products::browseProducts(const char* Filename)
{
string results;
system("CLS");
cout << "Browsing Products.." << endl << endl;
Sleep(3000);
ifstream file;
file.open(Filename, ios::in | ios::beg);
if(file.is_open())
{
while(!file.eof())
{
getline(file, results);
}
}
}
og til sidst products.h
#include "includer.h"
class Products {
public:
Products() {};
// Nyt produkt som skal skrives ind i vector
void newProduct(const char* Filename);
// Mulighed for at søge vectoren igennem
int searchProduct(char* Filename);
void browseProducts(const char* Filename);
};
Jeg fatter det ik s: