C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 17th, 2005, 05:45 PM
RTAdams89 RTAdams89 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 1 RTAdams89 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 m 7 sec
Reputation Power: 0
Cool Help with finding possition of letters with in a word

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.

Reply With Quote
  #2  
Old March 24th, 2005, 08:02 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
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.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Help with finding letters with in a word


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 | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway