Consol issue

For some reason, this little program I made to act as a cheat sheet for Ascii codes keeps displaying a new line and the number, '10', after the desired output. If I remove endl, it displays the 10 on the same line.

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
string choiceS;
cout << "Q to exit(windows value 81[CASE])\nNote the 10 is an irrelevant number. igrore it --\nkeys such as enter or your power"
<< " button are 1/0 values that I cannot mess with\n\nkey?\n";


//relevant stuff below
KeyLoop:
key = getchar();
cout << key << endl;
if (key == 81) return 0;
goto KeyLoop;
}
Hello,

So first of all I recommend reading the tutorial. That keyloop(goto method) isn't pretty, why don't you use a while loop?

Second, key is not declared.

Third unless you declare key as an int, cout<<key will output the key the user inputted.

Forth using getchar will create unexpected behavior if a user inputs more than one char.

What you problably want is something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main()
{
string choice;

do
{
    
    getline(cin, choice);
    cout<<static_cast<int>(choice[0]);
    
}while(choice!="Q");

}


What this does is:

declare a string choice. Read input to choice. print numerical value of the first char in choice. Break if choice is equal to Q.

Anyways I really advise you to read a book or good tutorial on C++.
Forth using getchar will create unexpected behavior if a user inputs more than one char.
getline(cin, choice);
cout<<static_cast<int>(choice[0]);
Thankyou!

So first of all I recommend reading the tutorial. That keyloop(goto method) isn't pretty, why don't you use a while loop?
I was experimenting to find out what goto does. I can use while and other such things but this is me learning more stuff.

Second, key is not declared.
I copy& pasted, leaving out some misc. stuff... and that on accident
^
Third unless you declare key as an int, cout<<key will output the key the user inputted.
No problems.

By the way, try to use the [code ][/code] and quote templates, they make your posts more readable.

You can find them on the right side of the writting area.

Good luck with C++. ;)
Topic archived. No new replies allowed.