Outputting using the STD OUTPUT HANDLE PROBLEM

How can I display my inputted values here without overwriting the other earlier inputted values (Sorry, I'm running out of english words). It should be displayed like this http://i.imgur.com/ZupIF5i.png but mine shows this
http://i.imgur.com/6V0yurM.png
I've been stucked here for hours in thinking for an algorithm. help me please..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  system("cls");
	cout << "		***** INVENTORY SYSTEM CS127L 4TH QTR*****"<<endl;
	cout << "PROD NO.   PRODUCT       NAME       PRICE    STOCK    SOLD   LEFT\n";
	unsigned int y;
	for (i = 0;  i < sval; ++i){
		for(j = 0, y = 2; j < temp[i].prod_size; ++j, y =+ j){
			gotoxy(hStdout, 11, i+y);
			cout << temp[i].prod_name;

			gotoxy(hStdout, 3, i + y);
			cout << i+1;

			gotoxy(hStdout, 25, j + y);
			cout << temp[i].new_brand[j].brand_name;
	
			gotoxy(hStdout, 36, j + y);
			cout << temp[i].new_brand[j].price;
	
			gotoxy(hStdout, 46, j + y);
			cout << temp[i].new_brand[j].stock;
	
			gotoxy(hStdout, 55, j + y);
			cout << temp[i].new_brand[j].sold;
		
			gotoxy(hStdout, 62, j + y);
			cout << temp[i].new_brand[j].left;
		}
	}

You should check out setw (http://www.cplusplus.com/reference/iomanip/setw/)

For example, your column header could look like this:
1
2
3
	cout << "PROD NO." << setw(10) << "PRODUCT" << setw(10) << "NAME" << setw(15) <<
		"PRICE" << setw(8) << "STOCK" << setw(8) << "SOLD" << setw(8) << "LEFT" << endl;


PROD NO.   PRODUCT      NAME          PRICE   STOCK    SOLD    LEFT
Our professor told us not to use that, she we should output using std output handle

I really cant understand why your professor would rather you use something such as gotoxy over setw.

Anyway try this..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

#include <iostream>
#include <string>
#include <Windows.h>  // for gotoxy function
using namespace std;

struct Stock
{
	int prodNo;
	string prodName;
	string custName;
	double price;
	int stock;
	int sold;
	int left;

	void setStock(int id, string pName, string cName, double cost, int stk, int sld, int lft) 
	{
		prodNo = id;
		prodName = pName;
		custName = cName;
		price = cost;
		stock = stk;
		sold = sld;
		left = lft;
	}


};

void gotoxy(int x, int y);


int main()
{
	Stock Items[3];
	// create some dummy data
	Items[0].setStock(1, "Shampoo", "Michael", 10.99, 6, 2, 4);
	Items[1].setStock(2, "Pepsi", "John", 0.59, 6, 1, 5);
	Items[2].setStock(3, "Batteries", "Debbie", 0.99, 11, 2, 9);

	cout << "		***** INVENTORY SYSTEM CS127L 4TH QTR*****" << endl;
	cout << "PROD NO.   PRODUCT       NAME       PRICE    STOCK    SOLD   LEFT\n";

	for (int i = 0; i < 3; i++)
	{
		gotoxy(2, i+2); cout << "[" << Items[i].prodNo << "]";
		gotoxy(11, i + 2); cout << Items[i].prodName;
		gotoxy(25, i + 2); cout << Items[i].custName;
		gotoxy(36, i + 2); cout << Items[i].price;
		gotoxy(47, i + 2); cout << Items[i].stock;
		gotoxy(55, i + 2); cout << Items[i].sold;
		gotoxy(62, i + 2); cout << Items[i].left;
	}

 	return 0;
}


void gotoxy(int x, int y)
{
	COORD coord;
	coord.X = x;
	coord.Y = y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}



                ***** INVENTORY SYSTEM CS127L 4TH QTR*****
PROD NO.   PRODUCT       NAME       PRICE    STOCK    SOLD   LEFT
  [1]      Shampoo       Michael    10.99      6       2      4
  [2]      Pepsi         John       0.59       6       1      5
  [3]      Batteries     Debbie     0.99       11      2      9
Last edited on

Well from your original post I assumed your problem was formatting the columns and not overwriting the header. The structure in my example is merely there to enter some data and was not intended to replace yours.

To answer your question, this really depends on how you have structured your data. From a real world database point you would have a one-to-many relationship with the different data groups, i.e

One customer could have many orders, one order could have many items and so on.

In your case, I would have structure with all stock items which would contain something like

Stock Id Number (Unique to stock item)
Stock Name
Stock Description
Stock Level
Price



Then I would have data structure for Customers with things like:

Customer ID (unique to customer)
Customer Name
...and anything else about customer


and finally Orders structure

Customer ID (linked to customer who purchased)
Stock ID (id we use to lookup stock item)
Qty (quantity ordered)
Price (copied from stock as prices increase but we would want to know what he paid at that time)
Date
... and so on


So...

Run through stock items 1 by 1, use StockID to search Orders which would then list all those orders for the given StockID, then you can use CustomerID from the Orders data to obtain customer details for each order.

Again it really depends on how you structured your program, and since you didnt post the whole code its just guess work.



EDIT: It seems I replied to a post you deleted - I'll leave it here as it may be helpful anyway.
Last edited on
Sorry about that, I still can't understand how, btw this is my whole code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include<iostream>
#include<string>
#include<cctype>
#include<cstdlib>
#include <iomanip>
#include <windows.h>
#include<conio.h>
int i, j;

using namespace std;

//////// function that takes the direction of x and y coordinates////////
void gotoxy( HANDLE StdOut, SHORT x, SHORT y )
{
	// Set the cursor position.
	COORD Cord;
	Cord.X = x;
	Cord.Y = y;
	SetConsoleCursorPosition( StdOut, Cord );
}
////////////use this structure name in creating the members of inventory//////////////
struct Products
{
	//add code here;
	string prod_name;
	int prod_size;
	string brand_name;
	double price;
	int stock;
	int sold;
	int left;
	Products *new_brand;

};

//////////////////////////////code for getting the password//////////////////////////////////
string EnterPassword()
{
	string NumAsString="";
	char ch=getch();//h
	while (ch!='\r'){//true
		cout<<'*';//*****
		NumAsString+=ch;//"hello"
		ch=getch();//enter/return
	}
	return NumAsString;
}
/////////////////////prototypes of functions////////////////////////////////////////////////
void password();
int ENTERPROD();
void INPUTPROD(int val);
void PDISPLAY(Products *temp, int sval);


////////////////////////////////////////////////////////////////////

void main()   // DO NOT ADD or REVISE ANYTHING FROM THIS FUNCTION
{
	int count;

	password();

	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);


	cout<<"		***** INVENTORY SYSTEM CS127L 4TH QTR*****"<<endl<<endl;

	count=ENTERPROD();

	cout<<endl<<"ENTER "<<count<<" PRODUCTS"<<endl;

	INPUTPROD(count);

	cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
	system("pause");
}//end main


////////////////////////////////////
int ENTERPROD()
{
	//add code here
	int size;
	cout << "ENTER NO. OF PRODUCTS FOR INVENTORY: ";
	cin >> size;

	return size;
}

/////////////////////////////////////
void INPUTPROD(int val)
{
	//add code here
	//call function PDISPLAY HERE…….
	Products *newProd;
	//dynamic allocation of size of val
	newProd = new Products[val];

	//loop for inputting the product name and quantity
	for (i = 0; i < val; ++i){
		cout << "Product[" << i+1 <<"]: ";
		cin >> newProd[i].prod_name;
		cout << "How many " << newProd[i].prod_name << "? ";
		cin >> newProd[i].prod_size;
		cout << endl;

		// dynamic allocation of the prod_size
		newProd[i].new_brand = new Products[newProd[i].prod_size];
		for(j = 0; j < newProd[i].prod_size; ++j){
			cout << newProd[i].prod_name << "[" << j+1 << "]: ";
			cin >> newProd[i].new_brand[j].brand_name;
			cout << "Price: ";
			cin >> newProd[i].new_brand[j].price;
			cout << "Stock: ";
			cin >> newProd[i].new_brand[j].stock;
			cout << "Sold: ";
			cin >> newProd[i].new_brand[j].sold;
			cout << endl;
			newProd[i].new_brand[j].left = newProd[i].new_brand[j].stock - newProd[i].new_brand[j].sold;
		}
	}

		cout << endl;
	//calls PDISPLAY function for outputting the inventory
	PDISPLAY(newProd, val);
}

//////////////////////////////////
void PDISPLAY(Products *temp, int sval)
{
	HANDLE hStdout = GetStdHandle( STD_OUTPUT_HANDLE );
	//add code here
	system("cls");
	cout << "		***** INVENTORY SYSTEM CS127L 4TH QTR*****"<<endl;
	cout << "PROD NO.   PRODUCT       NAME       PRICE    STOCK    SOLD   LEFT\n";
	const int y = 2;
	for (i = 0;  i < sval; ++i){
		for (j = 0; j < temp[i].prod_size; j++){
			gotoxy(hStdout, 3,);
			cout << "[" << i + j + 1 << "]";
			gotoxy(hStdout, 11, );
			cout << temp[i].prod_name;
			gotoxy(hStdout, 25);
			cout << temp[i].new_brand[j].brand_name;
			gotoxy(hStdout, 36);
			cout << temp[i].new_brand[j].price;
			gotoxy(hStdout, 46);
			cout << temp[i].new_brand[j].stock;
			gotoxy(hStdout, 55);
			cout << temp[i].new_brand[j].sold;
			gotoxy(hStdout, 62);
			cout << temp[i].new_brand[j].left;
		}
}

//////////////////////////////////////////////////////////////////////////////////
void password()
{
	//add code here
	//call function EnterPassword HERE…	
	string my_user = "carlo";
	string my_pwd = "hello";
	string enter_user;
	int attempt = 1;
	bool pass = true;
	while(pass){
		system("cls");
		//input username and password
		cout << "USERNAME: ";
		cin >> enter_user;
		system("cls");
		cout << "PASSWORD: ";
		string pwd = EnterPassword();
		//try catch block for username and password
		try{
			for(i = 0; i < pwd.size(); i++){
				if ((pwd.at(i) != my_pwd.at(i)) || (enter_user.at(i) != my_user.at(i)) ){
					attempt++;
					throw pwd;
				}
			}
		}

		catch(string p){
			if (!pass || attempt > 3){
				system("pause");
				cout << "Sorry, but you have exceeded the number of attempts. \n"
					 << "The program will now terminate.\n";
				exit(1);
			}
			cout << endl;
			cout << "Invalid username or password. Try again.\n";
			continue;
		}
		pass = false;
	}
	system("cls");
}

Has that structure been defined by the professor or have you decided on its layout and content? - i.e. are you free to change its layout, or add a second one ?

Also does he allow you to use vectors?
I'm not allowed to change anything from the functions but we can add one. We have not yet covered vectors.
From what I read in your source code you are not allowed to change the main function but are free to modify the others?
No, we're not allowed to change those functions.
this is the original code that she gave
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
***NOTE: ALL VALIDATION should be made using try throw catch method.
Use the following function prototype to complete the process of your source code. Strictly NO MODIFICATION.

#include<iostream>
#include<string>
#include<cctype>
#include<cstdlib>
#include <iomanip>
#include <windows.h>
#include<conio.h>

using namespace std;







//////// function that takes the direction of x and y coordinates////////
void gotoxy( HANDLE StdOut, SHORT x, SHORT y )
{
    // Set the cursor position.
    COORD Cord;
    Cord.X = x;
    Cord.Y = y;
    SetConsoleCursorPosition( StdOut, Cord );
}
////////////use this structure name in creating the members of inventory//////////////
struct Products
{
	//add code here;
};
//////////////////////////////code for getting the password//////////////////////////////////
string EnterPassword()
{
   string NumAsString="";
   char ch=getch();//h
   while (ch!='\r'){//true
	   cout<<'*';//*****
	   NumAsString+=ch;//"hello"
	   ch=getch();//enter/return

   }

   return NumAsString;

}
/////////////////////prototypes of functions////////////////////////////////////////////////
void password();
int ENTERPROD();
void INPUTPROD(int val);
void PDISPLAY(Products temp, int sval);


////////////////////////////////////////////////////////////////////

void main()   // DO NOT ADD or REVISE ANYTHING FROM THIS FUNCTION
{
	int count;
	
	password();

	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);


	cout<<"		***** INVENTORY SYSTEM CS127L 4TH QTR*****"<<endl<<endl;
	


           count=ENTERPROD();

	cout<<endl<<"ENTER "<<count<<" PRODUCTS"<<endl;
   


	INPUTPROD(count);


	cout<<endl;

}//end main


////////////////////////////////////
int ENTERPROD()
{
	 //add code here
}

/////////////////////////////////////
void INPUTPROD(int val)
{
    
     //add code here
	//call function PDISPLAY HERE…….
}

//////////////////////////////////
void PDISPLAY(Products  temp, int sval)
{
HANDLE hStdout = GetStdHandle( STD_OUTPUT_HANDLE );
	//add code here

}

//////////////////////////////////////////////////////////////////////////////////
void password()
{
       //add code here
	//call function EnterPassword HERE…			
  

}
Last edited on

Thats what I meant, the function code but not the function names or parameters passed to them.
yeah
But everything's working in my code except the PDISPLAY part where I am having a hard time.

Bubble sort the list of objects, then run through the list checking if the current one is the same as the last and just change its output slightly. Taking my last example I modified the code to show you what I mean.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

#include <iostream>
#include <string>
#include <Windows.h>  // for gotoxy function
using namespace std;

struct Stock
{
	int prodNo;
	string prodName;
	string custName;
	double price;
	int stock;
	int sold;
	int left;

	void setStock(int id, string pName, string cName, double cost, int stk, int sld, int lft)
	{
		prodNo = id;
		prodName = pName;
		custName = cName;
		price = cost;
		stock = stk;
		sold = sld;
		left = lft;
	}

};

void gotoxy(int x, int y);


int main()
{
	const int NUM_ITEMS = 5;

	Stock Items[NUM_ITEMS];
	Stock temp;  // for bubble sort

	// create some dummy data
	Items[0].setStock(1, "Shampoo", "Michael", 10.99, 6, 2, 4);
	Items[1].setStock(2, "Pepsi", "John", 0.59, 6, 1, 5);
	Items[2].setStock(3, "Batteries", "Debbie", 0.99, 11, 2, 9);
	Items[3].setStock(1, "Shampoo", "Debbie", 10.99, 4, 2, 2);
	Items[4].setStock(1, "Shampoo", "Paul", 10.99, 2, 1, 1);

	cout << "		***** INVENTORY SYSTEM CS127L 4TH QTR*****" << endl;
	cout << "PROD NO.   PRODUCT       NAME       PRICE    STOCK    SOLD   LEFT\n";

	// before displaying, bubble sort the list so that
	// its listed in ascending order.
	for (int passes = 0; passes < NUM_ITEMS; passes++)
	{
		for (int j = 0; j < NUM_ITEMS - passes - 1; j++)
		{
			if (Items[j].prodNo > Items[j + 1].prodNo)
			{
				temp = Items[j];
				Items[j] = Items[j + 1];
				Items[j + 1] = temp;
			}
		}
	}

	// now it will look like

//	              ***** INVENTORY SYSTEM CS127L 4TH QTR*****
//		PROD NO.PRODUCT       NAME       PRICE    STOCK    SOLD   LEFT
//		[1]      Shampoo       Michael    10.99      6       2      4
//		[1]      Shampoo       Debbie     10.99      4       2      2
//		[1]      Shampoo       Paul       10.99      2       1      1
//		[2]      Pepsi         John       0.59       6       1      5
//		[3]      Batteries     Debbie     0.99       11      2      9

	int last = 0;

	for (int orders = 0; orders < NUM_ITEMS; orders++)
	{
		// display whats common to both
		gotoxy(25, orders + 2); cout << Items[orders].custName;
		gotoxy(36, orders + 2); cout << Items[orders].price;
		gotoxy(47, orders + 2); cout << Items[orders].stock;
		gotoxy(55, orders + 2); cout << Items[orders].sold;
		gotoxy(62, orders + 2); cout << Items[orders].left;		
		// is the current one the same as the last?
		if (Items[orders].prodNo != last) {
			// no so show the item number and product name
			gotoxy(2, orders + 2); cout << "[" << Items[orders].prodNo << "]";
			gotoxy(11, orders + 2); cout << Items[orders].prodName;
		}

		// store the current in our last for next check.
		last = Items[orders].prodNo;
	}

	return 0;
}


void gotoxy(int x, int y)
{
	COORD coord;
	coord.X = x;
	coord.Y = y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}


                ***** INVENTORY SYSTEM CS127L 4TH QTR*****
PROD NO.   PRODUCT       NAME       PRICE    STOCK    SOLD   LEFT
  [1]      Shampoo       Michael    10.99      6       2      4
                         Debbie     10.99      4       2      2
                         Paul       10.99      2       1      1
  [2]      Pepsi         John       0.59       6       1      5
  [3]      Batteries     Debbie     0.99       11      2      9
Topic archived. No new replies allowed.