Declaring an array of structures

The point of the program is to create a structure CandyBar, fill it up with members(name, weight, calories), construct an array of 3 more CandyBar structures, initialize them to values of your choice, and then display the contents of each structure.

The main problem is array declaration of 3 new CandyBar structures.
Did I do it wrong, or I simply missed something?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
int main()
{
using namespace std;
struct CandyBar
{
	std::string name;
	float weight;
	int calories;
};
	
CandyBar * pointer = new CandyBar[3];
cout << "Enter the nameof snack: ";
getline(cin, pointer[0]);
cout << *pointer;
system("pause");
return 0;
}

*The program is not done completely, hence I was trying to make it all work with one of the newly created structures, and then add the rest.
Last edited on
It should look more like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
using namespace std;

struct CandyBar
{
	std::string name;
	float weight;
	int calories;
};

int main()
{
	CandyBar * pointer = new CandyBar[3];
	cout << "Enter the nameof snack: ";
	getline(cin, pointer[0].name);
	cout << pointer[0].name;
	
	system("pause");
	return 0;
}


Move using namespace std; and your struct out of the main function and change the way you are attempting to access the data member to use the . (dot) operator.
Oh, I see now.

Need to get more used to the .(dot) operator.

Thanks.
Don't forget to delete the memory that was allocated by new.
For some reason I get the following errors:

1>c:\documents and settings\user\my documents\visual studio 2008\projects\candy\candy\source.cpp(17) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_string<_Elem,_Traits,_Alloc> &' from 'float'
1> c:\program files\microsoft visual studio 9.0\vc\include\string(527) : see declaration of 'std::getline'
...


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
#include <iostream>
#include <string>
using namespace std;

struct CandyBar
	{
		std::string name;
		float weight;
		int calories;
	};
int main()
{
	CandyBar * pointer = new CandyBar[3];
	cout << "Enter the name of snack: ";
	getline(cin, pointer[0].name);
	cout << "Enter the weight of the snack: ";
	getline(cin, pointer[0].weight);
	cout << "Enter the amount of calories of the snack: ";
	getline(cin, pointer[0].calories);
	cout << "How about another snack?\n";
	cout << "Enter the name of another snack: ";
	getline(cin, pointer[1].name);
	cout << "Enter the weight of the snack: ";
	getline(cin, pointer[1].weight);
	cout << "Enter the amount of calories of the snack: ";
	getline(cin, pointer[1].calories);
	cout << "Excellent! How about yet another snack?\n";
	cout << "Enter the name of yet another snack: ";
	getline(cin, pointer[2].name);
	cout << "Enter the weight of the snack: ";
	getline(cin, pointer[2].weight);
	cout << "Enter the amount of calories of the snack: ";
	getline(cin, pointer[2].calories);
	cout << "Here it goes:\n";
	cout << "--Snack #1--\n";
	cout << "Name: " << pointer[0].name << endl;
	cout << "Weight: " << pointer[0].weight << endl;
	cout << "Calories: " << pointer[0].calories << endl;
	cout << "--Snack #2--\n";
	cout << "Name: " << pointer[1].name << endl;
	cout << "Weight: " << pointer[1].weight << endl;
	cout << "Calories: " << pointer[1].calories << endl;
	cout << "--Snack #3--\n";
	cout << "Name: " << pointer[2].name << endl;
	cout << "Weight: " << pointer[2].weight << endl;
	cout << "Calories: " << pointer[2].calories << endl;
	cout << "--The End--\n";
	delete [] pointer;
                system("pause");
	return 0;
}


seymore15074:
Don't forget to delete the memory that was allocated by new.

Yep.
Last edited on
getline() reads a line of text and stores it in a std::string, which is the second parameter.

You are using getline to read into floats, ints, etc.
Ok.

The code worked after I changed all of the inputs with float and int from:

1
2
3
4
...
cout << "Enter the weight of the snack: ";
getline(cin, pointer[0].weight);
...


To:

1
2
3
4
...
cout << "Enter the weight of the snack: ";
(cin >> pointer[0].weight).get();
...
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
	cout << "--Snack #1--\n";
	cout << "Name: " << pointer[0].name << endl;
	cout << "Weight: " << pointer[0].weight << endl;
	cout << "Calories: " << pointer[0].calories << endl;
	cout << "--Snack #2--\n";
	cout << "Name: " << pointer[1].name << endl;
	cout << "Weight: " << pointer[1].weight << endl;
	cout << "Calories: " << pointer[1].calories << endl;
	cout << "--Snack #3--\n";
	cout << "Name: " << pointer[2].name << endl;
	cout << "Weight: " << pointer[2].weight << endl;
	cout << "Calories: " << pointer[2].calories << endl;


Can you notice the similarities between all 3 snack codes?

A for() loop would do well there and for the code block where you are getting the candy bar's data.
Last edited on
Yep, I'm about to learn loops.
Topic archived. No new replies allowed.