Code needs to be edited cant figure out what to do

Hi all

So i have written a code that produces a statement for a customers bill. It gives discounts and such to different customers. However when my code asks for the customers name and i type in the name say eg Mr. Wilson it automatically shows the next line which is the customer number and then the width of the room. It does not let me input the customer number. Similarly when i type in Mr and Mrs Adams the code automatically produces all the statements and the end statement without allowing me to input any variables. I know it has something to do with the nme but i cant figure out what. 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <string>
#include <math.h>

using namespace std;

int calculateCarpetSize(float length, float width)
{
  float carpetSize = ceil(length) * ceil(width);
  return (int)carpetSize;
}

float calculateCarpetPrice(int carpetSize, float sellingPrice)
{
  return carpetSize * sellingPrice;
}

float calculateLabourCost(int carpetSize)
{
  return carpetSize * 24.00;
}

bool qualifyForDiscount(string customerNumber)
{
  if (customerNumber.find("0") == 0)
     return true;
  else
    return false;
}

float computeDiscount(float carpetSize)
{
  float discountPercentage;
  cout << "Enter the percentage discount: " << endl;
  cin >> discountPercentage;
  return carpetSize * discountPercentage;
}

void printCustomerStatement(string customerName, string customerNumber, float carpetCost, float labourCost, float discountAmount)
{
  float subtotal = carpetCost + labourCost;
  float subtotalAfterDiscount = subtotal - discountAmount;
  float taxAmount = subtotalAfterDiscount * 0.14;
  float totalIncludingTax = subtotalAfterDiscount + taxAmount;

  cout << "================================================================" << endl;
  cout << "CROSSWELL CARPET STORE" << endl;
  cout << "STATEMENT" << endl;
  cout << "Customer Name: " << customerName << endl;
  cout << "Customer Number: " << customerNumber << endl;
  cout << endl;

  cout << "Carpet cost: " << carpetCost << endl;
  cout << "Labour: " << labourCost << endl;
  cout << endl;

  cout << "Subtotal: " << subtotal << endl;
  cout << "Less discount: " << discountAmount << endl;
  cout << endl;

  cout << "Subtotal: " << subtotalAfterDiscount << endl;
  cout << "Plus tax: " << taxAmount << endl;
  cout << "TOTAL: " << totalIncludingTax << endl;
  cout << "================================================================" << endl;
}


 int main()
 {
  string customerName;
  string customerNumber;
  float roomLength;
  float roomWidth;
  float sellingPrice;
  float carpetPrice;
  float labourCost;
  float discountAmount;

  int carpetSize; //this is the calculated size

  cout << "Please enter the information" << endl;

  //Get customer name
  cout << "Enter the customer's name: " << endl;
  cin >> customerName;
  cout << endl;
  //Get customer number
  cout << "Enter Customer number: " << endl;
  cin >> customerNumber;

  //Get room length and width
  cout << "Enter the length of the room: " << endl;
  cin >> roomLength;
  cout << "Enter the width of the room: " << endl;
  cin >> roomWidth;

  //Get carpet cost per sq.m
  cout << "Enter the carpet cost (per square metre): " << endl;
  cin >> sellingPrice;


  //Do all the caculations
  carpetSize = calculateCarpetSize(roomLength, roomWidth);
  carpetPrice = calculateCarpetPrice(carpetSize, sellingPrice);
  labourCost = calculateLabourCost(carpetSize);
  if (qualifyForDiscount(customerNumber) == true)
    discountAmount = computeDiscount(carpetSize);
  else
    discountAmount = 0;


  //Print the statement!
  cout << endl << endl;
  printCustomerStatement(customerName, customerNumber, carpetPrice, labourCost, discountAmount);


  return 0;
}
Last edited on
If your string input has spaces embedded within it you'll need to use getline() instead of the extraction operator>>.


It does not let me input the customer number. Similarly when i type in Mr and Mrs Adams the code automatically produces all the statements and the end statement without allowing me to input any variables.


I just tried your code, it does allow variable input. It works fine.
One thing you might want to change though, is to add :

1
2
3
4
5
6
7
8
9
//Print the statement!
	cout << endl << endl;
	printCustomerStatement(customerName, customerNumber, carpetPrice, labourCost, discountAmount);

	int keepWindowOpen;
	cin >> keepWindowOpen;

	return 0;
}


to the end, so it waits for user input at the end after all statements have been printed. This gives you a chance to see the output details/bill.
Topic archived. No new replies allowed.