I have a problem. im trying to have it(it being this program ive writen), to when i press it, for example w for a piece of code to be preformed.

My code is as follows.

``````    int loopprocess = 0;
int score = 0;
int keycomand = getch();

while (loopprocess == 0)
{
if (keycomand == 0)
{
keycomand = getch()+256;

switch (keycomand)
{
case 57:
score++;
break;
}
}
cout << score << endl;
system("cls");
``````

i have no clue whats wrong i press w(57), but the score++; does not work. -_-
i learned this from this <daniweb> article [Click Here]
i have the aquired the ASCII code from Lookuptables.com Click Here
can you guys help me? please :)

## All 11 Replies

Capital W is 57h or 87d. Small w is 77h or 119d.

ok what is h after it? is there multiple ways to ASCII code?

The h after the number means that's it's in hexadecimal.

Hexadecimal numbers run from 0-9A-F. Each digit is multiplied by a power of 16. So 57h is 5 * 16^1 + 7 * 16^0.

You can also use the 0x prefix to denote hexadecimal. So 57h is the same as 0x57.

Hexadecimal can be useful in ASCII as numbers begin with 0x30 (48d) or '0' and go to 0x39 (57d) or '9'.

Also, the letters of the alphabet are offset by 0x40 (64d) for uppercase and 0x60 for lowercase. So, 'A' is 0x41, 'B' is 0x42, 'C' is 0x43, etc. up to 'Z' which is 0x5A.

Anyway, you should use the correct value in your Case: either 0x57 or 87, but not 57 which is the decimal ascii value of 9. That's only for capital W, so small w will not increment score.

Derp?

ok still makes no sense.
sorry im not catching on.

Try using 'W' and/or 'w' as in

``````     case 'W': case 'w' :
whatever
break;
``````

ok:

``````    int loopprocess = 0;
int score = 0;
int keycomand = getch();
if (keycomand == 0)
{
keycomand = getch()+256;

}

while (loopprocess == 0)
{
switch (keycomand)
{
case 'w':
score++;
cout << score << endl;
break;
}

cout << score << endl;
system("cls");

}
``````

this is all fine and dandi but when i press "w" it adds score and i cant stop it, and if i press some other key itll just stay at one.

help?

ASCII code fixed but now this new problem: it will not re check the the switch statment and go spirilling off.

(by the way ASCII is now working thumbs up for Dumbledor!)

Sit at a desk with a pencil and the code.
Write down the names of each variable in a column.
Go through the code line by line changing the variable values as you go.
Follow the loop until it exits.

This is the easiest way to figure out what's wrong with your code. You also learn more about programming.

i think ive got it, my switch statment does not have a default statment, and if not that then i will hold of on for this program and wait until i learn more code pertaining to this subject.
i will update you guys on my finding's for this program.

Do you want a string-based menu interface for console, or you just want to make something out of the ASCII and the `switch() {case:''}` methods?

I found that the bug in your program lies on the lines 24-25. The switch case, when 'w' is pressed it's ok, the score will just add up, but, after that, it will break and go on with the loop. After that it will again try to run the loop, having in the buffer the 'w' you pressed earlier, and there you go, the scores adds up again.

``````int main(){
int loopprocess = 0;
int score = 0;
int keycomand = getch();
if (keycomand == 0){
keycomand = getch()+256;
}
while (loopprocess == 0){
switch (keycomand){
case 'w':
score++;
//                cout << score << endl;
loopprocess=0;
break;
}

cout << score << endl;
break;

}
system("pause");
return (0);
}
``````

I've noticed you use system() functions. There are some threads on DaniWeb about that, you should check them out. Well, anyway, I've just put that system("pause") so that you can see what's happening.

But again, you can do these things by a string-based console interface:

``````#include <iostream>
#include <string>
#include <stdlib.h> //for exit(0)
using namespace std;

void foo(){
cout<<"foo\n";
}

void bar(){
cout<<"bar\n";
}

int main(){
string answer; //the variable in which the answer will be stored.
cout<<"What would it be?\n"
<<"1. foo()\n"
<<"2. bar()\n"
<<"Type 'q' to exit.\n"; //the menu, or you can simply make a show-menu function.
for (;;){ //like while(1) or while(true).
cout<<"> ";
foo(); //if you type 1 or 1. if will access the function foo.
}
bar();
}
exit(0); //if typed 'q' it will exit the program.
//just search the internet for this function.
//i think it's a C based function, which will take as arguments ints:
//0 - everything worked swell and there aren't any buggs or error in the program.
//1 - exit with error (used with perror() funciont). But I don't know exactly all the details.
}
else cout<<"\nInvalid command\n";
//error catching, if you type something different from '1', '1.', '2', '2.', 'q' it will print this
//message.
}
return (0);
}
``````

But again, I don't know what are you trying to accomplish here, maybe you have an ASCII homework, or something like that, so, it's better to stick with what you must do.
If you have further questions, just let us know.

commented: what prt of "I think I've got it" did you not understand in this week-old article? +0

lucaciandrew what i need is instead of typeing q and pressing enter, i press q and whatever code with the if bracets Executes, what your example shows is exactly what i want to happen, Beside's the user input gatherd by typing,i would like to have q or 1 or even 2 to have code executed.

lucaciandrew thanks for the time you spent into responding to my plees of help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.