| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hello all. I' work on a program and part of it requires C++ knowledge wich I do not yet have. Hopefulyl someone can help me.
Here is the current program code: #include <cstdio> #include <cstdlib> #include <iostream> #include <fstream> using namespace std; int main() { //Gets username and password char s[50]; char t[50]; cout << "Enter name: "; cin >> s; cout << "Enter password: "; cin >> t; //Defines variables char address[50]; char alphanum[72]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn opqrstuvwxyz"; float len; float i = 1; int code; //Starts function and checks to make sure "s" and "t" have values if (strlen(t)>0 && strlen(s)>0) { //sets "len" to longer of "s" and "t" if (strlen(s) >= strlen(t)) { len = strlen(s); } else { len = strlen(t); } //End of sets "len" to longer of "s" and "t" //Caps "len" at 24 if (len > 24) { len = 24; } else { } //Ends Caps "len" at 24 //Loop to output file name while ( i <= len) { int n = 0; int p = 0; //if ( i<s.length ) n = alphanum.indexOf(s.substring( i,i+1 )) //if ( i<t.length ) p = alphanum.indexOf(t.substring( i,i+1 )) code = ((2*n+p)^13) % 61; //if ( code>0 ) address += alphanum.substring( code,code+1 ) i++; } } //Returns error if "s" or "t" has no value else { cout << "Invalid name/password input!"; } system("PAUSE"); return 0; } In there I bolded(is that a word) the code I need help with. The code wiht comment markers befor it is code I got from a JavaScript program tha does what I want. Now I jsut need a way to convert it to C++. The idea is that it looks at "s", finds the "i"th letter in "s", finds that same letter in "alphanum" then stores it's position in "alphanum" as "n". It then does the same for "t" storing the resulting number as "p". Then it runs "n" and "p" through a mathmatical operation and saves the result as "code". It then outputs the "i"th letter in "alphanum" and appends it to the string "address". 1 is added to "i" and the loop runs again until "i" is the same as "len". How would i do this in C++? PS: I already tried usign the "at()" command however it only works with strings where my variables ("s" and "t") are all char. |
|
#2
|
|||
|
|||
|
i believe the official technical word is "boldify" :-P
use code tags!!! anyways, here is the bold part of your code, rewritten and all c++ified ( made up another word :-D) Code:
//here is my indexOf function. it takes two arguments: a string in the form of a char array, and a character to search for. it returns the index number of the first occurrence of that number in the string. (this is what string.indexOf does in JavaScript)
int indexOf(char searchin[], char searchfor)
{
int index = 0; //will hold the index
for (; index <= strlen(searchin) index++) //loop until end of string or searchfor is reached
{
if (searchin[index] == searchfor) break;
}
return index;
}
//Loop to output file name
for (int i = 0; i <= len; i++) //use a for loop, and remember you have to declare your variable i before you can use it
{
int n = 0;
int p = 0;
if ( i<strlen(s) ) n = indexOf(alphanum, s[i]) //use strlen() for string lengths just like you did in the other part of your code, and see my indexOf function above
if ( i<strlen(t) ) p = indexOf(alphanum, t[i])
code = ((2*n+p)^13) % 61;
if ( code>0 ) strcat(address, alphanum[code]); //use strcat to append strings--there must be enough space in address
//don't need i++ because the for loop does it for you
}
also, make sure you initialize address to something like "" if you plan to use it later. you can get around the problem of not having enough space in address, s, or t by using strings (google string.h) instead of char[]s. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Help with finding letters with in a word |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|