Click here to Skip to main content
15,880,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to study c++ about array and I'm having a problem displaying the name from the array name

Because when I try to display the name of the passenger based on where he seat the first letter becomes the last letter of his name..ahmm

Here's my code

C++
#include <iostream.h>
#include <conio.h>

char* name[10]={" "};
int* seat=0;
char* pname="The Passenger is: ";
char ask;
void display();

int main()
{
	clrscr();
	cout<<"The Entering of name and seat number..."<<endl;
	do
	{
		cout<<"\nEnter your name: ";
		cin>>*name;
		cout<<"Enter your seat number: ";
		cin>>*seat;
		cout<<"Do you want to input again?(Y/N): ";
		cin>>ask;
	}
	while(ask=='y'||ask=='Y');
	cout<<"\nDo you want to see the passenger's name?(Y/N): ";
	cin>>ask;
	if(ask=='y'||ask=='Y')
	{
		cout<<"\nThe Program will now direct you to the displaying of passenger's name...."<<endl;
		display();
	}
	else
	{
		cout<<"\n\nThe Program will end shortly....";
	}
	getch();
	return 0;
}

void display()
{
	do
	{
		cout<<"\nEnter seat number to display passenger's name: ";
		cin>>*seat;
		cout<<pname<<*name[*seat-1]<<endl;
		cout<<"Do you want to try again?(Y/N): ";
		cin>>ask;
	}
	while(ask=='y'||ask=='Y');
	cout<<"\nThe Program will now end shortly....";
	getch();
}
Posted
Updated 22-Feb-14 5:58am
v2

1 solution

If I understood correctly your requirements, you need an array of strings as well an array of seat numbers (or better an array of structs, with passenger name and seat number as members). Your program hasn't those. It looks you lack of a basic understanding on C++ (and C) array and string handling (e.g. you really don't need pointers).
You could write:
C++
#include <iostream>
#include <vector>

using namespace std;

struct Passenger
{
  string name;
  int seat;
};

int main()
{
  vector <Passenger> passenger;
  char answer;
  do
  {

    Passenger p;
    cout << endl << "enter passenger name ";
    cin >> p.name;
    cout << endl << "enter passenger seat number ";
    cin >> p.seat;
    passenger.push_back(p);
    cout << endl << "do you wish to continue ? ";
    cin >> answer;
  } while ( answer == 'y');

  int n, seat;

  cout << endl << "enter the seat number ";
  cin >> seat;

  for (n=0; n<passenger.size(); ++n)
  {
    if ( seat == passenger[n].seat )
      break;
  }

  if ( n != passenger.size() )
    cout << endl << "found passenger " << passenger[n].name;
  else
    cout << endl << "sorry, no match";
  cout << endl;
}
 
Share this answer
 
Comments
Usman Hunjra 24-Feb-14 12:01pm    
Gr8 .. +5 .. :)
CPallini 24-Feb-14 13:06pm    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900