is this program i wrote so far going to store all the data entered by the user correctly?, i dont really understand structures completely yet. i think my cin statements need to store them differently to reference the structure.
my code:

#include <iostream>


int main()
{


struct students
{
char name[10]; //name of student
int SSN; // social security number
char cardtype[10];
float balance;
}
for ( i = 0; i <= 10; i++ )
{
std::cout << "Enter student name\n";
std::cin >> name[ i ];

std::cout << "Enter social security number\n";
std::cin >> SSN;

std::cout << "Enter card type\n";
std::cin >> cardtype[ i ];
}

Recommended Answers

All 16 Replies

no, UDFs are defined outside methods in C and C++.
In Java you could have created an inner class similar to this, in C++ that's not allowed.

Something like

#include <iostream>
#include <string>

struct student
{
    std::string name; //name of student
    int SSN; // social security number
    std::string cardtype;
    float balance;
};

int main()
{
    student students[10];
    for (int i = 0; i <= 10; i++ )
    { 
        std::cout << "Enter student name\n";
        std::cin >> students[i].name;

        std::cout << "Enter social security number\n";
        std::cin >> students[i].SSN;

        std::cout << "Enter card type\n";
        std::cin >> students[i].cardtype;
    }
}

>in C++ that's not allowed
Are you sure? Maybe you should double check. It's allowed, though not very common because declaring a local structure restricts the scope of that structure to the function. That limits its usefulness.

>is this program i wrote so far going to store all the data entered by the user correctly?
No, it won't even compile. You're missing a closing brace for main and structure declarations must end with a semicolon. You also neglect to declare an instance of the structure. I imagine that something more like this is what you wanted:

#include <iostream>

int main()
{
  struct students 
  {
    char name[10]; //name of student
    int SSN; // social security number
    char cardtype[10];
    float balance;
  } array[10];

  for ( int i = 0; i < 10; i++ )
  { 
    std::cout << "Enter student name\n";
    std::cin >> array[i].name[ i ];

    std::cout << "Enter social security number\n";
    std::cin >> array[i].SSN;

    std::cout << "Enter card type\n";
    std::cin >> array[i].cardtype[ i ];
  }
}

Naru, was this intentional?

array.name[ i ];

shouldn't it be just

array.name;
?

Same with array.cardtype[ i ]; it should be array.cardtype; I think.

hmm, so C does support local UDFs inside methods?
Never too old to learn I guess :)

>Naru, was this intentional?
No, occasionally I'll miss something. Even though the way it is is "technically" legal, it's highly unlikely to be what the OP intended. The lint part of my brain was working at the time, but not the logic checker. ;)

whilst we are on the subject of structures.....

I have only experienced this TWICE in all my programming years....

struct var
{
    unsigned int a, b, c, d, e, f, g;
    unsigned char h, i, j, k;
};

sizeof reports this as 36....... but after a system reset it reports 32. The test program was nothing more than a main func with a sizeof call. Why did it temporarily add 4 bytes? Also if I remove an unsigned char it still remains 32; i assume from this all odd numbered sizes are padded +1? or is it padded to nearest base 2, eg a structure of real size 17 pads to 32 and not 18? The main suprise for me is the first option. Only twice have I seen a 'swelling' structure!!!! also it is worth to note that the struct in question is now indeed 32 bytes, BUT fstream usage results in 36 BYTES written, THEN a random string including the filename of a windows dll..... wierd error, no? The program has been fixed but the cause still eludes me, maybe I slipped up somewhere....

It would be odd to change after a system reset. Structures are often padded to fit the alignment you specified somewhere (or the default). So

struct
{
char a;
int b;
} foo;

If the alignment is 1 (unlikely), then this is going to be 5 bytes. If the alignment is 4 (likely) then it will be 8, because the char a got padded with 3 bytes before the int starts.

Some machines demand this (like ARM processors) or they'll fault. Others like x86 don't demand it but often run faster with shorts being short-aligned and longs being long-aligned.

Maybe before you reset you had just compiled something with a large alignment size and somehow your compiler didn't clear that out before compiling your test program?

