Program code problems

Hi i have a college project that i need finished for next wednesday. Its C++ and im ok with it but finding myself constantly stuck. The problem im having at moment is my program will not read the file. when i coment out readfile(); it will run but not display the info i want it to??? I will paste my code so far if its not clear what im asking for please say and i will try and clarify. Thanks in advance!


/*
*
*
*
*
*
**************************/
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <conio.h>
#include <string>
#include <windows.h>

using namespace std;

const int quit = 7;
const int MAX = 3;

struct tm *Sys_T = NULL;

//set up record structure
typedef struct
{
char bean_type [20];
int stock_amount;
char purch_date [12];
}coffee;

typedef coffee coffee_list[MAX];
int coffeecount = 0;

int logon();
void readfile();
void writefile();
void managermenu();
void barristamenu();
void date();
void display(coffee_list);
void processorder(coffee_list& );
void addstock();

// details of the actual array of records
coffee_list cl;


void main()
{ // code here
date();
int proceed;

// display date and time

proceed = logon();

if ((proceed ==1) || (proceed == 2))
{
readfile();
cout << " Welcome ";
_getch();
}
else
cout << " Incorect Logon details, try again! " << endl;

_getch();

if (proceed ==1)
{
cout << " Logged on as Manager ";
managermenu();
}


if (proceed ==2)
{
cout << " Logged in as Barrista ";
barristamenu();
}




}

int logon()
{ // code here

// read in username and password
// check and return value to main method

string username;
char password[13];
string pass;

cout << " Enter username ";
cin >>username;

cout << " Enter Password ";
cin >>pass;



//cout<< pass.compare("password");

if ((username.compare("manager")==0)&&(pass.compare("password")==0))
return 1;

if ((username.compare("barrista")==0)&&(pass.compare("password")==0))
return 2;

else
return 0;

}


void writefile()
{ // code here
ofstream outfile ("M:\\coffee.txt", ios::out );

for(int i = 0; !outfile.eof (); i++)
{
outfile << cl[i].bean_type << '\t'<< cl[i].stock_amount << '\t' << cl[i].purch_date << endl;

}

outfile.close ();

}


void readfile()
{ // code here
ifstream infile ("M:\\coffee.txt", ios::in );
if(!infile)
{
cout << "Unable to open file " << endl;
}

for(int i = 0; !infile.eof (); i++)
{
infile >> cl[i].bean_type;
infile >> cl[i].stock_amount;
infile >> cl[i].purch_date;

coffeecount++;

}

infile.close ();
}
void date()
{
SYSTEMTIME time;

GetLocalTime( &time );

cout << time.wMonth << "/" << time.wDay << "/" << time.wYear << endl;
cout << time.wHour << ":" << time.wMinute << endl;
}


void managermenu()
{ // screen output given place code araound
int choice;
do
{
system("cls");

cout << "\t\t MANAGER MENU \n";
cout << "____________________________________________________\n";
cout << "1. View current stock file\n";
cout << "2. Add more stock\n";
cout << "3. Barrista menu\n";
cout << "7 to Exit\n";

cout << "Enter choice: ";
cin >> choice;

switch (choice)
{
case 1:
display(cl);
break;
case 2:
addstock();
break;
case 3:
barristamenu();
break;
}

}
while (choice != quit);
}

void barristamenu()
{ // screen output given place code araound

int choice;
do
{
system("cls");
cout << "\t\t BARRISTA MENU \n";
cout << "____________________________________________________\n";
cout << "1. Make order\n";
cout << "2. Update stock file\n";
cout << "7 to Exit (or return to manager menu)\n";

cout << "Enter choice: ";
cin >> choice;
}
while (choice != quit);
}

void display(coffee_list coffee_list)
{ // code here
system("cls");
// print headings
cout << "BEAN TYPE \t STOCK AMOUNT\t PURCHASE DATE \n\n";

// for each record
for(int i = 0; i< MAX; i++)
{
cout << cl[i].bean_type << "\t\t" << cl[i].stock_amount << "\t\t" << cl[i].purch_date << endl;
}
_getch();


}

void processorder(coffee_list& )
{
int bean_choice;
int bean_pos;
int coffee_choice;
int portions;
string coffee;
int num_cups;
int total_portions;

system("cls");

cout << "select normal or decaffe" << endl;
cout << "1. Normal" << endl;
cout << "2. Decaffe" << endl;
cout << "Select: ";

// screen output given place code araound

cout << "\n\n";
cout << "Now select cofee type" << endl;
cout << "1. Americano" << endl;
cout << "2. Macchiato" << endl;
cout << "3. Espresso" << endl;
cout << "4. Double Espresso" << endl;
cout << "5. Cappuccino" << endl;
cout << "6. Mocha" << endl;
cin >> bean_choice;

cout << "Make selection: ";
cin >> coffee_choice;

// screen output given place code araound

}
void addstock()
{
string bean;
int amount;
SYSTEMTIME time;
GetLocalTime( &time );

// code here

}

Last edited on
You should use code tags, the code is pretty much unreadable without them.

it will run but not display the info i want it to???

And what is that info you want it to print...?
Hi thank you for replying! Code tags?? Do u mean coments??

Anyways i have a txt file containing data regarding coffee withing this is bean_type purchase_date stock_amount

when i comment out the readfile(); in the main method it will display the menus and i can swap between etc but the program will not read the txt file and display the data??

cheers and i appreciate ur help

Brendan
No, code tags are these: [code] and [/code]. Put your code between them.

when i comment out the readfile(); in the main method it will display the menus and i can swap between etc but the program will not read the txt file and display the data??

Well, obviously. When you comment something out, it will no longer be executed.
Yeah, but why will it not run at all when the readfile(); is active it does not like it but i can not see why not??
What makes you think readfile() doesn't run?
I still don't see any code tags, by the way.
Try ifstream infile ("M:\\coffee.txt", ios_base::in ); in your "Readfile()" function. Also you don't declare your cl global variable as an array but you try to use it as such in it looks like all of your functions, fix that. I see what you did there, but when you call date() you don't need to redeclare the data type like you do in readfile(). At this point your abuse of global variables, lack of code tags and having to scroll up and down constantly has caused me to lose interest.
Last edited on
Any update on this? Or should I let it die?
What's the format of your input file? From looking at your Readfile() function, I created a file called 'coffee.txt' like this:


Ground 5 20/04/2003
Fine 3 12/4/2009
Dark 20 1/2/2008


With a basic test, your code seems to work without crashing with this file. After it reads the file, I can use the manager menu to display the inventory. See if you can get that working. If your input format is different, perhaps post it.

As the others have pointed out, the code is far from perfect. I'll throw my two cents in too: there are a bunch of local variables that are unreferenced (at least at this stage of your development)...
'password', 'bean_pos', 'portions', 'num_cups', 'total_portions', 'amount'

No idea if that helps, but I hope so :).
Topic archived. No new replies allowed.