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
Arrays to store names in C++Arrays in C++
Page 1 of 1
7 Replies - 9551 Views - Last Post: 11 May 2010 - 05:45 PM
Replies To: Arrays to store names in C++
#2
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:
Or, you could define them seperately:
Or, you could define them all on the same line:
Hope this helps
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
#3
Re: Arrays to store names in C++
Posted 16 May 2008 - 11:36 AM
gabehabe, 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:
Or, you could define them seperately:
Or, you could define them all on the same line:
Hope this helps
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.
#4
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
#5
Re: Arrays to store names in C++
Posted 17 May 2008 - 10:16 AM
gabehabe, 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
#6
Re: Arrays to store names in C++
Posted 18 May 2008 - 07:56 AM
#7 Guest_ertem*
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?
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?
#8
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
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote








|