-
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?
-
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.
-
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?
-
> 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');