Hi, this is my first post everyone. I have just recently switched over to Visual Studio 2005, not by choice. I am having trouble with this program when I try to run it in VS 2005. I can't figure out a solution to the problem. Could anyone please help me!?
Code:
#include <malloc.h>
#include <fstream>
#include <iostream>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
using namespace std;
struct node
{
char item [20];
int a;
float b;
struct node *next;
};
typedef struct node *NODEPTR;
NODEPTR head;
NODEPTR tail;
NODEPTR previous;
NODEPTR current;
struct Data
{
char fruit[80];
float count;
float wholesale;
};
Data D;
//prcedure pre-definitions
int menu();
node *set_pointer();
void traverse_and_print();
void print_first();
void print_last();
void load_data();
void load_initial_data();
void get_data();
void save_file();
void load_file();
void look_me_up();
void out_of_stock();
void update_inventory();
//global variables
int first_entry = 1;
int need_to_save_file = 0;
void main()
{
int x;
while (x = menu())
{
switch (x)
{
case 1: //load from file
load_file();
break;
case 2: //add daily purchases
get_data();
if (first_entry)
{
load_initial_data();
}
else
{
need_to_save_file = 1;
}
break;
case 3: // to look up item
if (!first_entry)
{
look_me_up();
}
break;
case 4: // delete items no longer in stock
if (!first_entry)
{
out_of_stock();
need_to_save_file = 1;
}
break;
case 5: // update current inventory list at end of day
if (!first_entry)
{
update_inventory();
need_to_save_file = 1;
}
break;
case 6: // review last entry
if (!first_entry)
{
print_last();
}
break;
case 7: //save data to disk
if (!first_entry)
{
save_file();
need_to_save_file = 0;
}
break;
case 8: //print current inventory
if(!first_entry)
{
traverse_and_print();
}
break;
}
}
if (need_to_save_file)
{
cout<<">>>Warning<<< Saving File! "<<endl;
save_file();
}
cout<<"done!"<<endl;
}
int menu()
{
int choice;
cout<<"The Fruit Market "
<<'\n'
<<"Link List Processing "
<<'\n'
<<'\n'
<<"0. to Exit"
<<'\n'
<<"1. to Load from File"
<<'\n'
<<"2. to Add daily purchases"
<<'\n'
<<"3. to Lookup item"
<<'\n'
<<"4. to Delete out of stock items"
<<'\n'
<<"5. to Update inventory"
<<'\n'
<<"6. to Reprint last entry"
<<'\n'
<<"7. to Save to File"
<<'\n'
<<"8. to Print Current Data"
<<endl;
cout<<"?";
cin>>choice;
return (choice);
}
void print_first()
{
cout<<"Using head"<<endl;
cout<< head->item<<'\n'
<< head->a<<'\n'
<< head->b<<endl;
_getch();
}
void print_last()
{
cout<<"Item Lbs Wholesale Cost" << endl;
cout<< tail->item<<'\n'
<< tail->a <<'\n'
<< tail->b << endl;
_getch();
}
node *set_pointer()
{
node *p;
p = new node;
if(!p)
{
cout<<"OOOOOOOOOOOOOps";
_getch();
exit(-1);
}
return(p);
}
void traverse_and_print()
{
cout<<"Print List"<<endl;
current=head;
cout<<"Item LBS wholesale cost" << endl;
while (current != NULL)
{
cout<<current->item<<'\n'
<<current->a<<'\n'
<<current->b<<endl;
current = current->next;
}
_getch();
}
void get_data()
{
char temp[80];
cout <<"Enter Fruit Name: ";
cin >> D.fruit;
cin.getline (temp,80);
strcat_s(D.fruit,temp);
D.fruit[0] = toupper(D.fruit[0]);
if (D.fruit[strlen(D.fruit)-1] == 's')
{
cout<<"How many (whole) pounds of " <<D.fruit<<" did you buy? "<<endl;
}
else
{
cout<<"How many (whole) pounds of " <<D.fruit<<" did you buy? "<<endl;
}
cin>>D.count;
cout<<"What is the wholesale cost of each pound of "<<D.fruit<<" ? "<<endl;
cin>>D.wholesale;
}
void save_file()
{
ofstream FILE("fruit.txt",ios::out);
current = head;
while (current != NULL)
{
strcpy_s(D.fruit,current->item);
D.count = current->a;
D.wholesale = current->b;
FILE<<D.fruit<<" "<<D.count<< " "<<D.wholesale<<endl;
current = current->next;
}
}
void load_intitial_data()
{
tail=head=current=set_pointer();
strcpy_s(current->item,D.fruit);
current->a=D.count;
current->b=D.wholesale;
current->next = NULL;
first_entry = false;
}
void load_data()
{
previous = tail;
tail = current = set_pointer();
previous->next = current;
strcpy_s(current->item,D.fruit);
current->a = D.count;
current->b = D.wholesale;
current->next = NULL;
}
void load_file()
{
cout<<"Loading Data from Disk"<<endl;
ifstream FILE("fruit.txt",ios::in);
while (FILE>>D.fruit>>D.count>>D.wholesale)
{
cout<<D.fruit<<" "<<D.count<<" "<<D.wholesale<<endl;
if (first_entry)
{
load_initial_data();
}
else
{
load_data();
}
}
cout<<"Data Loaded! "<<endl;
_getch();
}
void look_me_up()
{
int x;
char item[80];
char item1[80];
cout<<"Enter name or part of name of fruit: ";
cin>> item;
current=head;
cout<<"Item LBS Wholesale cost"<< endl;
while (current !=NULL)
{
strcpy_s(item1,current->item);
for (x=0; x<strlen(item1);x++)
{
item[x]=tolower(item1[x]);
}
for (x=0; x<strlen(item); x++)
{
item[x]=tolower(item[x]);
}
if (strstr(item1,item) != NULL)
{
cout<<current->item<<" "<<current->a<<" "
<<current->b<<endl;
}
current = current->next;
}
_getch();
}
void out_of_stock()
{
int x;
char y_n;
char item[80];
char item1[80];
cout<<"Enter name or part of name of fruit: ";
cin>>item;
previous=current=head;
cout<<"Item LBS Wholesale cost"<<endl;
while (current != NULL)
{
strcpy_s(item1,current->item);
for (x=0; x<strlen(item1);x++)
{
item1[x]=tolower(item1[x]);
}
for (x=0; x<strlen(item);x++)
{
item[x]=tolower(item[x]);
}
if (strstr(item1,item) != NULL)
{
cout<<current->item<<" "<<current->a<<" "
<<current->b<<endl;
cout<<'\n'<<"Are we now out of "<<current->item<<"s?"<<endl;
cin>>y_n;
y_n=tolower(y_n);
if (y_n =='y')
{
if((current == head) & (current == tail))
{
first_entry = true;
}
if (current == head)
{
head = current->next;
}
if (current == tail)
{
previous->next=NULL;
tail = previous;
}
if ((current != head) & (current !=tail))
{
previous->next=current->next;
}
}
}
previous = current;
current = current->next;
}
}
void update_inventory()
{
int x;
char y_n;
char item[80];
char item1[80];
cout<<"Enter name or part of name of fruit: ";
cin>>item;
previous = current = head;
cout<<"Item LBS Wholesale cost"<<endl;
while (current != NULL)
{
strcpy_s(item1,current->item);
for(x=0;x<strlen(item1);x++)
{
item1[x]=tolower(item1[x]);
}
for (x=0;x<strlen(item);x++)
{
item[x]=tolower(item[x]);
}
if (strstr(item,item) !=NULL)
{
strcpy_s(D.fruit,current->item);
D.count=current->a;
D.wholesale=current->b;
cout<<D.fruit<<" "<<D.count<<" "<<D.wholesale<<endl;
if (D.fruit[strlen(D.fruit)-1] == 's')
{
cout<<'\n'<<"Do you want to update the number of" << current->item<<"?"<<endl;
}
else
{
cout<<'\n'<<"Do you want to update the number of" << current->item<<"s?"<<endl;
}
cin>>y_n;
y_n=tolower(y_n);
if (y_n == 'y')
{
cout<<'\n'<<"How many (whole) pounds of "<<D.fruit<<" do you have left? ";
cin >> current->a;
}
}
previous = current;
current = current->next;
}
}
Thank you.
George