CODE
#include <iostream>
#include <iomanip>
#include <cctype>
#include <cstring>
using namespace std;
struct STATE_STRUCT
{
char state_name[31];
char state_code;
};
void Load_State_Table(STATE_STRUCT[],int);
void Display_State_Table(STATE_STRUCT[],int);
int main()
{
const int TABLE_SIZE=5;
STATE_STRUCT state_table[TABLE_SIZE];
int row;
char state_key[31];
char response[2];
cout<<setprecision(2)
<<setiosflags(ios::fixed)
<<setiosflags(ios::showpoint);
Load_State_Table(state_table, TABLE_SIZE);
cout<<endl<<endl;
cout<<"This is the table you entered.";
Display_State_Table(state_table,TABLE_SIZE);
cout<<endl<<endl;
cout<<"Do you want to search the table? (Y/N): ";
cin.getline(response,2);
while (toupper(*response)=='Y')
{
cout<<endl;
cout<<"Enter the State name: ";
cin.getline(state_key,31);
for(row=0;row<TABLE_SIZE;++row)
if(strcmp(state_table[row].state_name,state_key)==0)
break;
if(row==TABLE_SIZE)
{
cout<<endl;
cout<<"The search for "<<state_key
<<"Was not successful."<<endl;
}
else
{
cout<<endl;
cout<<"The State code for "<<state_key<<": "<<endl<<endl;
cout<<"State Postal Code is: "<<setw(10)
<<state_table[row].state_code<<endl;
}
cout<<endl<<endl;
cout<<"Do you want to search the table? (Y/N): ";
cin.getline(response,2);
}
return 0;
}
void Load_State_Table(STATE_STRUCT state_table[],int size)
{
int row;
cout<<endl;
cout<<"Enter the table values as you are prompted:"
<<endl<<endl;
for(row=0;row<size;++row)
{
cout<<endl;
cout<<"For row# "<<row+1<<" enter:"<<endl;
cout<<"State Name: ";
cin.getline(state_table[row].state_name, 31);
cout<<"State Postal Code: ";
cin>>state_table[row].state_code;
cin.get();
}
}
void Display_State_Table(STATE_STRUCT state_table[],int size)
{
int row;
cout<<endl<<endl;
cout<<setw(20)<< "STATE NAME"
<<setw(20)<< "STATE CODE"<<endl;
for(row=0;row<size;++row)
{
cout<<endl;
cout<<setw(20)<<state_table[row].state_name
<<setw(20)<<state_table[row].state_code;
}
}
[Mod Edit] by placing the code in between code tags you create that neat box which preserves formating and makes your code easier to read and to copy and paste into an editor.
[code]your code here[/code]This post has been edited by NickDMax: 7 Jun, 2007 - 09:59 PM