Arrays with integers and strings?

Hi!
I'm working with arrays and I'm wondering how I would put an integer and a string into the same array, if that's possible?

Here's my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 #include <string>
#include <iostream>

using namespace std;
void displayArray(string x[][3], int r, int c);

void displayPurchases(string item1, int quantity1, int price1, string item2, int quantity2, int price2, string item3, int quantity3, int price3, string item4, int quantity4, int price4)
{
	cout << endl;

	string purchases[3][3] = { {"Description", "Quantity", "Price"}, {item1} };
	displayArray(purchases, 3, 2);


	return;
}


I want my output to look like this:


Description     Quantity     Price

Item1           Quantity1    Price1
Item2           Quantity2    Price2
etc...

Last edited on
Use a struct:

1
2
3
4
5
6
struct Purchase
{
   string item;
   int quantity;
   int price;
};


Then
vector<Purchase> purchases;
Last edited on
You could use a std::tuple to hold the int (item #), std::string (item name) and double (item price).

http://www.cplusplus.com/reference/tuple/tuple/

And use a std::vector to hold each item's tuple. A vector can hold a dynamic number of elements, you wouldn't be restricted to 3, or having to over-allocate an array to fit a changeable number of items.

http://www.cplusplus.com/reference/vector/vector/
Hello raneie,

It is helpful if you include the whole code that can compile and run.

With what you have posted there is a problem:

5
6
7
void displayArray(string x[][3], int r, int c);

void displayPurchases(string item1, int quantity1, int price1, string item2, int quantity2, int price2, string item3, int quantity3, int price3, string item4, int quantity4, int price4)

Inside the () the parameters of each must match.

Using a struct as lastchance has suggested is the best to work with data of different types. If you can use a vector that would be the best, but a simple array would work. And you would only need a 1D array for this.

Hope that helps,

Andy

Edit: I discovered this difference when working with the program.
Last edited on
it depends on what exactly you want.
you can store integers as text, if you just want to print it, but not do math to it:
"3" is the string for 3, as per your example.

you can put 2 types into a container of some sort and make an array of that, as mentioned above.

From there it begins to get exotic and weird and 'maybe don't do this' areas...

the union type lets you pick what type something is from your defined set of types. But it can only hold ONE of the types, not all at once. There is a variant or something similar as well. These are uncommon, but useful at times.

you can directly hijack the bytes. This is usually not recommended.
string imanint = "12345678";// 8 bytes worth of data can hold up to 64 bit int
int * ip = (int*)(&imanint[0]);
ip[0] = 31415;
cout << ip[0];
//the string contains gibberish. Its just storing the int for whatever insane reason
cout << imanint; //binary junk

I tried using a struct and this is where I got:

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
#include <string>
#include <iostream>
#include "purchasesDirectory.h"

using namespace std;
void displayArray(Directory Purchases[4], int r);

void displayPurchases(string item1, int quantity1, int price1, string item2, int quantity2, int price2, string item3, int quantity3, int price3, string item4, int quantity4, int price4)
{
	Directory Purchases[4];

	Purchases[0].item = item1;
	Purchases[0].quantity = quantity1;
	Purchases[0].price = price1;

	Purchases[1].item = item2;
	Purchases[1].quantity = quantity2;
	Purchases[1].price = price2;

	Purchases[2].item = item3;
	Purchases[2].quantity = quantity3;
	Purchases[2].price = price3;

	Purchases[3].item = item3;
	Purchases[3].quantity = quantity3;
	Purchases[3].price = price3;

        displayArray(Purchases, 4);

	return;
}


Array code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "purchasesDirectory.h"
#include <string>
#include<iostream>
using namespace std;

void displayArray(Directory Purchases[4], int r)
{
	Directory Purchases;

	for (int rows = 0; rows <= r; rows++)
	{
		cout << Purchases[rows];
	}
}


struct header:
1
2
3
4
5
6
7
8
9
10
11
12
#pragma once
#include <iostream>

using namespace std;

struct Directory
{
	string item;
	int quantity;
	int price;
};


However, there's an error saying "no operater '<<' matches the operands.
Also, I'm not sure if I'm using the struct properly to display it in an array, sorry.
Last edited on
you can't do that, you need
cout << Purchases[rows].item << " " << Purchases[rows].quantity //etc
you can overload << to work but its a little advanced. I rarely do that, as it assumes the desired format, rather than let the user of the class decide, but there are merits to both ways.
I did the entire project again but with what you suggested jonnin and with a little more research online.

Everything worked perfectly!

Thanks everyone so much for the help :)
Topic archived. No new replies allowed.