Linked Lists Adding Record. VS2005

This is a discussion on Linked Lists Adding Record. VS2005 within the C++ Programming forums, part of the General Programming Boards category; I cant seem to get the get_data() function to do what it is supposed to! Nor can I get the ...

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

    Question Linked Lists Adding Record. VS2005

    I cant seem to get the get_data() function to do what it is supposed to! Nor can I get the save_file() function to work. It works in the old visual studio, but not 2005. Can anyone please help me?? I don't know what I am doing wrong!
    Last edited by snap_Dragon555; 12-04-2006 at 11:37 PM.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Have you tried to remove warnings?

    int main

    current->a=D.count;
    current->b=D.wholesale;

    a is int and count is float

    and so on (2 error(s), 21 warning(s))
    I think you should start with fixing it, and only when you get something that compiles without errors - start working on the logic
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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

    Exclamation

    Oh sorry I posted the wrong code!! Here is the code that compiles



    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<<" "
    		<< tail->a <<" "
    		<< 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<<" "
    			<<current->a<<" "
    			<<current->b<<endl;
    		current = current->next;
    	}
    	_getch();
    }
    
    
    
    
    
    
    
    
    
    void load_initial_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;
    	}
    }
    
    
    
    
    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;
    	}
    }

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    still void main
    still int a-float count

    see no difference
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    6
    I do see a difference but even with those both fixed I still have the same problem. I can not get it to add a record to list. I'm sorry about the trouble I am new to this board and new to c++ all together... so please bare with me.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    In the case 2
    After you read the 1st item you add it to the list using load_initial_data function
    After reading all other items you do nothing - so the items are effectively lost

    1. Don't use globals
    2. If you working with C++ - work with C++
    - Create Class
    - use string instead of c-strings
    - use standard containers
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  2. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  3. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21