Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
This one was made entirely by me, but(SUPRISE!!) it has a problem. The idea is it to locate letters whitin a word and after replace the found letters by number, producing some sort of primitive code for example A = 1.
Here's the code:
#include<iostream>
#include<string>using namespace std;
int main(){
enum fields{ABC_C, ABC_V, NUMBER, FIELDS_NR};
const int ABC_NR = 24;
string abc[ABC_NR][FIELDS_NR] =
{
{"A", "a", "1"},
{"B", "b", "2"},
{"C", "c", "3"},
{"D", "d", "4"},
{"E", "e", "5"},
{"F", "f", "6"},
{"G", "g", "7"},
{"H", "h", "8"},
{"I", "i", "9"},
{"J", "j", "10"},
{"K", "k", "11"},
{"L", "l", "12"},
{"M", "m", "13"},
{"N", "n", "14"},
{"O", "o", "15"},
{"P", "p", "16"},
{"Q", "q", "17"},
{"R", "r", "18"},
{"S", "s", "19"},
{"T", "t", "20"},
{"U", "u", "21"},
{"V", "v", "22"},
{"X", "x", "23"},
{"Z", "z", "24"}
};
cout << "Demonstrating Array: \n";
for(int i = 0; i<ABC_NR; ++i){
for(int j = 0; j<FIELDS_NR; ++j){
cout << abc[i][j];
}
cout << endl;
}
cout << endl;
string word = "Hello";
cout << "The test word is " << word << endl;
cout << "Testing Consonants\n";
for(int i = 0; i<(ABC_NR*2); ++i){
string tempC = abc[i][ABC_C];
int consonant_nr = word.find(tempC);
if(consonant_nr != string::npos){
char letterC = word[consonant_nr];
cout << "Found the consonant " << letterC << " in the word\n";
}else{
cout << abc[i][ABC_C] << " wasn't found on the word\n";
}//end if
}//end for
cout << "Testing Vogals: \n";
for(int i = 0; i<(ABC_NR*2); ++i){
string tempV = abc[i][ABC_V];
int vogal_nr = word.find(tempV);
if(vogal_nr != string::npos){
char letterV = word[vogal_nr];
cout << "Found the vogal(s) " << letterV << " in the word\n";
}else{
cout << abc[i][ABC_V] << " wasn't found on the word\n";
}//end if
}//end for
system("Pause");
}//end main()The problem is that system("Pause") doesn't work and the app shuts down imidiatly, why?
Am I giving any command that acts as the Enter Key being pressed? Or is it a more complex error?
Best Regards
AMD ATHLON X2 5200 2.6ghz;
ASUS M2N-E SLI;
2GB DDR800 KINGSTON;
ASUS GF8600GTS;
Seagate 7200rpm 320GB;

The problem is that system("Pause") doesn't work and the app shuts down imidiatly, why?
Am I giving any command that acts as the Enter Key being pressed? Or is it a more complex error?
You're over thinking it. The problem is a segfault; your program isn't even making it to the system("Pause");.Why? Because of this line (there's two of 'em):
for(int i = 0; i<(ABC_NR*2); ++i){

Thanks for your answer but...
...isn't *2 multiplying by 2?
EDIT: As soon as I clicked the confirm post message i understood, i cant change a const and im trying to, right?
AMD ATHLON X2 5200 2.6ghz;
ASUS M2N-E SLI;
2GB DDR800 KINGSTON;
ASUS GF8600GTS;
Seagate 7200rpm 320GB;

No, the array is 24 elements long. You're trying to iterate through it 24*2 times. As soon as you hit the 25th element, it bombs w/ a segfault.

What Razor said is right. By the way, you're not testing consonants and "vogals" (vocals? vowels? bagels?); you seem to be testing capital letters and small letters (uppercase and lowercase in the U.S.) Are you Greek by any chance?

I'm Portuguese, we use the terms "Maiusculas" and "Minusculas", and yes, US talking, i mean to test the uppercase and the lowercase letters.(lol...what a silly error, it aint the vowels or the consonants i want to test its uppercase and lowercase...LOL)
I still haven't finished it, it still needs the Actual "word to number conversion" part of the code, i dont know if this is the best way to do it, can you give me some kind of hints?
Best Regards
AMD ATHLON X2 5200 2.6ghz;
ASUS M2N-E SLI;
2GB DDR800 KINGSTON;
ASUS GF8600GTS;
Seagate 7200rpm 320GB;

How would I change words into numbers? I'd probably use the letter's value, and stringstream to do the formatting:
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
inline bool notSymbol(unsigned char c) {
const unsigned char mask = ~('A' ^ 'a');
return (c >= '0' && c <= '9') || ((c & mask) >= 'A' && (c & mask) <= 'Z');
}
int main(int argc, char *argv[]) {
std::string s = "Hello World! 0123 xyz";
std::string::iterator si;
std::ostringstream out;
out.fill('0');
out.setf(std::stringstream::hex, std::stringstream::basefield);
out.setf(std::stringstream::uppercase);
for (si = s.begin(); si < s.end(); ++si) {
std::cout << *si << " = " << (char)*si - '0' << '\n';
if (notSymbol(*si)) out << std::setw(2) << (char)*si - '0';
else out << *si;
}
std::cout << out.str() << std::endl;
system("PAUSE");
return EXIT_SUCCESS;
}

Gulp...Isnt there a longer but simpler way that i can actualy understand? :S
PS: How did you learn programing Razor2.3?
AMD ATHLON X2 5200 2.6ghz;
ASUS M2N-E SLI;
2GB DDR800 KINGSTON;
ASUS GF8600GTS;
Seagate 7200rpm 320GB;

Longer but simpler? Probably, but I can't think of it.
I guess I could have used for (int i = 0; i < s.size(); ++i) instead of the string iterator, but the iterator is so convenient (once I remember how to spell 'iterator').
I didn't use using namespace std;, which is why almost everything's prefixed by std::.
I guess I could have not used stringstream (roughly 1/3 of the program), but aside from the out.str() function, everything can be used with cout (and mostly cin), so it's nice to know.
The trickiest part of the code is the bool notSymbol(unsigned char) function. Specifically, how the mask works. To understand how it works, you need to know two things: (1) In ASCII, there's only one bit difference between upper and lower case; (2) This bit, when set to '0', is uppercase. Outside of that, you just need to know the binary operators XOR (^), inversion (~), and, well, AND (&); the logical operators AND (&&) and OR (||); as well as the comparison operators >= and <=.
How did you learn programing?
I took a course or two in college, but mostly I've learned by myself, partly by trial and error, partly by visiting my local library.- The C++ FAQ LITE is a good read, and includes book recommendations.
- Join a newsgroup, possibly comp.lang.c++.
- Lurk on C++ forums. Not this one, this forum is too general to be good as a C++ reference. As an added bonus, you can stop lurking long enough to ask questions! Just make sure you START by lurking. Otherwise, you won't know their mannerisms. That goes for the newsgroup option, too. Remember: Proper netiquette starts with lurking.EDIT: Also, I recommend Visual Studio Express (I link it because MS' site becomes more atrocious with every iteration). It's one of the best IDE's I've seen, and it includes one of the best debuggers I've seen. Added bonus: MSDN documentation.

I'm sorry i took so much time to repply to your post, i'l do everything you said and thanks for the nice & short code explication!!
Best Regards
AMD ATHLON X2 5200 2.6ghz;
ASUS M2N-E SLI;
2GB DDR800 KINGSTON;
ASUS GF8600GTS;
Seagate 7200rpm 320GB;

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |