passing arrays through a function

Oct 15, 2010 at 4:05am
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.
*********************************/


#include <iostream>
#include <iomanip>
#include <cctype>
#include <string>
using namespace std;

//Function prototypes

void get_data(string, int);


//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;

}

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;
}
}
Oct 15, 2010 at 4:09am
first ,plase post the code
using source code:
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

#include <iostream>
#include <iomanip>
#include <cctype>
#include <string>
using namespace std;

//Function prototypes

void get_data(string, int);


//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; 

}

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;
}
}
Oct 15, 2010 at 4:12am
now whats the job of getdata function?
question have studied structer yet?
Last edited on Oct 15, 2010 at 4:21am
Topic archived. No new replies allowed.