Simple Shopping Program

Hello everyone, I began working with C++ few months ago and I'm trying to create a simple shopping program that would ask the user to enter their information (such as name date and address) and then it would take the user through a simple shopping experience where the user enters the qty of the listed items and it generates an invoice in the end with the total sum. Unfortunately I am having a hard time debugging it...I would really appreciate your help.
Here is what i have so far:


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
#include<iostream>
using namespace std;

double getUnitprice(int itemCode);

int main(){

  double invoice[10][4];
  int i=0; char more; char date[100]; char address[100]; int name;
  

  cout<< "\n\n********* WELCOME TO THE WIDGET INC!********** \n\nItem code\tUnitPrice\n\n1:Magic Widget\t150\n2:Wigitizer\t75\n3:Tango Widget\t200\n4:Widget9000\t325\n5:Widget II\t100\nInvalidCode\t0\n\n";
  cout<< "Please enter the date: "<<endl;
  cin>> date;
  cout<<"Please enter your address: "<<endl;
  cin>> address;

  do {
 
    cout << "\n\nItem code: ";
    cin >> invoice[i][0];
    cout << "Quantity : ";
    cin >> invoice[i][1];
    
    invoice[i][2] = getUnitprice(invoice[i][0]); 
    invoice[i][3] = invoice[i][1] * invoice[i][2]; 
    
    cout << "Do you have any other items to be entered next (y/n) ? ";
    cin >> more;
  
    i++;
  } while(more == 'y');
    
 
  cout << "\n\n-----------------------------\n\n";
  cout << "Date : " << date << "\n";
  cout << "Address : " <<address<< "\n\n";
  
  cout << "ItemCode|Quantity|UnitPrice|TotalPrice\n\n";
  int tot=0;
  for(int k=0; k<i; k++)   
    
	{
		for(int l=0; l<6; l++) 
		{
		    cout << invoice[k][l]  << "\t";		   
		}
		cout << endl;
		tot = tot + invoice[k][6];
	}

  cout << "\n\n Your Total Amount : " << tot;
  cout << "\n\n-----------------------------\n\n\n";
  
  return 0;
}

double getUnitprice(int itemCode){
  double price;
  switch (itemCode)
  {
    case 1: price = 150;
      break; 
    case 2: price = 75;
      break;
    case 3: price = 200;
      break;
	case 4: price= 325;
	  break;
	case 5: price= 100;
	  break;
    default: price = 0;
      break;
  }
  return price;
}

closed account (4NqL8vqX)
1. while(more == 'y');

You should also check the letter for uppercase.

while(more == 'y' || more == 'Y');

2.
If you enter 'more' more than 10 times, your program will perform an out of bounds error. The array only has 10 elements.

double invoice[10][4];
Hello Zenyat,

Who ever was attached to "closed account" made two good points, but I did not even get that far when I tested the program.

On line 14 you use "cin" to get the date and that should work, but some day it might become a problem. And on line 16 you also use "cin" to get an address and that is a problem because an address will most likely contain spaces and "cin" will extract to the first white space or a "\n" which ever comes first leaving the remainder in the buffer, so the next call to "cin" will extract from what is left in the buffer not the keyboard thus putting incorrect information into the next variables. Thus the inputs at line 21 and 23 will come from the input buffer not the keyboard essentially putting incorrect information into your array. And this could also bypass line 29 and end the while loop finishing the program printing out bad information.

On line 14 I would suggest using std::getline(std::cin, date); and the same on line 16. You should also change char date[100]; to std::string date{ "" }; and the same with address. You will find that std::string will work the same as the old style char array yet offers more flexibility than the char array.

Also on line 9 I would suggest that you initialize all your variables before you us them.

Some where around line 31 you need to check the value of "i" so as not to out of bounds with the array.

In line 32 you could write the "while" as while(toupper(more) == 'Y'); or you could use tolower(). Either way will work.

I will need to make some changes and do so more testing before I can go any farther.

Hope that helps,

Andy
Hello Zenyat,

After further testing I found these problems:

line 25 I changed getUnitprice(invoice[i][0]) to getUnitprice(static_cast<int> (invoice[i][0])) because you were trying to send a double to a function that requires an int. This gave me a compiler warning, but no error.

After line 31 I added if (i > 9) more = 'N'; to keep the loop from going out of bounds with the array.

Line 40 changed "tot" form an int to a double.

Line 44 changed l < 6 to l < 4 because the second dimension is only 4 i.e., 0 to 3 6 is out of bounds.

The same applies to line 49.

Hope that helps,

Andy
Topic archived. No new replies allowed.