passing array as argument to function?
virk (5)
Feb 10, 2013 at 5:38am UTC
how can i pass an array as an argument to the function?
in getCoin() fcn, I am supposed to pass coins array as an argument to the function. fcn prompts user to enter coin(Date, Type and Country). values entered
by user are read and assigned to the coins array.
Help is appreciated.
I tried the code below.
//# include "Coins.h";
#include <iostream>
#include <string>
using namespace std;
struct Coins
{
int date;
string type;
string country;
};
void getCoins(Coins & coins[], int SIZE);
int main()
{
const int SIZE = 10;
Coins coins;
//char choice;
cout << "Enter upto 10 coins.\n"
<< "Enter a coin (Y/N)?\n";
cin >> choice;
//while(choice == 'y'||'Y')
getCoins(coins, SIZE);
return 0;
}
//--------------------------------------------
void getCoins(Coins & coins[], int SIZE)
{
cout << "Date: ";
cin >> coins.date;
cin.ignore();
cout << endl << "Type: ";
getline(cin, coins.type);
cout << endl << "Country: ";
getline(cin, coins.country);
}
Smac89 (195)
Feb 10, 2013 at 6:37am UTC
What you want to do in main is create an array of type coins:
Coins coins[SIZE];
Then your void getCoins should be (I think):
void getCoins(Coins coins[], int SIZE)
Since you created an array of coins, to access each one, you do:
1 2 3
//coins[i].type...
//coins[i].date...
//etc
virk (5)
Feb 10, 2013 at 7:12am UTC
Thanks for taking a look, my code compiles, but runs infinitely.. couldn't figure out whats wrong?
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
//# include "Coins.h";
#include <iostream>
#include <string>
using namespace std;
struct Coins
{
int date;
string type;
string country;
};
void getCoins(Coins coins[], int SIZE);
void displayCoins(Coins coins[], int SIZE);
int coinCount;
int main()
{
const int SIZE = 10;
Coins coins[SIZE];
char choice;
cout << "Enter upto 10 coins.\n"
<< "Enter a coin (Y/N)?\n" ;
cin >> choice;
while (choice == 'y' ||'Y' )
{
coinCount++;
getCoins(coins, SIZE);
}
cout << "There are " << coinCount << " coins.\n" ;
displayCoins(coins, SIZE);
return 0;
}
//--------------------------------------------
void getCoins(Coins coins[], int SIZE)
{
int i=0;
cout << "Date: " ;
cin >> coins[i].date;
cin.ignore();
cout << endl << "Type: " ;
getline(cin, coins[i].type);
cout << endl << "Country: " ;
getline(cin, coins[i].country);
i++;
}
//---------------------------------------------
void displayCoins(Coins coins[], int SIZE)
{
char choice;
cout << "Display the coins (Y/N)?\n" ;
while (choice == 'y' ||'Y' )
{
system("cls" );
for (int i = 0; i < coinCount; i++)
{
cout << coins[i].date << endl
<< coins[i].type << endl
<< coins[i].country << endl;
}
}
}
vlad from moscow (3650)
Feb 10, 2013 at 8:21am UTC
First of all take into account that in the function declarations
void getCoins(Coins coins[], int SIZE);
void displayCoins(Coins coins[], int SIZE);
name SIZE is not the same as name in definition
const int SIZE = 10;
So it would be bettter to use some other name in the function decclaration that do not confuse readers of the code. For example
void getCoins(Coins coins[], int n);
void displayCoins(Coins coins[], int n);
Further the condition in the while tatement i always equal to true
while(choice == 'y'||'Y')
because 'Y' is converted to true. You shall write
while(choice == 'y' || 'choice == Y')
Also the loop i incorrect becaue the condition does not is not changed inside the loop.
virk (5)
Feb 10, 2013 at 10:05pm UTC
THANKS a lot Smac89 and vlad from moscow. I appreciate your time guys...
my code worked! :)
It's posted below for reference:
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
//# include "Coins.h";
#include <iostream>
#include <string>
using namespace std;
struct Coins
{
int date;
string type;
string country;
};
//--------------------------------------------
void getCoins(Coins coins[], int count);
void displayCoins(Coins coins[], int count);
const int SIZE = 10;
int main()
{
int count = 0;
Coins coins[SIZE];
char choice = 'y' ;
cout << "Enter upto 10 coins.\n" ;
while (choice == 'y' || choice == 'Y' )
{
cout << "count in main = " << count << endl;
getCoins(coins, count);
cout << endl << "Enter a coin (Y/N)?\n" ;
cin >> choice;
system("cls" );
count++;
}
cout << "There are " << count << " coins.\n" ;
displayCoins(coins, count);
return 0;
}
//--------------------------------------------
void getCoins(Coins coins[], int count)
{
cout << " count in getCoins = " << count << endl;
cout << "Date: " ;
cin >> coins[count].date;
cin.ignore();
cout << "Type: " ;
getline(cin, coins[count].type);
cout << "Country: " ;
getline(cin, coins[count].country);
}
//---------------------------------------------
void displayCoins(Coins coins[], int count)
{
for (int i = 0; i < count; i++)
{
cout << "#" << i+1 << ".)" << endl;
cout << "Date: " ;
cout << coins[i].date << endl;
cout << "Type: " ;
cout << coins[i].type << endl;
cout << "Country: " ;
cout << coins[i].country << endl << endl;
}
}
//=============================================
/*OUTPUT:
There are 3 coins.
#1.)
Date: 1
Type: aa
Country: aa
#2.)
Date: 2
Type: bb
Country: bb
#3.)
Date: 3
Type: cc
Country: cc
Press any key to continue . . .
*/
//=============================================