Home » Archimedes archive » Acorn Computing » 1995 02 subscription disc.adf » 9502s » C++/Struct

C++/Struct

This website contains an archive of files for the Acorn Electron, BBC Micro, Acorn Archimedes, Commodore 16 and Commodore 64 computers, which Dominic Ford has rescued from his private collection of floppy disks and cassettes.

Some of these files were originally commercial releases in the 1980s and 1990s, but they are now widely available online. I assume that copyright over them is no longer being asserted. If you own the copyright and would like files to be removed, please contact me.

Tape/disk: Home » Archimedes archive » Acorn Computing » 1995 02 subscription disc.adf » 9502s
Filename: C++/Struct
Read OK:
File size: 02C6 bytes
Load address: 0000
Exec address: 0000
File contents
// A simple example of a structure

#include <iostream.h>
#define SIZE    3

struct inventory {
        char    name[30];       // The name of the item
        int     stock;          // The number in stock
} inv[SIZE];

int
main()
{
        int x;
        for (x = 0; x < SIZE; x++) {
                cout << "Please enter the name of an item:\n";
                cin >> inv[x].name;
                cout << "Now, enter the number in stock: \n";
                cin >> inv[x].stock;
        }
        cout << "Your information is held in an array of structures.\n";
        cout << "Here it is:\n";
        for (x = 0; x < SIZE; x++) {
        cout << inv[x].name << ":\t" << inv[x].stock << "\n";
        }
}
00000000  2f 2f 20 41 20 73 69 6d  70 6c 65 20 65 78 61 6d  |// A simple exam|
00000010  70 6c 65 20 6f 66 20 61  20 73 74 72 75 63 74 75  |ple of a structu|
00000020  72 65 0a 0a 23 69 6e 63  6c 75 64 65 20 3c 69 6f  |re..#include <io|
00000030  73 74 72 65 61 6d 2e 68  3e 0a 23 64 65 66 69 6e  |stream.h>.#defin|
00000040  65 20 53 49 5a 45 20 20  20 20 33 0a 0a 73 74 72  |e SIZE    3..str|
00000050  75 63 74 20 69 6e 76 65  6e 74 6f 72 79 20 7b 0a  |uct inventory {.|
00000060  20 20 20 20 20 20 20 20  63 68 61 72 20 20 20 20  |        char    |
00000070  6e 61 6d 65 5b 33 30 5d  3b 20 20 20 20 20 20 20  |name[30];       |
00000080  2f 2f 20 54 68 65 20 6e  61 6d 65 20 6f 66 20 74  |// The name of t|
00000090  68 65 20 69 74 65 6d 0a  20 20 20 20 20 20 20 20  |he item.        |
000000a0  69 6e 74 20 20 20 20 20  73 74 6f 63 6b 3b 20 20  |int     stock;  |
000000b0  20 20 20 20 20 20 20 20  2f 2f 20 54 68 65 20 6e  |        // The n|
000000c0  75 6d 62 65 72 20 69 6e  20 73 74 6f 63 6b 0a 7d  |umber in stock.}|
000000d0  20 69 6e 76 5b 53 49 5a  45 5d 3b 0a 0a 69 6e 74  | inv[SIZE];..int|
000000e0  0a 6d 61 69 6e 28 29 0a  7b 0a 20 20 20 20 20 20  |.main().{.      |
000000f0  20 20 69 6e 74 20 78 3b  0a 20 20 20 20 20 20 20  |  int x;.       |
00000100  20 66 6f 72 20 28 78 20  3d 20 30 3b 20 78 20 3c  | for (x = 0; x <|
00000110  20 53 49 5a 45 3b 20 78  2b 2b 29 20 7b 0a 20 20  | SIZE; x++) {.  |
00000120  20 20 20 20 20 20 20 20  20 20 20 20 20 20 63 6f  |              co|
00000130  75 74 20 3c 3c 20 22 50  6c 65 61 73 65 20 65 6e  |ut << "Please en|
00000140  74 65 72 20 74 68 65 20  6e 61 6d 65 20 6f 66 20  |ter the name of |
00000150  61 6e 20 69 74 65 6d 3a  5c 6e 22 3b 0a 20 20 20  |an item:\n";.   |
00000160  20 20 20 20 20 20 20 20  20 20 20 20 20 63 69 6e  |             cin|
00000170  20 3e 3e 20 69 6e 76 5b  78 5d 2e 6e 61 6d 65 3b  | >> inv[x].name;|
00000180  0a 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |.               |
00000190  20 63 6f 75 74 20 3c 3c  20 22 4e 6f 77 2c 20 65  | cout << "Now, e|
000001a0  6e 74 65 72 20 74 68 65  20 6e 75 6d 62 65 72 20  |nter the number |
000001b0  69 6e 20 73 74 6f 63 6b  3a 20 5c 6e 22 3b 0a 20  |in stock: \n";. |
000001c0  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 63  |               c|
000001d0  69 6e 20 3e 3e 20 69 6e  76 5b 78 5d 2e 73 74 6f  |in >> inv[x].sto|
000001e0  63 6b 3b 0a 20 20 20 20  20 20 20 20 7d 0a 20 20  |ck;.        }.  |
000001f0  20 20 20 20 20 20 63 6f  75 74 20 3c 3c 20 22 59  |      cout << "Y|
00000200  6f 75 72 20 69 6e 66 6f  72 6d 61 74 69 6f 6e 20  |our information |
00000210  69 73 20 68 65 6c 64 20  69 6e 20 61 6e 20 61 72  |is held in an ar|
00000220  72 61 79 20 6f 66 20 73  74 72 75 63 74 75 72 65  |ray of structure|
00000230  73 2e 5c 6e 22 3b 0a 20  20 20 20 20 20 20 20 63  |s.\n";.        c|
00000240  6f 75 74 20 3c 3c 20 22  48 65 72 65 20 69 74 20  |out << "Here it |
00000250  69 73 3a 5c 6e 22 3b 0a  20 20 20 20 20 20 20 20  |is:\n";.        |
00000260  66 6f 72 20 28 78 20 3d  20 30 3b 20 78 20 3c 20  |for (x = 0; x < |
00000270  53 49 5a 45 3b 20 78 2b  2b 29 20 7b 0a 20 20 20  |SIZE; x++) {.   |
00000280  20 20 20 20 20 63 6f 75  74 20 3c 3c 20 69 6e 76  |     cout << inv|
00000290  5b 78 5d 2e 6e 61 6d 65  20 3c 3c 20 22 3a 5c 74  |[x].name << ":\t|
000002a0  22 20 3c 3c 20 69 6e 76  5b 78 5d 2e 73 74 6f 63  |" << inv[x].stoc|
000002b0  6b 20 3c 3c 20 22 5c 6e  22 3b 0a 20 20 20 20 20  |k << "\n";.     |
000002c0  20 20 20 7d 0a 7d                                 |   }.}|
000002c6