Thread: C++ Structures

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    10

    C++ Structures

    Code:
    #include<iostream>
    #include<iomanip>
    #include<fstream>
    using namespace std;
    
    
    
    struct info_struct
    {
    	char ID[5];
    	int number_year;
    	int number_CD;
    };
    
    
    
    void Display(info_struct[], int);
    void Passing(info_struct[], int);
    
    int main()
    {
    	char select;
    	cout << setprecision(2)
    		<< setiosflags(ios::fixed)
    		<< setiosflags(ios::showpoint);
    
    
    
    	info_struct info[4] = 
    	{{"M123" , 2, 3},
    	{"M225", 1, 6},
    	{"M248", 2, 1},
    	{"M552", 3, 5}};
    	do
    	{
    	
    	cout << "Mr. Muzik's Fanatastic Music Club\n";
    	cout << "==================================\n";
    	cout << "A - Display Annual Report\n";
    	cout << "B - Input Information\n";
    	cout << "Please make a selection: ";
    	cin >> select;
    	
    	switch(select)
    	{
    	case 'A':
    	case 'a':
    		
    		
    			 Display(info, 4);
    
    		break;
    
    			Passing(info, 4);
    
    	case 'B':
    	case 'b':
    			
    
    		break;
    	default:
    		cout << endl << endl
    			<< "Invalid Property Code! Try again.\n" << endl;
    		break;
    
    
    	}
    	}while((select != 'a' || select != 'A') && (select != 'B' || select != 'b'));
    	return 0;
    }
    
    
    void Display(info_struct NEW[], int size)
    {
    			int i;
    			cout << endl << endl;
    		cout << setw(20)<< "ID" 
    			 << setw(20) << "Number of Year"
    			 << setw(20) << "Number of CD bought"  << endl;
    
    			for (i = 0; i < size; ++i)
    			{
    				cout << endl;
    				cout << setw(20)<< NEW[i].ID 
    					 << setw(20)<< NEW[i].number_year 
    					 << setw(20)<< NEW[i].number_CD  << endl;
    			}
    }
    void Passing(info_struct NEW_2, int size_2)
    
    {
    	int row;
    
    	cout << endl;
    	cout << "Enter the table values: "
    		<<endl << endl;
    	for(row = 0; row < size_2; ++row)
    	{
    		cout << endl;
    		
    		cout << "ID Number: ";
    		cin.getline(NEW_2[row].ID, 5);
    
    		cout << "Number of Year: ";
    		cin >> NEW_2[row].number_year;
    
    		cout << "Number of CD bought: ";
    		cin >> NEW_2[row].number_CD;
    
    		cin.get();
    	}
    }
    Two quick question in order to put information in like how many numbers of CDs would I put it inside the main or out? And if its out how can I make the second function work?

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    The definition of the struct only define's what it is, what it's values are, functions and such.

    You can create an instance of the struct in the main method or any method definition outside of main that will be eventually called within main.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    10

    Program works

    Okay I see well now th program works but it is not letting the user put in the information if it's not too much trouble I guess would I have to have a string before a getline under the void Passing?

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > cin >> select;
    You probably need to add a cin.ignore() after this cin, as the newline entered by the user is still left in the input buffer, which will be waiting to be read by the next getline().
    Code:
    	cin >> select;
    	cin.ignore(80, '\n');
    Or:
    Code:
    	cin >> select;
    	cin.ignore(std::streamsize::max(), '\n');

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Structures within Structures
    By Misko82 in forum C Programming
    Replies: 2
    Last Post: 08-27-2007, 12:25 AM
  3. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 08:00 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM