Problem with C++ arrays... HELP!! :(

Problem with C++ arrays... HELP!! :(

Post by code2004 on Tue Jul 28, 2009 8:53 am
([msg=27578]see Problem with C++ arrays... HELP!! :([/msg])

WARNING: THIS POST MAY CONTAIN SPOILERS FOR PROGRAMMING 12... MAYBE... NOT SURE YET - AIN'T WRITTEN IT YET...

Okay, so, I've already done Programming 12, but seeing as I'd decided to learn more programming languages, I chose to transcribe the main ideas from PHP to C++. Unfortunately, there is no proper string -> array splitter on C++ that I can find! So I decided to do this...:

Code: Select all
//VARIABLES
x = 0;
char passArray(sizeof[PASSWORD]);

/*Chops up password into array*/
while (x>sizeof(PASSWORD))
{
     passArray[x]=PASSWORD.substr(x,1);    //ERROR IN SYNTAX IS HERE
     x = x++;
}


Unfortunately... There's something wrong with the 'passArray[x]=PASSWORD.substr(x,1)'. To me, it looks fine: it tells the program to add the first letter of the string PASSWORD to the array passArray. The only thing that I know may look wrong is the substr(x,1)... That's to say, I'm not sure if 0,1 would select the first letter, or whether I'd need to put x+1,1 (or more properly, 1,1).

Now, I'm using the Bloodshed Dev, and the syntax error report I receive from that is thus:

Code: Select all
33 | C:\Documents and Settings\Connor\My Documents\Cryptography program.cpp | cannot convert `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to `char' in assignment


And I'd be lying if I said I understood a word of ANYTHING after 'convert'... If you can think of anything, I'd be very grateful if you could either PM or respond here...

THANKS A LOT :)!!!

Code2004 // Connor
Image
.............................H..o.....i..l.....C..u..o..r..e.....S..p..e..z..z..a..t..o.............................
User avatar
code2004
New User
New User
 
Posts: 36
Joined: Sat Jun 13, 2009 5:23 am
Blog: View Blog (0)


Re: Problem with C++ arrays... HELP!! :(

Post by BhaaL on Tue Jul 28, 2009 12:24 pm
([msg=27594]see Re: Problem with C++ arrays... HELP!! :([/msg])

You might want to check the parameters substr takes, and what it returns.
substr returns a string, so you'd have to get the first character of it (even tho it is only 1 char, as string it takes up 2 bytes).
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: Problem with C++ arrays... HELP!! :(

Post by code2004 on Tue Jul 28, 2009 12:27 pm
([msg=27595]see Re: Problem with C++ arrays... HELP!! :([/msg])

CONT'D

I believe the problem may be in the deftype, i.e. the substr function doesn't definitely mean that the result will be a char type, so perhaps that's why it's faulting... I'm going to try splitting the letters into two arrays, int and string. Any thoughts, please respond.

EDIT: Yeah, what he said ^^^^^^^
Image
.............................H..o.....i..l.....C..u..o..r..e.....S..p..e..z..z..a..t..o.............................
User avatar
code2004
New User
New User
 
Posts: 36
Joined: Sat Jun 13, 2009 5:23 am
Blog: View Blog (0)


Re: Problem with C++ arrays... HELP!! :(

Post by smithmetal on Tue Jul 28, 2009 3:18 pm
([msg=27601]see Re: Problem with C++ arrays... HELP!! :([/msg])

Code: Select all
//VARIABLES
x = 0;
char passArray(sizeof[PASSWORD]);

/*Chops up password into array*/
while (x>sizeof(PASSWORD))
{
     passArray[x]=PASSWORD.substr(x,1);    //ERROR IN SYNTAX IS HERE
     x = x++;
}


1. Variable x doesn't have a type
2. If you want a array of char its char passArray[]; (notice the brackets instead of the parenthesis)
3. The sizeof() fonction return the number of bytes (size_t) the type is. (e.g. char = 1, int = 4, double = 8,etc), what you want is the lenght() function. Such as char passArray[PASSWORD.lengh()]; (assuming that PASSWORD is a string object.)
4. Your code will never enter your while loop because x has no way to be greater than sizeof(PASSWORD)
5. "passArray[x]=PASSWORD.substr(x,1);".... passArray[x] is a char and PASSWORD.substr(x,1) will return a string. You can't put a string in a char..
6. "x = x++;"..... This is the same thing as saying x = x = x + 1 , which make no sense, just use "x++;" instead

Oh and btw if you initialised PASSWORD like this :

string PASSWORD = "mysecretpassword";

you can access the letters individualy by their index like this:

PASSWORD[0] or PASSWORD.at(0) will egual 'm'
PASSWORD[1] or PASSWORD.at(1) will egual 'y'
PASSWORD[2] or PASSWORD.at(2) will egual 's'
...

if you're at ease with vectors, here's a function you might like, its basically a PHP explode() in c++
http://www.zedwood.com/article/106/cpp-explode-function
Image
User avatar
smithmetal
New User
New User
 
Posts: 22
Joined: Sun Apr 13, 2008 9:09 pm
Blog: View Blog (0)


Re: Problem with C++ arrays... HELP!! :(

Post by code2004 on Fri Jul 31, 2009 4:20 pm
([msg=27725]see Re: Problem with C++ arrays... HELP!! :([/msg])

Oh my god, I can't believe I missed all them errors... They stick out like a sore thumb. Thanks for pointing it out to me... Feel really embarrassed now ^^'... Thanks for all your help !
Image
.............................H..o.....i..l.....C..u..o..r..e.....S..p..e..z..z..a..t..o.............................
User avatar
code2004
New User
New User
 
Posts: 36
Joined: Sat Jun 13, 2009 5:23 am
Blog: View Blog (0)


Re: Problem with C++ arrays... HELP!! :(

Post by myhexhax on Fri Jul 31, 2009 4:42 pm
([msg=27727]see Re: Problem with C++ arrays... HELP!! :([/msg])

That was a beautiful reply smithmetal.
gniripsni ewa si rehte eht morf cisum siht
myhexhax
Poster
Poster
 
Posts: 217
Joined: Tue Sep 16, 2008 2:19 pm
Location: Between the ether and the information superhighway
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests

cron