C++ problem with it please help

I wrote this program but it doesn't work well when I am entering t

Page 1 of 1

7 Replies - 1304 Views - Last Post: 08 August 2011 - 11:54 PM Rate Topic: -----

#1 Moe Joe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-June 07

C++ problem with it please help

Posted 07 June 2007 - 12:01 PM

#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: 07 June 2007 - 10:59 PM


Is This A Good Question/Topic? 0
  • +

Replies To: C++ problem with it please help

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: C++ problem with it please help

Posted 07 June 2007 - 12:09 PM

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.
Was This Post Helpful? 0
  • +
  • -

#3 ZlxA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 07-November 06

Re: C++ problem with it please help

Posted 07 June 2007 - 02:47 PM

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.
Was This Post Helpful? 0
  • +
  • -

#4 Moe Joe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-June 07

Re: C++ problem with it please help

Posted 07 June 2007 - 10:39 PM

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.
Was This Post Helpful? 0
  • +
  • -

#5 RautRupali  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 07-June 07

Re: C++ problem with it please help

Posted 07 June 2007 - 11:07 PM

Hi,

Your program is working fine...

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

It's searching correctly....
Was This Post Helpful? 0
  • +
  • -

#6 Moe Joe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-June 07

Re: C++ problem with it please help

Posted 07 June 2007 - 11:17 PM

View PostRautRupali, on 7 Jun, 2007 - 11:07 PM, said:

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.
Was This Post Helpful? 0
  • +
  • -

#7 RautRupali  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 07-June 07

Re: C++ problem with it please help

Posted 07 June 2007 - 11:32 PM

Hi...I got ur problem

Just

declare
char state_code[4];

instead of
char state_code;


Now Its working fine............
Was This Post Helpful? 0
  • +
  • -

#8 sunilchintu2468  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 37
  • Joined: 29-May 11

Re: C++ problem with it please help

Posted 08 August 2011 - 11:54 PM

Why you are using getline function? That too offset 31?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1