Having Trouble with my code.

I am trying to get the User to enter values into the array. 20 numbers, and the array has 2 columns. 1 colum for the value[x] entered and another with the [y]. Y should be the number with added VAT (30%). can anyone help?

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

using namespace std;

int main()
{
   int PriceArray[20] [2] = { {}, {} };
   int x, y;
   int Num, Vat, Sum;
   int counter;


cout << "Please Enter 20 Prices and I will add VAT to it(30%). \n";

for (x = 0; x < 20; x++){
  cin >> x;

  for (y = 0; y < 2; y++){


  cin >> y;


  }
}
cout << PriceArray [x] [y];
return 0;
}
15
16
17
18
  for (x = 0; x < 20; x++)
  {  cin >> PriceArray[x][0];  //  Input price[x]
        PriceArray[x][1] = PriceArray[x][0] * 1.3;  // Calculate VAT     
  }


Line 26 is not going to work. You probably want another loop here.

Note: You probably also want your array to be doubles rather than ints.


Okay thanks,

My array will be: x | y
x | y etc

x is the number entered then y is the same number with the added VAT. how would I store these and be able to output them?

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

using namespace std;

int main()
{
   double PriceArray[10] [2] = { {}, {} };
   double x, y;
   int Num, Vat, Sum;
   int counter;


cout << "Please Enter 10 Prices and I will add VAT to it(20%). \n";

for (x = 0; x < 10; x++)
    {
  cin >> PriceArray [x] [0];
  PriceArray[x][1] = PriceArray[x][0] * 1.3;
  
  for (y = 0; y < 2; y++){


  cin >> PriceArray [0] [y];

  }

}

return 0;
}
Last edited on
You're confusing your loop index variables with the values being entered.
x and y loop indexes and should be ints.
Line 17 enters the price directly into the array.
Line 18 calculates the prince + VAT.

Line 23, you want to output the calculated prices rather than input them.
 
  cout << PriceArray [y] [1] << endl;


Thank you for the information. I get 3 errors on lines I dont even have?


||=== Build: Debug in NumVAT (compiler: GNU GCC Compiler) ===|
C:\Users\Joshyma\Desktop\C++\NumVAT\main.cpp|30|error: 'cout' does not name a type|
C:\Users\Joshyma\Desktop\C++\NumVAT\main.cpp|31|error: expected unqualified-id before 'return'|
C:\Users\Joshyma\Desktop\C++\NumVAT\main.cpp|32|error: expected declaration before '}' token|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|


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

using namespace std;

int main()
{
   double PriceArray[10] [2] = { {}, {} };
   int x, y;



cout << "Please Enter 10 Prices and I will add VAT to it(20%). \n";

for (x = 0; x < 10; x++){

  cin >> PriceArray [x] [0];
  PriceArray[x][1] = PriceArray[x][0] * 1.3;

  for (y = 0; y < 2; y++){


  cout << PriceArray [y] [1] << endl;

  }

}

return 0;

}
closed account (j3Rz8vqX)
No errors on my end.
I'm sure you've forgotten to delete something on your side of the code.

Modified indentations:
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
#include <iostream>

using namespace std;

int main()
{
    double PriceArray[10] [2] = { {}, {} };
    int x, y;



    cout << "Please Enter 10 Prices and I will add VAT to it(20%). \n";

    for (x = 0; x < 10; x++){

        cin >> PriceArray [x] [0];
        PriceArray[x][1] = PriceArray[x][0] * 1.3;

        for (y = 0; y < 2; y++){


            cout << PriceArray [y] [1] << endl;

        }

    }

    return 0;

}
Last edited on
I compiled that program verbaitim and got no errors.

okay I closed codeblox and reopened the program. I started a new project and C&P the code. Now when I run it, I enter a number 1,10,110,ect.

I always get 26 \n 13. This happens whenever I enter any number.

Cin
26
13
Cin
26
13
Cin
26
13

etc.

I want a display like a table.

x | y
x | y
x | y
X | y

20 times

x = user input y = user input * 1.3.

Can anyone point in to information regarding this, or help me understand what to do. Thanks for your help so far.
Last edited on
You don't want nested for loops.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main()
{	const int num_prices = 10;
	double PriceArray[num_prices] [2] = { {}, {} };

    cout << "Please Enter 10 Prices and I will add VAT to it(20%). \n";
    for (int i=0; i<num_prices; i++)
	{	cin >> PriceArray[i][0];
        PriceArray[i][1] = PriceArray[i][0] * 1.3;        
    }
	for (int i=0; i<num_prices; i++)
	{	cout << PriceArray[i][1] << endl;
    }
	system ("pause");
    return 0;
} 


Note: The cout says VAT is 20%, but the calculation is 30%.
closed account (j3Rz8vqX)
Semi pseudo:
For(x stuff; limit 10)
{
----get the value from user and store it into array[x][0]
----modify value at array[x][1] = array[x][0]*1.3;
----For(y stuff; limit 2)
----{
--------print value at array[x][y];(ideally twice) //<<---------Your mistake here.
----}
}

Edit:
Think about it =D
Last edited on
Okay, I missed the bit about displayed the results as a table, but in any case, you don't want the output within the input loop.

YOu could use a nested loop here, or just output the two values:
13
14
15
    for (int i=0; i<num_prices; i++)
    {  cout << PriceArray[i][0] << " | " << PriceArray[i][1] << endl;
    }

Yea, Thanks Dput. The will loop and output.

user input
x|y
user input
x|y

but is there a way to output the whole table at the end or would this require loads of code?
closed account (j3Rz8vqX)
You've got a 2d array, so possibilities are to hardcode print each or to use loop(s).

A loop to control the rows(top/down), and print two values for columns.

Or

A loop to control the rows(10)(top/down), and embedded with another loop to control columns(2)(left/right).

Possibly other methods as well, but those are the the primitive ones.

Pick your poison, either way it isn't too much coding, with the brute force method being the most redundant.
Last edited on
Topic archived. No new replies allowed.