Public Group

# Need help with some code

This topic is 4304 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've been trying to work on this program that changes a 5 letter character string into a number by taking the corresponding number of the letter, a is 1, b is 2 etc, and then multiplying those numbers together. So, for example, if you were to enter asdfg a would be 1, s would be 19, d would be 4, f would be 6, g would be 7 and multiplied together they would equal 3192. Then you would enter another 5 character string and it'd do the same thing and if they were equal it would say something and if they didn't it would say something else. But I was just working on the first part of it, the part that analyzes the first character and matches it to a number, and I was testing it out, and it works fine and dandy except it makes 'a' 865 when it is in a string longer than 3 characters, and it makes 'b' 871 when it's in a string of 5 characters. All of the other letters work out perfectly fine. I've looked at this code over and over to see if anything is wrong but I can't think of anything. Can anyone help?
#include <iostream>

using namespace std;

int main(){
char a[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char c[4];
int b[4];
int i;
cin>>c;
cin.get();
for (b[0]=0;b[0]==0;i++)
{
if (c[0]==a)
{b[0]=i+1;}
}
cout<<b[0]<<endl;
cin.get();
return 0;
}


##### Share on other sites
Are you looking to match characters exactly? If so, that logic doesn't work.

You're saying you're inputing strings of five characters but you're only allowing for four characters. Although the indexing of the elements is zero based you still have to declare space for five characters.

char b[5];

Second, check that the characters that you're showing in your array are the ones you entered at the keyboard. Once you've verified that you're getting exactly what you enter put into the array, you can delete the part that verifies that fact and move on to the rest of the logic.

Lilith

##### Share on other sites
There are various mistakes in the program. First of all, to hold the string you need 5 chars, plus one more that stores the 'null' value that signifies the end of the string. So, you need char c[6], not char c[4](why '4' anyway? Even if you forgot about the 'null' char, you said you need 5 letters). Consider using std::string to hold strings so you don't have to deal with such details. Second, you don't initialize 'i'. It's a mistake to assume it will automatically initialize to 0.

Also, this is not a mistake, just a hint: chars hold the ASCII code of the letter.You can just subtract a char from 'a' to get the value you want, assuming it's lowercase. For example, 'a'-'a'==0, 'b'-'a'==1,'c'-'a'==2 and so on. Although I don't really understand what all of this is about. Is it just to compare strings? You can use strcmp() for C-strings or even better std::string supports the '==' operator.

##### Share on other sites
Thanks guys, the problem was the array values. I am just starting out c++ and I've been doing these little challenges so I can get these kind of little mistakes down like forgetting to leave an area for the null value. I changed the array to 5 and it works perfectly.

edit:By the way, is there any way in c++ like php to set it so that you can get an array to continue in alphabetic order after setting the first couple so I don't have to type out all of the letters?

##### Share on other sites
This should be pretty much self-explanatory. You basically just use the conversion int(char) and you get a number (the ASCII-Code).

#include <iostream>#include <string>using namespace std;// ASCII Code explanation// A 65// B 66// Z 90//...// a 97// b 98//...// z 122// 'a' - 'A' = 32const int ASCII_OFFSET = 64;const int BIGCASE_OFFSET = 32;int main(){    // our string    string the_string;    // get the string    cin>>the_string;    // the final number    long number = 1;    // loop through whole string    for (int i = 0; i < the_string.length(); i++) {        // adjust to lower case so that the ASCII char is well defined        // get the i-th character        char the_char = the_string;        // >>>>>>>>>> cast to int <<<<<<<<<<        int char_number = int(the_string)-ASCII_OFFSET;        // the following makes sure we get the char_number 1 for 'a' 'A'        if (char_number > 32)            char_number -= BIGCASE_OFFSET;        // print the char        cout<< the_char << " : " << char_number << endl;        // multiply the number (for whatever reason)        number *= char_number;    }    cout<<"\nthe number: " << number << endl;    return 0;}

##### Share on other sites
Quote:
 edit:By the way, is there any way in c++ like php to set it so that you can get an array to continue in alphabetic order after setting the first couple so I don't have to type out all of the letters?

char myArray[26]for(char c = 'a'; c <= 'z'; c++){    myArray[c-'a'] = c;}

##### Share on other sites
For things like you want to do its best to use the string functionality (or regular expressions).

• 37
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631360
• Total Posts
2999556
×