C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old April 22nd, 2003, 10:43 AM
wizard2468 wizard2468 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 3 wizard2468 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
two dimension array

I am tring to set up a 2x10 two dimensional array that holds two seperate strings (char firstName and char lastName) for the two columns and i want to store up to 10 people (rows). I want the user to enter in the names and be able to display the names after they are typed in. I am unsure how to set up the char array and I am unsure how to have the user enter the names. Any information that will help me code this will be thankful

Reply With Quote
  #2  
Old April 22nd, 2003, 12:29 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 h 55 m 33 sec
Reputation Power: 797

Reply With Quote
  #3  
Old April 22nd, 2003, 02:14 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,803 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 11 h 56 m 57 sec
Reputation Power: 437
I would think:
char *names[10][2]; // will need to dynamically allocate memory space for each name
or
char *names[10][2][15]; // static allocation, assuming no name over 14 letters

In effect, you'll have a 3-dimensional array, when you figure that each element of your 2D array will itself be a 1D array.

How the user will input the names depends in part on whether you're using C or C++. In C++, it seems popular now to use iostreams:
cin >> firstName >> last Name;

Then strcpy those strings into the appropriate elements of the array. Of course, if you are using the dynamic allocation approach, be sure to allocate one character more than the string length for the null terminator.

Reply With Quote
  #4  
Old April 23rd, 2003, 11:37 AM
wizard2468 wizard2468 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 3 wizard2468 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I have having trouble coding the information that dwise1_aol give me. What would the coding of the main module look like

Reply With Quote
  #5  
Old April 23rd, 2003, 01:19 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
It sounds like you're trying to get someone to do your homework for you.

Reply With Quote
  #6  
Old April 23rd, 2003, 01:26 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,803 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 11 h 56 m 57 sec
Reputation Power: 437
Quote:
Originally posted by wizard2468
I have having trouble coding the information that dwise1_aol give me. What would the coding of the main module look like


Show us what you're having trouble with, then we can tutor you.

Reply With Quote
  #7  
Old April 24th, 2003, 11:06 AM
wizard2468 wizard2468 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 3 wizard2468 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
For the record this is not homework. I am learning C++ on my own with the books you can buy at any bookstore. The code that I have been working with is not right but here it is

#include <iostream.h>


int main()
{
char name [10][20];
int x;
for (x=0; x<10; ++x)
{
cout << "\nEnter the name: ";
cin.getline(name,20)
}
cout << name;

return 0;
}

but the cin.getline(name,20) developes an error so i changed it to

#include <iostream.h>

int main()
{
char name [10][20];
int x;
for (x=0; x<10; ++x)
{
cout << "\nEnter a name: ";
cin >> name
)
cout << name;

return 0;
}

With this code it allowed me to enter 10 names but it wou't display them

Reply With Quote
  #8  
Old April 24th, 2003, 02:04 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,803 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 11 h 56 m 57 sec
Reputation Power: 437
Quote:
Originally posted by wizard2468
For the record this is not homework. I am learning C++ on my own with the books you can buy at any bookstore.


Pardon our assumptions. It's just that we tend to get a lot of students looking for help with their homework. I don't mind tutoring, but I'm reluctant to just give an answer. If it just gets done for them, then they won't learn.

Quote:
Originally posted by wizard2468
The code that I have been working with is not right but here it is

#include <iostream.h>


int main()
{
char name [10][20];
int x;
for (x=0; x<10; ++x)
{
cout << "\nEnter the name: ";
cin.getline(name,20)
}
cout << name;

return 0;
}

but the cin.getline(name,20) developes an error so i changed it to

#include <iostream.h>

int main()
{
char name [10][20];
int x;
for (x=0; x<10; ++x)
{
cout << "\nEnter a name: ";
cin >> name
)
cout << name;

return 0;
}

With this code it allowed me to enter 10 names but it wou't display them

There was a discussion here, about a month ago, about a problem with getline(). IIRC, it has to do with how it handles (or doesn't quite handle) the newline.

Now, since name is a two-D array and a string would only go into a 1-D array, you should do that:
cin >> name[i];
And the same when you output it. You would probably either want to echo it out immediately or write a second for-loop to output the contents of the array. Preferably the latter, I would think.

Reply With Quote
  #9  
Old April 25th, 2003, 01:28 AM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
char text [10][20];

text is the name of the whole conglomeration of parts that make up the array, so when you say:

cin>>text;

or

cout<<text;

you're not correctly accessing the storage containers that make up the array. The two dimensional char array you declared looks like this when filled with data:

row1: aaaaaaaaaaaaaaaaaaa\0 (19 char's and a terminating \0)
....
....
....
row10:aaaaaaaaaaaaaaaaaaa\0

So, what you want to do is input data into each row of the array. To do that, you must specify which row you want to read data into, e.g.

cin>>text[0];
cin>>text[1];
...
...
...
...
cin>>text[9];

Typically, you would do that in a loop like this:

for(int i=0; i<10; i++)
{
cin>>text[i];
}

Reply With Quote
  #10  
Old April 25th, 2003, 09:31 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,803 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 11 h 56 m 57 sec
Reputation Power: 437
One further note. Both the << and >> operators are overloaded, which means that they will run different code depending on the data-type/class being output or input. That is why they handle char* differently than int or double.

Your code as it stands is telling them to handle a char** . I personally have difficulty believing that the iostream library writer would have written a method to handle a char**, but apparently they did because it does compile (doesn't it?). Still, I have no idea how it would actually be trying to handle it. Unless the character data being input was being interpreted as a pointer [shudder!].

Reply With Quote
  #11  
Old April 28th, 2003, 10:43 AM
ClayDowling ClayDowling is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Flint, MI
Posts: 328 ClayDowling User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 19 m 25 sec
Reputation Power: 6
the stream input operator won't play particularly nicely with char*. You really want to input to strings. So you might use something like this:
Code:
string name[10];

for(int i=0; i < 10; i++) {
  cout << "Name: ";
  cin >> name[i];
}

for(int j=0; j < 10; j++)
  cout << name[j] << endl;
__________________
Clay Dowling
Lazarus Notes
Articles and commentary on web development
http://www.lazarusid.com/notes/

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > two dimension array


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |