Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,144 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,079 people online right now. Registration is fast and FREE... Join Now!




C++ problem with it please help

 
Reply to this topicStart new topic

C++ problem with it please help, I wrote this program but it doesn't work well when I am entering t

Moe Joe
7 Jun, 2007 - 11:01 AM
Post #1

New D.I.C Head
*

Joined: 7 Jun, 2007
Posts: 3


My Contributions
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
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: C++ Problem With It Please Help
7 Jun, 2007 - 11:09 AM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 3,984



Thanked: 16 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
your title was longer than allowed, so i have no idea what your question is, please describe it as well as post any error msgs you recieve.
remember to use [code] tags around ALL code.
User is offlineProfile CardPM
+Quote Post

ZlxA
RE: C++ Problem With It Please Help
7 Jun, 2007 - 01:47 PM
Post #3

New D.I.C Head
*

Joined: 6 Nov, 2006
Posts: 10


My Contributions
Well without knowing exactly what the problem is and just going by the part of the title that made the cut
"C++ problem with it please help, I wrote this program but it doesn't work well when I am entering t"

I'm assuming you mean the problem is when you're entering the state name / state code (postal code?) in which case the problem is the way you're reading in the state code / how long the state code is (it only holds 1 character). With that changed the rest of the program ran.
User is offlineProfile CardPM
+Quote Post

Moe Joe
RE: C++ Problem With It Please Help
7 Jun, 2007 - 09:39 PM
Post #4

New D.I.C Head
*

Joined: 7 Jun, 2007
Posts: 3


My Contributions
Hi there the problem is that I to write a C++ program in which you hard code a five states names and the five state abbreviations:
Example
New York NY
New Jersey NJ
Pennsylvania PA
Connecticut CT
Rhode Island RI

The program should prompt the user to enter the state name. Let the program do a sequential search to fine the corresponding state postal code. The program should display the postal code.
User is offlineProfile CardPM
+Quote Post

RautRupali
RE: C++ Problem With It Please Help
7 Jun, 2007 - 10:07 PM
Post #5

New D.I.C Head
*

Joined: 7 Jun, 2007
Posts: 44


My Contributions
Hi,

Your program is working fine...

What is probem?????????????

It's searching correctly....


User is offlineProfile CardPM
+Quote Post

Moe Joe
RE: C++ Problem With It Please Help
7 Jun, 2007 - 10:17 PM
Post #6

New D.I.C Head
*

Joined: 7 Jun, 2007
Posts: 3


My Contributions
QUOTE(RautRupali @ 7 Jun, 2007 - 11:07 PM) *

Hi,

Your program is working fine...

What is probem?????????????

It's searching correctly....


if you run it. It would work fine with the first state entry but the second one the program jumps over the state name to the state code and keeps jumping to te state code it should let me input the name and the code both. that is my problem with this program.

please help me out.


User is offlineProfile CardPM
+Quote Post

RautRupali
RE: C++ Problem With It Please Help
7 Jun, 2007 - 10:32 PM
Post #7

New D.I.C Head
*

Joined: 7 Jun, 2007
Posts: 44


My Contributions

Hi...I got ur problem

Just

declare
char state_code[4];

instead of
char state_code;


Now Its working fine............





User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:49PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month