Thread: Assignment Problem!!! Help, PLZ

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    3

    Assignment Problem!!! Help, PLZ

    Code:
    1231

  2. #2
    Registered User
    Join Date
    Jul 2014
    Posts
    3
    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <cstdlib>
    #include <ctime>
    #include <string>
    using namespace std;
    
    
    int first_Option;
    int second_Option;
    int third_Option;
    string product_type;
    string product_ID;
    string corresponding_product_ID;
    int num_products;
    string quit_Ans;
    const int y_ID=33;
    const int p_Name=6;
    const int p_Types=2;
    string(*arr)[p_Name][p_Types]=new string[y_ID][p_Name][p_Types];
    string word;
    double p_Price[y_ID];
    double cost;
    
    
    int y=0, x1=0, x2=0, x3=0;
    char char_arr1[20];
    char char_arr2[20];
    char char_arr3[20];
    int count1=0;
    int count2=0;
    int count3=0;
    class T
    {
    public:
    	T()
    	{
    		total = 0;
    	}
    
    
    	void Ascending_order_of_Price()
    	{
    
    
    		ifstream myReadFile;
    		myReadFile.open("ProductList.txt");
    		while (myReadFile>>word)
    		{
    			if (word=="Book"||word=="Furniture"||word=="Shoe"||word=="Furniture"||word=="Stationary"||word=="Food")
    			{
    				x1=0;//arr[y][0][0] refer productID,arr[y][x1][0] refer productName, arr[y][0][x2] refer to productTypes.
    				x2=1;
    				arr[y][x1][x2]=word;
    				myReadFile>>cost;
    				p_Price[x3]=cost;
    				++y;
    				++x3;
    				x2=0;
    			}
    			else
    			{
    				arr[y][x1][x2]=word;
    				x1++;
    			}
    		}
    		cout << endl;
    		cout << left << setw(14) << "Product ID" << setw(30) << "Name" << setw(15) << "Type" << setw(11) << "Price (HK$)" << endl;
    		cout << "----------------------------------------------------------------------" << endl;
    		cout << fixed << setprecision(2);
    		for (int next = 1; next<y_ID; next++)
    		{
    			double insert = p_Price[next];//use Insertion sort and set double insert to store the smallest price.
    			string insert1 = arr[next][0][0];//if the next has change, the original variable s1(below there) will change too.
    			string insert2 = arr[next][1][0];
    			string insert3 = arr[next][2][0];
    			string insert4 = arr[next][3][0];
    			string insert5 = arr[next][4][0];
    			string insert6 = arr[next][0][1];
    			int moveItem = next;
    			while ((moveItem>0)&&(p_Price[moveItem-1]>insert))//example like P[0]>P[1], P[2]>P[3].......
    			{
    				p_Price[moveItem]=p_Price[moveItem-1];//both values change together
    				arr[moveItem][0][0]=arr[moveItem-1][0][0];//if the next has change, the original variable s1(below there) will change too.
    				arr[moveItem][1][0]=arr[moveItem-1][1][0];
    				arr[moveItem][2][0]=arr[moveItem-1][2][0];
    				arr[moveItem][3][0]=arr[moveItem-1][3][0];
    				arr[moveItem][4][0]=arr[moveItem-1][4][0];
    				arr[moveItem][0][1]=arr[moveItem-1][0][1];
    				moveItem--;
    			}
    			p_Price[moveItem] = insert;
    			arr[moveItem][0][0]=insert1;//arr[moveItem][0][0]=the original number of element.
    			arr[moveItem][1][0]=insert2;
    			arr[moveItem][2][0]=insert3;
    			arr[moveItem][3][0]=insert4;
    			arr[moveItem][4][0]=insert5;
    			arr[moveItem][0][1]=insert6;
    		}
    		for (int s1=0; s1 < y_ID; s1++)
    		{
    			cout << setw(14) << arr[s1][0][0];
    
    
    			if (arr[s1][2][0]=="\0")//1		//Since [s1][1][0] = "apple", then [s1][2][0] = "\0", thus the number of element is 1
    			{
    				cout << setw(29) << arr[s1][1][0]; 
    			}
    			else if (arr[s1][3][0]=="\0")//2
    			{
    				strcpy(char_arr1, arr[s1][1][0].c_str());//change string arr[s1][1][0] to char char_arr1[20]
    				for (int s3=0; char_arr1[s3]!='\0';s3++)
    				{
    					char_arr1[s3];
    					count1++;
    				}
    				cout << arr[s1][1][0] << " " << setw(30-count1-2) << arr[s1][2][0];
    			}
    			else if (arr[s1][4][0]=="\0")//3
    			{
    				strcpy(char_arr1, arr[s1][1][0].c_str());
    				strcpy(char_arr2, arr[s1][2][0].c_str());
    				for (int s4=0; char_arr1[s4]!='\0';s4++)
    				{
    					char_arr1[s4];
    					count1++;
    				}
    				for (int s5=0; char_arr2[s5]!='\0';s5++)
    				{
    					char_arr2[s5];
    					count2++;
    				}
    				cout << arr[s1][1][0] << " " << arr[s1][2][0] << " " << setw(30-count1-count2-3) << arr[s1][3][0];
    			}
    			else if (arr[s1][5][0]=="\0")//4
    			{
    				strcpy(char_arr1, arr[s1][1][0].c_str());
    				strcpy(char_arr2, arr[s1][2][0].c_str());
    				strcpy(char_arr3, arr[s1][3][0].c_str());
    
    
    				for (int s6=0; char_arr1[s6]!='\0';s6++)
    				{
    					char_arr1[s6];
    					count1++;
    				}
    				for (int s7=0; char_arr2[s7]!='\0';s7++)
    				{
    					char_arr2[s7];
    					count2++;
    				}
    				for (int s8=0; char_arr3[s8]!='\0';s8++)
    				{
    					char_arr3[s8];
    					count3++;
    				}
    				cout << arr[s1][1][0] << " " << arr[s1][2][0] << " " << arr[s1][3][0] << " " << setw(30-count1-count2-count3-4) << arr[s1][4][0];
    			}
    			cout << " " << setw(15) << arr[s1][0][1] << setw(11) << p_Price[s1] << endl;
    			count1=0;
    			count2=0;
    			count3=0;
    		}
    		cout << endl;
    		myReadFile.close();
    	}
    void Ascending_order_of_Product_ID()
    	{
    
    
    	}
    	void Ascending_order_of_Product_Name()
    	{
    
    
    	}
    	void eShop_Main_Menu()
    	{
    		cout << "**** eShop Main Menu ****" << endl;
    		cout << "(1) List products" << endl;
    		cout << "(2) Filter products by type" << endl;
    		cout << "(3) Add product to shopping cart" << endl;
    		cout << "(4) Go to shopping cart" << endl;
    		cout << "(5) Quit " << endl;
    		cout << "*************************" << endl;
    		cout << "Option (1 - 5): ";
    		cin >> first_Option;
    		cout << endl;
    		if (first_Option == 1)
    		{
    			cout << "**** List Products Menu ****" << endl;
    			cout << "(1) Ascending order of Product ID" << endl;
    			cout << "(2) Ascending order of Product Name" << endl;
    			cout << "(3) Ascending order of Price" << endl;
    			cout << "****************************" << endl;
    			cout << "Enter your option (1 - 3): ";
    			cin >> second_Option;
    			cout << endl;
    			switch (second_Option)
    			{
    			case 1:
    				Ascending_order_of_Product_ID();
    				break;
    			case 2:
    				Ascending_order_of_Product_Name();
    				break;
    			case 3:
    				Ascending_order_of_Price();
    				break;
    			}
    			eShop_Main_Menu();
    		}
    		
    	}
    
    int main ()
    
    
    {	
    ifstream myReadFile;
    	myReadFile.open("ProductList.txt");
    
    T eShop;
    	eShop. eShop_Main_Menu();	
    myReadFile.close();
    	delete[]arr;
    	system ("pause");
    	return 0;
    }
    i want to load a text file (must one file) and get data from file with setw(), thus i use Ascending_order_of_Price();,Ascending_order_of_Pro duct_ID(); and Ascending_order_of_Product_Name(); to make an order in different type of item. Supposed that these three class functions are same. it is a problem that i can not execute the program or these class functions because repeated loading same file. However, i want to use arr[][][] to do the ascending order and make more actions by arr[][][].
    about the class function eShop_Main_Menu();, I want to call it more than one, but it contains these class functions. Please Help me! give me some advises or some code for reference.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    So what is in ProductList.txt ?

    You have a whole mess of global variables, and a single class (with NO member variables), and code to do "everything". This is plainly wrong.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You've got some cleaning up to do.
    Use local variables instead of global ones. Declare them where they are first needed.
    Don't use new/delete. Use std::vector.
    Stop using strcpy; use the assignment operator instead (provided you are using std::vector and std::string).
    Stop using such big arrays. I don't believe they're needed. Split the data into more variables.
    Name variables and classes appropriately. What they're used for is impossible to tell.
    Use std::sort instead of your hand-rolled sort.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment problem
    By SterlingM in forum C++ Programming
    Replies: 5
    Last Post: 12-05-2009, 09:43 AM
  2. Assignment problem
    By cboard_member in forum C++ Programming
    Replies: 11
    Last Post: 02-18-2006, 02:16 PM
  3. Another assignment problem
    By the_winky_files in forum C Programming
    Replies: 14
    Last Post: 10-03-2005, 10:33 AM
  4. An assignment problem
    By EeeK in forum C Programming
    Replies: 1
    Last Post: 10-03-2003, 07:51 PM
  5. problem of assignment.....
    By skwei81 in forum C++ Programming
    Replies: 1
    Last Post: 03-27-2003, 12:28 PM