Hey folks. I'm having a hard time grasping how functions work. I am supposed to pass the parallel arrays through a function. I can get the program to run just as arrays, not with functions though. Right now I'm getting an error while trying to debug my program. Thanks for any advice and help!!
/*****************************
Program Description: This program allows the user to enter up to 20 book names and prices.
It can sort for the most expensive, least expensive, and average price.
*********************************/
//Main function
int main()
{
//declare arrays to store book names and prices.
const int SIZE = 20;
string name[SIZE];
double price[SIZE];
string book_name;
double book_price = 0;
int numValues;
//call function to prompt user to enter book names and prices
get_data(name, SIZE);
//disply books and prices entered by the user
cout << endl << endl;
cout << left << setw(20) << "Book Title" << right << setw(10) << "Price" << endl;
cout << "==============================" << endl;
for (int index = 0; index < numValues; index++)
{
cout << left << setw(20) << name[index];
cout << fixed << right << setw(10) << setprecision(2) << price[index] << endl;
void get_data (string book_name, int size )
for (numValues = 0; numValues < size; numValues++)
{
cout << "Next book name? Enter #### to end data entry :";
cin >> book_name;
if (book_name == "####")
{
break;
}
else
{
name[numValues] = book_name;
cout << "Enter the price of this book :";
cin >> book_price;
price[numValues] = book_price;
}
}
#include <iostream>
#include <iomanip>
#include <cctype>
#include <string>
usingnamespace std;
//Function prototypes
void get_data(string, int);
//Main function
int main()
{
//declare arrays to store book names and prices.
constint SIZE = 20;
string name[SIZE];
double price[SIZE];
string book_name;
double book_price = 0;
int numValues;
//call function to prompt user to enter book names and prices
get_data(name, SIZE);
//disply books and prices entered by the user
cout << endl << endl;
cout << left << setw(20) << "Book Title" << right << setw(10) << "Price" << endl;
cout << "==============================" << endl;
for (int index = 0; index < numValues; index++)
{
cout << left << setw(20) << name[index];
cout << fixed << right << setw(10) << setprecision(2) << price[index] << endl;
}
return 0;
}
//******************************************************
//Functions
//******************************************************
//prompt user to enter name or exit program
void get_data (string book_name, int size )
for (numValues = 0; numValues < size; numValues++)
{
cout << "Next book name? Enter #### to end data entry :";
cin >> book_name;
if (book_name == "####")
{
break;
}
else
{
name[numValues] = book_name;
cout << "Enter the price of this book :";
cin >> book_price;
price[numValues] = book_price;
}
}