Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,690 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,501 people online right now. Registration is fast and FREE... Join Now!




Syntax error: Struct/class access

 
Reply to this topicStart new topic

Syntax error: Struct/class access

quim
post 27 Dec, 2005 - 02:35 PM
Post #1


D.I.C Head

Group Icon
Joined: 11 Dec, 2005
Posts: 145



Thanked 2 times

Dream Kudos: 350
My Contributions


I dont know what is wrong with this small Program.
Anyone to help me find the error?
when I compile it.it shows me this error:

Error 1 error C2228: left of '.firstName' must have class/struct/union c:\.....

Error 2 error C2228: left of '.lastName' must have class/struct/union c:\.....

Error 3 error C2228: left of '.creditCard' must have class/struct/union c:\.......



CODE

#include <iostream>
#include <cstring>
using namespace std;



    class NameDataSet
    {
    public:
 char firstName[128];
 char lastName[128];
 int creditCard;
    };
    
    // function prototypes:
    bool getData (NameDataSet& nds);
    void displayData (NameDataSet& nds);

    int main ()
    {
 const int MAX = 25;
 NameDataSet nds [MAX];
    
 //load first name, last name and social security...

 cout << "Read name/credit card information \n"
   << "Enter 'exit' to quit"
   <<endl;
 int index = 0;
 while (getData(nds[index]) && index < MAX)
 {
     index++;
 }
 //display the name and numbers entered
 cout << "\nEntries: " << endl;
 for (int i = 0; i <index; i++)
 {
     displayData(nds [i]);

     //compare the nameinpit irrespective of case
     if (stricmp(nds.firstName, "exit") == 0)
     {
   return false;
     }

     cout << " Enter last name: ";
     cin >> nds.lastName;

     cout << "Enter credit card number: ";
     cin >> nds.creditCard;
     return true;
 }
 // displayData - diaplay a data set
 void displayData (NameDataSet& nds)
 {
     cout << nds.firstName
    << ""
    << nds.lastName
    << "/"
    << nds.creditCard
    <<endl;
 }

sleepy.gif

This post has been edited by quim: 27 Dec, 2005 - 03:50 PM
User is offlineProfile CardPM

Go to the top of the page


supersloth
post 27 Dec, 2005 - 02:59 PM
Post #2


serial frotteur

Group Icon
Joined: 21 Mar, 2001
Posts: 19,493



Thanked 11 times

Dream Kudos: 2147483647

Expert In: being gentlemanly

My Contributions


what error is it giving you when you try to compile?
User is online!Profile CardPM

Go to the top of the page

Mrafcho001
post 27 Dec, 2005 - 03:50 PM
Post #3


D.I.C Addict

Group Icon
Joined: 1 Nov, 2005
Posts: 753



Thanked 5 times

Dream Kudos: 120
My Contributions


nds is an array

CODE

nds[0].members
//You are doing
nds.members

//Solutions
/*  Either make nds a single non array variable
    but in your situation just add [i] after nds and before the dot
    */

/* Also yo have not closed the main function and never defined
   void getData(NameDataSet &nds)
*/

User is offlineProfile CardPM

Go to the top of the page

quim
post 27 Dec, 2005 - 04:32 PM
Post #4


D.I.C Head

Group Icon
Joined: 11 Dec, 2005
Posts: 145



Thanked 2 times

Dream Kudos: 350
My Contributions


QUOTE(Mrafcho001 @ 27 Dec, 2005 - 05:47 PM)
nds is an array

CODE

nds[0].members
//You are doing
nds.members

//Solutions
/*  Either make nds a single non array variable
    but in your situation just add [i] after nds and before the dot
    */

/* Also yo have not closed the main function and never defined
   void getData(NameDataSet &nds)
*/


Oh yeah i forgot to define the void funtion.

this is how it will be.


CODE


#include <iostream>
#include <cstring>
using namespace std;

class NameDataSet
    {
    public:
 char firstName[128];
 char lastName[128];
 int creditCard;
    };
    
    // function prototypes:
    bool getData (NameDataSet& nds);
    void displayData (NameDataSet& nds);

    int main()
    {
 const int MAX = 25;
 NameDataSet nds [MAX];
    
 //load first name, last name and social security...

 cout << "Read name/credit card information \n"
   << "Enter 'exit' to quit"
   <<endl;
 int index = 0;
 while (getData(nds[index]) && index < MAX)
 {
     index++;
 }
 //display the name and numbers entered
 cout << "\nEntries: " << endl;
 for (int i = 0; i <index; i++)
 {
     displayData(nds [i]);
 }
    }
    // get data populate a NameDataSet object
    bool getData(NameDataSet& nds)
    {
 cout << "\nEnter first name:";
 cin  >> nds.firstName;
    
 

     //compare the name input irrespective of case
     if (stricmp(nds.firstName, "exit") == 0)
     {
   return false;
     }

     cout << " Enter last name: ";
     cin >> nds.lastName;

     cout << "Enter credit card number: ";
     cin >> nds.creditCard;
     return true;
    }
 // displayData - diaplay a data set
 void displayData (NameDataSet& nds)
 {
     cout << nds.firstName
    << " "
    << nds.lastName
    << " / "
    << nds.creditCard
    <<endl;
 }



This post has been edited by quim: 27 Dec, 2005 - 04:33 PM
User is offlineProfile CardPM

Go to the top of the page

Mrafcho001
post 27 Dec, 2005 - 04:34 PM
Post #5


D.I.C Addict

Group Icon
Joined: 1 Nov, 2005
Posts: 753



Thanked 5 times

Dream Kudos: 120
My Contributions


Again, nds is an array

add this after every nds:

CODE

[i]
//-------------------------------
//so it should look like

nds[i].firstName
nds[i].lastName
nds[i].creditCard
User is offlineProfile CardPM

Go to the top of the page

bluesuus
post 27 Dec, 2005 - 04:48 PM
Post #6


D.I.C Head

Group Icon
Joined: 26 Dec, 2005
Posts: 57



Dream Kudos: 25
My Contributions


1) since you do not have any methods for your class why use class at all..Use struct.
CODE

    struct NameDataSet
    {
 char firstName[128];
 char lastName[128];
 int creditCard;
    };

