Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3027

struct usage and setup

$
0
0
Hi,
I have included a small program that revolves around struct's. This program works to a certain extent. I have three questions about the struct form and how it is used:
1.) Is there a way to enter data without asking the user how many records you want to add (howmany)?
2.) Is there a way to to make the struct persistent. In other words can you end the program and then start the program again and then add more data to the struct?
3.) What are some of the uses of the struct?
Thanks

Code:


//
#include <iostream>

const int strsize = 30;

// Benevolent Order of Programmers name structure
struct bop {
    char fullname[strsize]; //real name
    char title[strsize];    //job title
    char bopname[strsize];  //secret bop name
    int preference;        //0 = fullname, 1 = title, 2 = bopname
};

int main()
{
    using namespace std;
    int howmany = 0;
    cout << "How many bop's are you going to load into the bop structure?: ";
    cin >> howmany;
    cin.get();
    int count = 0;
    bop * ps = new bop[howmany];

    while (count < howmany)
    {
      cout << "\n" << "Please enter bop title: ";
      cin.get(ps[count].title, strsize);
      cin.get();
      cout << "\n" <<"Please enter bop fullname: ";
      cin.get(ps[count].fullname, strsize);
      cin.get();
      cout << "\n" <<"Please enter bopname: ";
      cin.get(ps[count].bopname, strsize);
      cin.get();
      cout << "\n" << "Please enter preference: ";
      cin >> ps[count].preference;
      cin.get();
      /*
      cout << "bop title: " << ps[count].title << "\n";
      cout << "bop fullname: " << ps[count].fullname << "\n";
      cout << "bopname: " << ps[count].bopname << "\n";
      cout << "preference: " << ps[count].preference << "\n";
      */
      count++;
    }
    cout << "Please enter one of the following choices:\n";
    cout << "a. display by name\t" << "b. display by title\n";
    cout << "c. display by bopname\t" << "d. display by preference\n";
    cout << "q. quit\n";
    cout << "Enter  choice ";
    char choice;
    cin >> choice;
    while (choice != 'Q' && choice != 'q')
    {
        switch(choice)
        {
            case 'a':
            case 'A':
                    count = 0;
                    while (count < howmany)
                      {
                      cout << ps[count].fullname << "\n";
                      count++;
                      }
                      break;
            case 'b':
            case 'B':
                    count = 0;
                    while (count < howmany)
                      {
                      cout << ps[count].title << "\n";
                      count++;
                      }
                      break;
            case 'c':
            case 'C':
                    count = 0;
                    while (count < howmany)
                      {
                      cout << ps[count].bopname << "\n";
                      count++;
                      }
                      break;
            case 'd':
            case 'D': cout << "games are for the young at heart \n";
                      break;
            default : cout << "Enter a valid choice.\n";
        }
        cout << "Next choice: ";
        cin >> choice;
    }


    cin.get();
    cin.get();
    return 0;
}


Viewing all articles
Browse latest Browse all 3027

Latest Images

Trending Articles



Latest Images