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

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




Arrays to store names in C++

 
Reply to this topicStart new topic

Arrays to store names in C++, Arrays in C++

Maxood
16 May, 2008 - 09:46 AM
Post #1

New D.I.C Head
*

Joined: 15 Mar, 2008
Posts: 4

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

gabehabe
RE: Arrays To Store Names In C++
16 May, 2008 - 10:07 AM
Post #2

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,539



Thanked: 98 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
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:
cpp
for (int i = 0; i < 10; i++)
cin >> myArray[i];

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

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


Hope this helps smile.gif
User is offlineProfile CardPM
+Quote Post

Maxood
RE: Arrays To Store Names In C++
16 May, 2008 - 10:36 AM
Post #3

New D.I.C Head
*

Joined: 15 Mar, 2008
Posts: 4

QUOTE(gabehabe @ 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:
cpp
for (int i = 0; i < 10; i++)
cin >> myArray[i];

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

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


Hope this helps smile.gif



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

gabehabe
RE: Arrays To Store Names In C++
16 May, 2008 - 10:54 AM
Post #4

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,539



Thanked: 98 times
Dream Kudos: 2650
Expert In: ruling the world.

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

Maxood
RE: Arrays To Store Names In C++
17 May, 2008 - 09:16 AM
Post #5

New D.I.C Head
*

Joined: 15 Mar, 2008
Posts: 4

QUOTE(gabehabe @ 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 happy.gif


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

gabehabe
RE: Arrays To Store Names In C++
18 May, 2008 - 06:56 AM
Post #6

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,539



Thanked: 98 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
Wikipedia can help smile.gif
Bubble Sort
Quick Sort
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 11:18PM

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