2) your function dispalydata() should take an integer variable represeting size in their parameter)...good programming pratice.
3) why are using the string "exit", use something like 'q' to exit...its easier.u dont have to do string compare.
4)i cnt find the function definition of getData()...so i dnt knw waht u r doin in the while loop..but i hv a suggestion
why dont you read in the information before the while loop.example
CODE

    int main()
    {
 
    cout <<"Read fname";

    int index=0;
    cin >> nds[index].firstName;
    

    while( index < max && (nds[index].firstName) != 'q')  )
    {
 cout << "last name";
 cin >> nds[index].lastName;
 cout << "credit info";
 cin >> nds[index].creditCard;

 cout << "Enter first name for .................
 index++;
 cin >> nds[index].firstName;
    }

its just a suggestion..and you should fix some errors because im jus writing, i dint compile or anytin..jus givn u an idea.

5) back to your code,i hv found ur function getData().... nds is an array, u shud call it with nds[index].firstName inside the function getData()..correct that and u r done

6)
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 27 Dec, 2005 - 07:01 PM
Post #7


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,163



Thanked 32 times

Dream Kudos: 25
My Contributions


QUOTE(bluesuus @ 27 Dec, 2005 - 08:45 PM)
1) since you do not have any methods for your class why use class at all..Use struct.
CODE

    struct NameDataSet
    {
 char firstName[128];
 char lastName[128];
 int creditCard;
    };


This looks like an academic assignment...it is probable that this is the first step in programming a true class, complete with methods.

While exiting on 'exit' or 'q' does not really make a difference (in C++, the string object may be used for direct compares through the overloaded == operator, unless you have been directed to use an array of chars instead of the string object), I would suggest that you determine if the user wishes to exit before assigning the input value to the name variable for comparison.
User is online!Profile CardPM

Go to the top of the page

born2c0de
post 27 Dec, 2005 - 08:43 PM
Post #8


printf("I'm a %XR",195936478);

Group Icon
Joined: 26 Nov, 2004
Posts: 3,895



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


[To bluesuus]
You can't use structures always if there are no functions to a class. If the other data members are declared private...they're better off in a class than as a structure.

[Adding to bluesuus' Post]
hey guim,not just because of "no-functions"...but all your data members are public.
Using a struct structure would be ideal.
Using a class does not give you any advantage.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 28 Dec, 2005 - 06:49 AM
Post #9


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,163



Thanked 32 times

Dream Kudos: 25
My Contributions


Again, this looks like an academic assignment...it may well be that the user is directed to use a class, as part of an ongoing set of exercises.
User is online!Profile CardPM

Go to the top of the page

bluesuus
post 29 Dec, 2005 - 10:00 AM
Post #10


D.I.C Head

Group Icon
Joined: 26 Dec, 2005
Posts: 57



Dream Kudos: 25
My Contributions


well i have to say GOOD JOB for quim.i mean the way he entangled everything inside functions...Excellent programming feature.I love functions.And also to say that we have a hell lot of good programmers in this place..eg the people who replied to this post.nice job
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 08:16AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month