The Open Source Swiss Army Knife

/code/cPP/data_str_cPP/
/code/cPP/data_str_cPP/ + sub-categories
http://www.sirfsup.com/
web directory content
    
      

Not logged in
Chat Register Login
return to:  http:/www.sirfsup.com      /code   /cPP   /data_str_cPP 
Permalink: struct.c
Title: add
article options : please login   |  raw source view  

#include <stdlib.h>
#include <iostream.h>
#include <string.h> //strcpy

struct MyRec {
	char name[21];
	int age;
};
// a declaration which takes up no space in memory
int main () {
	MyRec rec1 = {"Tom Jones", 31};
	// int are moving to all 4-byte integers
	// occupies 23 bytes storage, assuming no extra padding
	strcpy (rec1.name, "Tom Jones");
	cout << rec1.name << "\n" << rec1.age << endl;
	rec1.age=31;
	cout << "enter age of dude in my structure char[21]:" << endl;
 	cin >> rec1.age;
	cout << "enter name of dude in my structure char[21]:" << endl;
	cin >> rec1.name;
//	cin.get(rec1.name, xy); //get including spaces
				// stops at enter key
				//cin.get also takes initial whitespace
		// get up to 21, and also takes enter key
	cout << "name = " << rec1.name << " age = " << rec1.age << endl;
	//so tell it to get rid of any whitespace
	// to read from a file, use payroll.get
	// "file_name.get"
	// get only applies to strings
	// if a single char or number just read it directly.
}

Leave a Reply
Your Name:     anonymous
Your Email:
Website:  
Comments:

The author will be notified of your reply.
return to top