Reputation: 43
I have a homework question. I need help displaying my results in this format:
Price1 Price2 Price3
Price4 Price5 Price6
Price7 Price8 Price9
How can I display the results in the desired format in a cout
statement? Here's my code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
// use a constant Declaration
const int SIZE = 9;
// use a variable Declaration
float prices[SIZE];
cout << fixed << setprecision(2);
// prompt user to enter all 9 values :
for(int i = 0; i < SIZE; i++)
{
cout << "Enter the value: " << i + 1 << "-> ";
cin >> prices[i];
}
cout << "\n----------------------------------------------------\n";
// Display all values in the array
for(int i = 0; i < SIZE; i++)
{
cout << "value Entered " << i + 1 << "\t\t " << right << setw(7) << prices[i] << endl;
}
}
This is the output when running the code:
Enter the value: 1-> 21
Enter the value: 2-> 34
Enter the value: 3-> 54
Enter the value: 4-> 12
Enter the value: 5-> 65
Enter the value: 6-> 34
Enter the value: 7-> 76
Enter the value: 8-> 88
Enter the value: 9-> 90
----------------------------------------------------
value Entered 1 21.00
value Entered 2 34.00
value Entered 3 54.00
value Entered 4 12.00
value Entered 5 65.00
value Entered 6 34.00
value Entered 7 76.00
value Entered 8 88.00
value Entered 9 90.00
--------------------------------
Process exited after 16.86 seconds with return value 0
Press any key to continue . . .
Upvotes: 0
Views: 57
Reputation:
Here try this
#include <iostream>
using namespace std;
int main()
{
// use a constant Declaration
const int SIZE = 9;
// use a variable Declaration
float prices[SIZE];
// prompt user to enter all 10 values :
for (int i = 0; i < SIZE; i++)
{
cout << "Enter the value: " << i + 1 << "-> ";
cin >> prices[i];
}
cout << "\n----------------------------------------------------\n";
// Display all values in the array
int index = 0;
while (index < SIZE)
{
for (int ctr = 0; ctr < 3; ++ctr)
{
cout << "values Entered " << index + 1 << " " << prices[index] << "\t\t";
++index;
}
cout << endl;
}
}
Upvotes: 1