C++ struct adding and displaying a bookstore's magazines.
#include <iostream> using namespace std; #include <ctype.h> #include <stdio.h> struct mag_info//from ww w . jav a2 s .c o m { char title[25]; char pub[25]; int month; int year; int stock_copies; int order_copies; float price; }; mag_info fill_mags(struct mag_info mag); void print_mags(struct mag_info mags[], int mag_ctr); void main() { mag_info mags[1000]; int mag_ctr=0; // Number of magazine titles. char ans; do { mags[mag_ctr] = fill_mags(mags[mag_ctr]); cout << "Do you want to enter another magazine? "; fflush(stdin); ans = getchar(); fflush(stdin); // Discards carriage return. if (toupper(ans) == 'Y') { mag_ctr++; } } while (toupper(ans) == 'Y'); print_mags(mags, mag_ctr); return; // Returns to operating system. } void print_mags(mag_info mags[], int mag_ctr) { int i; for (i=0; i<=mag_ctr; i++) { cout << "\n\nMagazine " << i+1 << ":\n";// Adjusts for subscript. cout << "\Name: " << mags[i].title << "\n"; cout << "\tPublisher: " << mags[i].pub << "\n"; cout << "\tPub. Month: " << mags[i].month << "\n"; cout << "\tPub. Year: " << mags[i].year << "\n"; cout << "\tIn-stock: " << mags[i].stock_copies << "\n"; cout << "\tOn order: " << mags[i].order_copies << "\n"; cout << "\tPrice: " << mags[i].price << "\n"; } return; } mag_info fill_mags(mag_info mag) { puts("\n\nWhat is the title? "); gets(mag.title); puts("Who is the publisher? "); gets(mag.pub); puts("What is the month (1, 2, ..., 12)? "); cin >> mag.month; puts("What is the year? "); cin >> mag.year; puts("How many copies in stock? "); cin >> mag.stock_copies; puts("How many copies on order? "); cin >> mag.order_copies; puts("How much is the magazine? "); cin >> mag.price; return (mag); }