7 Replies - 23063 Views - Last Post: 11 May 2010 - 05:45 PM Rate Topic: -----

#1 Maxood  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 15-March 08

Arrays to store names in C++

Post icon  Posted 16 May 2008 - 10:46 AM

I am learning to program in C++. i would like to know how can i store names in an array. Lets say i have 10 names that i have to store. What array i am going to declare and how can i access them individually.
Please explain me with examples.Thanks
Is This A Good Question/Topic? 0
  • +

Replies To: Arrays to store names in C++

#2 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1385
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Arrays to store names in C++

Posted 16 May 2008 - 11:07 AM

In C++ you can create an array of strings:
string myArray[10];
(Note that value 10 is a NULL character, and the first value is 0)

As for declaring it, you could use a for loop to count through and get the user to enter them all:
for (int i = 0; i < 10; i++)
    cin >> myArray[i];

Or, you could define them seperately:
myArray[0] = "danny";
myArray[1] = "leonardo";

Or, you could define them all on the same line:
string myArray[10] = {"danny", "leonardo", etc...);


Hope this helps :)
Was This Post Helpful? 0
  • +
  • -

#3 Maxood  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 15-March 08

Re: Arrays to store names in C++

Posted 16 May 2008 - 11:36 AM

View Postgabehabe, on 16 May, 2008 - 11:07 AM, said:

In C++ you can create an array of strings:
string myArray[10];
(Note that value 10 is a NULL character, and the first value is 0)

As for declaring it, you could use a for loop to count through and get the user to enter them all:
for (int i = 0; i < 10; i++)
    cin >> myArray[i];

Or, you could define them seperately:
myArray[0] = "danny";
myArray[1] = "leonardo";

Or, you could define them all on the same line:
string myArray[10] = {"danny", "leonardo", etc...);


Hope this helps :)



The Dev C++ compiler says C:\Documents and Settings\maqsoodrehman\My Documents\arr.cpp `string' undeclared (first use this function) .
Can you help me with this. What i want to do is to store 10 names from the console i.e. want to prompt the user for 10 names and store them in a for loop.
Please help.
Was This Post Helpful? 0
  • +
  • -

#4 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1385
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Arrays to store names in C++

Posted 16 May 2008 - 11:54 AM

OK, it's compiler specific stuff: some will include string as a standard, others you have to add #include <string> to the top of your code. Try it out and see if it works now ^_^
Was This Post Helpful? 0
  • +
  • -

#5 Maxood  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 15-March 08

Re: Arrays to store names in C++

Posted 17 May 2008 - 10:16 AM

View Postgabehabe, on 16 May, 2008 - 11:54 AM, said:

OK, it's compiler specific stuff: some will include string as a standard, others you have to add #include <string> to the top of your code. Try it out and see if it works now ^_^


Now my requirement is to sort 10 names from their with respect to their first character using bubble sort or any other sorting algorithm. I have my code here:
#include <iostream.h>
#include <conio.h>
#include <string.h>



main()
{
char name[10][25];


cout<<"Enter the Names:"<<endl;


//prompt user to input 10 names
for (int i = 0; i<=9; i++)
{
   cout<<"Name"<<i+1<<": ";
   cin>>name[i];        
}



//display 10 names
for (int j=0;j<=9;j++)
{
    cout<<name[j]<<endl;    
}

getch();
}


How can i sort the names stored in name[i] with respect to their first charater.[size=3][size=3]
Please help
Was This Post Helpful? 1
  • +
  • -

#6 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1385
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Arrays to store names in C++

Posted 18 May 2008 - 07:56 AM

Wikipedia can help :)
Bubble Sort
Quick Sort
Was This Post Helpful? 0
  • +
  • -

#7 Guest_ertem*


Reputation:

Re: Arrays to store names in C++

Posted 11 May 2010 - 04:27 PM

hello,i have just started to learn c++

i have a question about Maxood s code.

char name[10][25];

Why did he use 2-d array here?
is it impossible that store and print names with one-dimensional array?
Was This Post Helpful? 0

#8 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Arrays to store names in C++

Posted 11 May 2010 - 05:45 PM

you can just use a std::string array rather than a char[][] array, start your own thread if you have questions :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1