HELP

I am struggling to find the issue.

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
  #include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;

int main(int argc, char argv) {

    int n = 0;
    float sum = 0.00; 
    float tax = 0.00; 
    float salesTax = 0.00; 
    char ch = 'y';


    while (ch == 'Y' || ch == 'y') {
        sum = 0;
        cout << "\nHow many sales items do you have? :";
        cin >> n; 
        
		float items [n]; 

        for (int i = 0; i < n; i++) {
            cout << "Enter in the value of sales item  " << (i + 1) << " : $ ";
            cin >> items[i]; 
            sum = sum + items[i]; 
        }

        cout << "\n\nEnter in the sales tax percentage\n";
        cout << "<Enter 10 for 10% >: ";
        cin >> tax; 

        salesTax = sum * tax / 100;

        cout << fixed; 
        cout << setprecision(2); 

        cout << "\n\n\n*******************************************************" << "\n";
        cout << "********** S A L E S  R E C E I P T *******************" << "\n";
        cout << "*******************************************************" << "\n";
        cout << "**                                                   **" << "\n";
        cout << "**                                                   **" << "\n";
        
        for (int i = 0; i < n; i++) {
        cout << "**" << right << setw(20) << "Sales item  "<< setw(2) << right <<(i+1)<<"       $" << setw(12) << right << items[i] << "         **" << "\n";
        }
        
        cout << "**                                                   **" << "\n";
        cout << "**                                                   **" << "\n";
        cout << "*******************************************************" << "\n";
        cout << "**                                                   **" << "\n";
        cout << "**                                                   **" << "\n";
        cout << "**" << right << setw(30) << "Total Sales         $" << setw(12) << right << sum << "         **" << "\n";
        cout << "**" << right << setw(30) << "  Sales tax         $" << setw(12) << right << salesTax << "         **" << "\n";
        cout << "**" << right << setw(30) << "                              ------------" << "         **" << "\n";
        cout << "**" << right << setw(30) << "Grand Total         $" << setw(12) << right << sum + salesTax << "         **" << "\n";
        cout << "**                                                   **" << "\n";
        cout << "**                                                   **" << "\n";
        cout << "*******************************************************" << "\n";

        cout << "\n\n\nDo you want to run this program again? <Y/N>: ";
        cin >> ch;

        while (ch != 'Y' && ch != 'y' && ch != 'N' && ch != 'n') {
            cout << "please enter a valid input\n\n"; 
            cout << "Do you want to run this program again? <Y/N>: ";
            cin >> ch;
        }
    }

    return 0;
}


I get the following errors.

C:\Documents and Settings\ajc5212\My Documents\hw5.cpp(22) : error C2057: expected constant expression
C:\Documents and Settings\ajc5212\My Documents\hw5.cpp(22) : error C2466: cannot allocate an array of constant size 0
C:\Documents and Settings\ajc5212\My Documents\hw5.cpp(22) : error C2133: 'items' : unknown size
C:\Documents and Settings\ajc5212\My Documents\hw5.cpp(45) : error C2374: 'i' : redefinition; multiple initialization
C:\Documents and Settings\ajc5212\My Documents\hw5.cpp(24) : see declaration of 'i'
Last edited on
Which IDE you're using? Visual C++?
Why my DevC++ with minGW runs good.

Have you tried float *items = new float[n];
Last edited on
I'm using Microsoft Visual C++
Try float float *items = new float[n]; instead of float items[n];
Last edited on
That fixed most of the issues, but I am stilling having errors with lines 45 and 24 with (i)
declare int i; before loop, and in loop just use i=0; as initial.
To define a static array like that, the size needs to be constant. Ie. const int n = #; However, since you want to set the size dynamically, you need to allocate the space at runtime the way lsk mentioned. float *items = new float[n];

For the int i problem, I have seen different behavior with different compilers. Personally I hate it when a compiler extends the scope of variables defined in a for loop outside of the for loop! You can either change your second for loop to use a different variable name, or instead of redefining i, you can just reinitialize it.

for(i=0; i<n; i++) { ... }
Last edited on
Sorry, but where in the loop should i use i=0; ???

I'm the most novice of beginners ....
oh i see... ill try it thanks
That did it! Thank you for your help
Don't forget to change your thread be SOLVED(Do not ask me, I don't know how to do it *w*).
Topic archived. No new replies allowed.