illegal indirection with pointers to structure
Dec 17, 2009 at 9:19pm UTC
Hey,
The following program must ask user to enter number of contributors, then using the dynamic array of structures store the users data about each of the contributor's name and amount donated. Then it must display every contributor with amount donated equal to or greater than 10000 under Grand Patrons, and the rest should be listed under Patrons.
For some reason VC++ 08 Express doesn't like the way I try to access the pointer to structure's members.
Error Log:
...source.cpp(30) : error C2100: illegal indirection
...source.cpp(32) : error C2100: illegal indirection
...source.cpp(33) : error C2100: illegal indirection
...source.cpp(41) : error C2100: illegal indirection
...source.cpp(43) : error C2100: illegal indirection
...source.cpp(44) : error C2100: illegal indirection
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
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
int contributors;
string mystr;
struct contributor
{
string name;
double contribution;
};
cout << "Enter a number of contributors: " ;
getline(cin, mystr);
stringstream(mystr) >> contributors;
contributor * pcontributor = new contributor[contributors];
for (int i = 0; i < contributors; i++)
{
cout << "Enter the name: " ;
getline(cin, pcontributor[i].name);
cout << "Enter the amount: " ;
getline(cin, mystr);
stringstream(mystr) >> pcontributor[i].contribution;
}
cout << "\n--Grand Patrons--\n" ;
for (int i = 0; i < contributors; i++)
{
if (*pcontributor[i].contribution >= 10000)
{
cout << *pcontributor[i].name << endl;
cout << *pcontributor[i].contribution << endl;
}
else
cout << "\nNone\n" ;
continue ;
}
cout << "\n--Patrons--\n" ;
for (int i = 0; i < contributors; i++)
{
if (*pcontributor[i].contribution < 10000)
{
cout << *pcontributor[i].name << endl;
cout << *pcontributor[i].contribution << endl;
}
else
cout << "\nNone\n" ;
continue ;
}
cin.get();
cin.get();
return 0;
}
Dec 17, 2009 at 9:31pm UTC
Remove the asterisks. The offset operator ([]) is already dereferencing the pointer, so you don't need to.
a[b] is the same as *(a+b).
Dec 17, 2009 at 10:16pm UTC
Alright, thanks.
Topic archived. No new replies allowed.