While loop or If error

I feel like I am running headfirst into a wall on this one and I am sure it is something easy.

There is something wrong with my while loop. After inputting a few lumber "orders" to process, I try to use "T" (for total) it exits the loop and then goes to a blank prompt. After that if I press T again it goes into a continuous loop. What do I need to do? It might actually be a problem with my getType function...I know there is a problem I just don't know enough to fix it...grrr...
I'm sure this is very basic, but I am new to C++ and can't seem to figure it out.

Any help would be greatly appreciated!



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
//This program calculates the cost of lumber												


#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

double boardFeet (double, double, double);
string getType (char);
double getPrice (char, double);

double price;

int main (){

	char type = ' ';
	double width = 0.0, height= 0.0, length = 0.0;
	int quantity = 0;
	double boardFt = 0.0;
	string typeWood = " ";
	double subTotal = 0.0;
	double total = 0.0;

	cout << "Lumber Calculator" << endl << endl << "Available type codes: ""P"" for pine , ""F"" for fir, ""C"" for cedar, ""M"" for maple, \n and ""O"" for oak. Press ""T"" for total." << endl
		<< "Use type quantity width height length format. (i.e. P 10 2 4 8)" << endl << endl;
cout << "Enter first item: ";
cin >> type >> quantity >> width >> height >> length;
	if (type != 'T')
		while (type != 'T') {
			boardFt = boardFeet (width, height, length);
			typeWood = getType (type);
			subTotal = getPrice (type, boardFt);
			total += subTotal;
			cout << quantity << " " << width << "x" << height << "x" << length << " " << typeWood << setprecision(2) << ", Costs: $" << subTotal << endl;
			cout << "Enter item: ";
			cin >> type >> quantity >> width >> height >> length;}
	else
		cout << "Total cost: $" << total << endl;
	
	system("pause");
	return 0;
}

double boardFeet (double width, double height, double length){
	
	double boardFts = (( width * height * length )/ 12.0);
	return boardFts;
}

double getPrice (char type, double boardFt)
{
	double price;
	
	switch (toupper(type))
	{
	case 'P':
		return price = boardFt * 0.89;
		break;
	case 'F':
		return price = boardFt * 1.09;
		break;
	case 'C':
		return price = boardFt * 2.26;
		break;
	case 'M':
		return price = boardFt * 4.50;
		break;
	case 'O':
		return price = boardFt * 3.10;
		break;
	}
	return 0;
}

string getType (char type) 
	{
	string woodType;
		
	switch (toupper(type))
	{
		case 'P':
			return woodType = "Pine";
			break;
		case 'F':
			return woodType = "Fir";
			break;
		case 'C':
			return woodType = "Cedar";
			break;
		case 'M':
			return woodType = "Maple";
			break;
		case 'O':
			return woodType = "Oak";
			break;
		default: 'T';
	}
	return "T";
}
The problem is, your input expects 5 arguments, and won't return until those are all inputted. Look at the cin.peek() function and see if you can modify your program to work.
As far as I know, if I were to use cin.peek(), it would only take the next character. This would be fine but I need all of the characters between the white spaces. That way my functions get the entire number that the user input instead of just the first digit. Though I certainly see that you are on to something with the 5 arguments.
if I were to use cin.peek(), it would only take the next character.


Not quite -- peek() doesn't "take" any characters; it only tells you what the next one in the buffer is.

The idea would be, if peek returned a "T", don't bother reading; just print out the total and be done. If it's not "T", read the buffer and process it in your loop.

I think a little cleaning up of your logic and the peek() function can make your program work just fine.
Topic archived. No new replies allowed.