i have a problem with this code, my cin statement is only reading in one character i think, how do i fix this?

my code:

#include <iostream>

int main()
{
struct students
{
char name[10];
int SSN;
char cardtype[10];
float balance;
} array[10];

for ( int i = 0; i < 10; i++ )
{
std::cout << "Enter student name\n";
std::cin >> array.name[ i ];

std::cout << "Enter social security number\n";
std::cin >> array.SSN;

std::cout << "Enter card type\n";
std::cin >> array.cardtype[ i ];
}

return 0;
}

i have a problem with this code, my cin statement is only reading in one character i think, how do i fix this?

Don't ask it to read only one character.
(And read some of the other responses above.)
(And please make an attempt at a fix!)
(And please learn to use code tags.)

with this structure it says Given your definition, it should be possible to write the declaration:
CARD holder[10];

what does this exactly mean? im really confused.

my structure:

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
struct students
{
char name[10]; //name of student
int SSN; // social security number
char cardtype[10];
float balance;
} array[10];

for ( int i = 0; i < 10; i++ )
{
cout << "Enter student name\n";
cin >> array.name;

cout << "Enter social security number\n";
cin >> array.SSN;

cout << "Enter card type\n";
cin >> array.cardtype;
}

return 0;
}

SeePlusPlus,
Please use Code Tags it makes everyone who is reading your posts so much happier :)

with this structure it says Given your definition, it should be possible to write the declaration:
CARD holder[10];

what does this exactly mean? im really confused.

my structure:

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
struct students 
{
char name[10]; //name of student
int SSN; // social security number
char cardtype[10];
float balance;
} array[10];

for ( int i = 0; i < 10; i++ )
{ 
cout << "Enter student name\n";
cin >> array[i].name;

cout << "Enter social security number\n";
cin >> array[i].SSN;

cout << "Enter card type\n";
cin >> array[i].cardtype;
}





return 0;
}

I am a bit confused by your question but if you are asking what I think then, you have to think as structs as new data types(like int,float,char,...). They are your own defined ones so whenever you need one you just declare it like any other datatype.

#include <iostream>

//this is creating your own datatype named student
//you use it the same as you would any other datatype
struct student
{
  char name[10];
  int SSN;
  char cardType[10];
  float balance;
};

int main()
{
  
  student freshman[10];//creating 10 "freshman"
  
  std::cout<<"Enter the first freshman name"<<std::endl;
  std::cin>>freshman[0].name;
  std::cout<<"Enter the first freshman's SSN"<<std::endl;
  std::cin>>freshman[0].SSN;
  //so on and so forth
  return 0;
}

ok i have to right a function called AddHolder - that allows the user to insert a record into the database. i never lerned about how to do this, can anyone help?

my code:

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
  struct students 
  {
    char name[10];        //name of student
    int SSN;              // social security number
    char cardtype[10];
    float balance;
  } array[10];

  for ( int i = 0; i < 10; i++ )
  { 
    cout << "Enter student name\n";
    cin >> array[i].name;

    cout << "Enter social security number\n";
    cin >> array[i].SSN;

    cout << "Enter card type\n";
    cin >> array[i].cardtype;
  }





return 0;
}

can anyone help with this function?

can anyone help with this function?

I think you need to ask a better question.

with this structure it says Given your definition, it should be possible to write the declaration:
CARD holder[10];

what does this exactly mean? im really confused.

What are you talking about? Where did CARD enter the picture?

ok i have to right a function called AddHolder - that allows the user to insert a record into the database.

Where???

Copy and paste your exact code. If you have not even got the function name in your code, you're really asking someone to write it for you -- don't. Make an attempt.

Where is the attempt?

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
  struct students 
  {
    char name[10];        //name of student
    int SSN;              // social security number
    char cardtype[10];
    float balance;
  } array[10];

  for ( int i = 0; i < 10; i++ )
  { 
    cout << "Enter student name\n";
    cin >> array[i].name;

    cout << "Enter social security number\n";
    cin >> array[i].SSN;

    cout << "Enter card type\n";
    cin >> array[i].cardtype;
  }
   return 0;
}

For that matter, where is CARD?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.