Thread: Help linked lists Visual Studio 2005

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    6

    Question Help linked lists Visual Studio 2005

    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

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    You should go for some debugging to figure out the problem. Also, you never check if the file is valid. Are you sure it is (maybe you switched to your home computer and forgot it)?

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    6
    These are the errors I recieve after I made sure the file is there, and have verified it.

    1>------ Build started: Project: Final Project, Configuration: Debug Win32 ------
    1>Linking...
    1>finalProject.obj : error LNK2005: _main already defined in final project.obj
    1>finalProject.obj : error LNK2005: "struct node * current" (?current@@3PAUnode@@A) already defined in final project.obj
    1>finalProject.obj : error LNK2019: unresolved external symbol "void __cdecl load_initial_data(void)" (?load_initial_data@@YAXXZ) referenced in function "void __cdecl load_file(void)" (?load_file@@YAXXZ)
    1>C:\Documents and Settings\George\My Documents\VisualStudioProjects\Final Project\Debug\Final Project.exe : fatal error LNK1120: 1 unresolved externals
    1>Build log was saved at "file://c:\Documents and Settings\George\My Documents\VisualStudioProjects\Final Project\Final Project\Debug\BuildLog.htm"
    1>Final Project - 4 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    It would appear as though you have another file being compiled with this project (project.cpp)? Also, you try to use load_initial_data when you only define a load_data.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    6
    Thank you! You have been very helpful! Now I have to see if it does what it is supposed to do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. stdin in Visual Studio 2005 vs. 2003
    By Trev614 in forum C Programming
    Replies: 5
    Last Post: 06-23-2008, 02:44 PM
  3. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  4. using classes
    By krazykrisi in forum C++ Programming
    Replies: 9
    Last Post: 11-22-2006, 10:41 AM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM