Sign in to follow this  
BlakeJustBlake

Need help with some code

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[i])
{b[0]=i+1;}
}
cout<<b[0]<<endl;
cin.get();
return 0;   
}

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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' = 32

const 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[i];

// >>>>>>>>>> cast to int <<<<<<<<<<
int char_number = int(the_string[i])-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 this post


Link to post
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this