4 Replies - 9591 Views - Last Post: 29 December 2007 - 12:47 PM Rate Topic: -----

#1 Macroniac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-December 07

3 dimensional string array in C++

Posted 27 December 2007 - 01:30 AM

Hi :) ,
I wished to make a code in C++ that uses arrays of strings and works like a Login Authenticator.
In the process, I decided to get the multidimensional (3 or more dim.) string array idea cleared up. It accepted data and displayed prety well for the string :
 char unpw [10][2][8] 

So all I am trying to do is minimize it to a single username + password array, but can't get past the following piece, please help :-
#include <stdio.h>
#include <conio.h>
void main()
{
/*char unpw [10][2][8];
int i;
printf("Please enter the usernames and respective passwords  (total=5)\n");
for (i=1;i<=5;i++)
{
scanf("%s",&unpw[i][0][i]);
scanf("%s\n",&unpw[i][1][i]);
}
i=1;
for (i=1;i<=5;i++)
{
printf("%s\n",&unpw[i][0][i]);
printf("%s\n \n",&unpw[i][1][i]);
}  */	  // Works pretty well till here
int i=1;
char eunpw[10][2][1];
 printf("Please enter the username and password to verify your account \n");
 scanf("%s %s",eunpw[1][1][1],eunpw[1][2][1]);

 printf("%s\n",eunpw[1][1][1]); //trying to verify it	:-(
 printf("%s\n \n",eunpw[1][2][1]);

getch();
}



Is This A Good Question/Topic? 0
  • +

Replies To: 3 dimensional string array in C++

#2 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 41
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: 3 dimensional string array in C++

Posted 27 December 2007 - 03:05 AM

The first (minor) problem is that you have formatted the dimensions of your arrays strangely - with the way multidimensional arrays in C are usually interpreted, the last two dimensions are considered to be the rows and columns in the array. So for a 3D array, it would be
char myArray[n_users][n_rows][n_cols];

In your code, you appear to have transposed these, resulting in an array with 10 layers, each containing a 2x1 array. However, in the first block of code (the commented out section), the dimensional formats are correct for 10 users, each with a username and pw that can be a maximum of 8 characters long, so this may have been just some confusion when you tried to shorten the code.

You should also note that C arrays are zero-based, so the first element of your array is eunpw[0][0][0], not eunpw[1][1][1]. So, if the size of your array in some dimension is N, then the maximum index you can use for assigning/accessing is N-1.

The bigger problem (i.e. the one that is causing the program to crash or otherwise whack out) is that you're trying to store an entire string into a single character - something that it won't like to do. When using scanf with strings, you assign to a location - a pointer to a block of memory. So instead of specifying the user number, row, and column, you would only specify the user number and row indices (the row determines whether you're storing the username or password - 0 for un, 1 for pw).

The following code is a cleaned up version of yours, that should work properly:
#include <stdio.h>

int main() {
	char unpw [10][2][8];//storage for 10 users, username and pw both maximum of 8 characters long
	int user_number;
	printf("Please enter the usernames and respective passwords  (total=5)\n");
	for (user_number=0; user_number<5; user_number++) {
		scanf("%s", &unpw[user_number][0]);//store username in layer user_name, row 0
		scanf("%s\n", &unpw[user_number][1]);//store username in layer user_name, row 1
	}
	
	for (user_number=1; user_number<5; user_number++) {
		printf("%s\n", &unpw[user_number][0]);
		printf("%s\n \n", &unpw[user_number][1]);
	}
	
	return 0;
}


And as an aside, for your code to be up to standard, main() must specify and int return type (and thus must return an int as well), and shouldn't include non-standard headers or functions like conio.h or getch(). It's not critical, but it makes a difference for portability, and some members might go ballistic when they see stuff like that ;)

Hope that helps,

-jjh

This post has been edited by jjhaag: 27 December 2007 - 03:07 AM

Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4892
  • View blog
  • Posts: 11,288
  • Joined: 16-October 07

Re: 3 dimensional string array in C++

Posted 27 December 2007 - 05:38 AM

You might want to try using a struct here:
typedef struct UserInfo {
   char userName[8];
   char password[8];
} UserInfo;

void main() {
   UserInfo validUserList[10];



Your brain will thank you. ;)
Was This Post Helpful? 0
  • +
  • -

#4 mattman059  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 15
  • View blog
  • Posts: 538
  • Joined: 23-October 06

Re: 3 dimensional string array in C++

Posted 27 December 2007 - 10:22 AM

a struct would make an insane amount of difference in the readability and understandability of your code.....ability
Was This Post Helpful? 0
  • +
  • -

#5 Macroniac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-December 07

Re: 3 dimensional string array in C++

Posted 29 December 2007 - 12:47 PM

Thank You jjhaag, it helped a lot. I got the working of string arrays totaly. :^: Thanks a lot.

Thanks baavgai, using a struct is a cool idea too. :)

mattman059 :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1