0

I'm trying to learn C++, my first idea was to make simple win/lost ratio calculator, but it's not working well. This is my code:

#include<iostream>
#include<conio.h> 
using namespace std;

int match=0;
int win=0;
int lose=0;
int ratioo=1;
int total=0;


void lost()
{
if (match=='L');
lose=lose+1;
}

void won()
{
if (match=='W');
win=win+1;
}

int main() {

cout << "Welcome to winratio calculator!" << endl;
cout << "Pressing W adds one win point" << endl;
cout << "Pressing L adds one lose point" << endl;
cout << "Press ENTER to start" << endl;
cout << "Press ESC to close" << endl;
getch();

while(1)
{
cout << "Last game score: ";
cin >> match;
total++;

won();
lost();
ratioo=(win/total)*100;
cout << "Games won: " << win << endl;
cout << "Games lost: " << lose << endl;
cout << "Ratio: " << ratioo <<"%" << endl;
break;
}
return 0;
}

And now my problems:

1)After pressing ANY key there is +1 to win and lose same time

2)I got no idea how to start whole calculator with ENTER and stop it by ESC by getch();, tried few ways but always some errors on the way(it should add points all the time until ESC is pressed

Explanations are very welcome!

Kamzia
  • 11
  • 3
  • 6
    Remove the `;` after your `if` statements, otherwise they do nothing and the next line executes regardless. Also `win/total` does integer division and will truncate, you want to do floating point division (`float` or `double`) – Cory Kramer Jun 13 '18 at 22:02
  • 2
    When you are learning C++, don't choose a problem that depends on interactive user input, particularly not one that depends on reading single keystrokes. Also, avoid menus. –  Jun 13 '18 at 22:03
  • 1
    Note that the `` header and the `getch` function are not part of C++. It's platform-specific header file and function, and incorrectly used will not work well together with `std::cin` and friends. – Some programmer dude Jun 13 '18 at 22:04
  • 1
    Also note that `cin >> match`, where `match` is an `int`, will read an *integer* and not a character. I suggest you take a couple of steps back, [get a couple of good books about C++](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282), and start over. – Some programmer dude Jun 13 '18 at 22:05
  • You should start learning how to use debugger. It will be your best friend soon. – Piotr Siupa Jun 13 '18 at 22:06
  • @NO_NAME "It will be your best fiend soon" - immediately, (given "fiend") I would say. People that jump into the debugger are the ones that have the most problems debugging, in my experience. –  Jun 13 '18 at 22:09
  • After changing ints to doubles and deleting " ; " now the score is 0, is there any other problem? – Kamzia Jun 13 '18 at 22:09
  • 2
    My two bits: Write very little code at a time. Write a few lines, compile, test those few lines. That way if you have a mistake, you learn from it fast and probably don't repeat it all through the code. Less time wasted rewriting. Plus the more you write the more bugs will build up. Bugs are notorious for helping hide one another. It's not unheard of to find fixing multiple simultaneous bugs takes much longer than fixing the same number of bugs one at a time. – user4581301 Jun 13 '18 at 22:10
  • 1
    @Kamzia Since you're asking, you could add indentations to the code. – Piotr Siupa Jun 13 '18 at 22:16

1 Answers1

0

Okay, so it looks like we've got a couple of errors in your program as it is now. Let's take a look!

First thing's first. Notice that you have Semicolons at the end of your if statements.

void lost()
{
     if (match=='L');    //NOTE: This is a useless if statement
          lose=lose+1;   //As is, this line will ALWAYS RUN regardless of what character is pressed.
}

void won()
{
     if (match=='W');  //Same as Above ^^^^^
          win=win+1;
}

Remember that in C, the if statement will execute the next statement conditionally. Remember also that a ; in C is a statement. So, by adding a semicolon after an if statement, you're nullifying the if conditional because the if statement will run the semicolon conditionally, and not your lose = lose + 1;.

Consider writing this instead:

void lost()
{
    if (match == 'L')
    {
        lose = lose + 1;
    }
}

void won()
{
    if (match == 'W')
    {
        win = win + 1;
    }
}

Furthermore, I noticed that you've inserted a break command to avoid an infinite while(1) loop. To avoid this problem. Consider using match = _getch() instead of cin << match.

match = _getch();

In addition, your Ratioo variable is not receiving anything but 0 due to truncation. To get the percentage you're looking for, you must cast your win/total to double as follows:

ratioo = ((double)win / (double)total) * 100;

So, Now we get to detecting the ESC And ENTER Keys! This is exciting. So, in order to read in these invisible characters, you have to understand that to the computer, these keys are just numbers. The ESC Key is number 27, and the ENTER Key is number 13 (Look Up the ASCII Table for a full list). So To detect them you need to do the following:

match = _getch();

if (match == 27)        //27 is the ASCII Code for the Escape Key
{
    break;
}

OR Just Substitute 13 if you're looking for the Enter key. By adding A while(1) Loop around it, you can pause a program until that key is pressed (AND ignore all other input).

My Final Version of your Code can be viewed below:

#include<iostream>
#include<conio.h> 
using namespace std;

int match = 0;
int win = 0;
int lose = 0;
int ratioo = 1;
int total = 0;


void lost()
{
    if (match == 'L')
    {
        lose = lose + 1;
    }
}

void won()
{
    if (match == 'W')
    {
        win = win + 1;
    }
}

int main() {

    cout << "Welcome to winratio calculator!" << endl;
    cout << "Pressing W adds one win point" << endl;
    cout << "Pressing L adds one lose point" << endl;
    cout << "Press ENTER to start" << endl;
    cout << "Press ESC to close" << endl;
    while (1)
    {
        match = _getch();
        if (match == 13)        //13 is the ASCII Code for the Enter Key
        {
            break;
        }
    }

    while (1)
    {
        cout << "Last game score: ";
        match = _getch();

        if (match == 27)        //27 is the ASCII Code for the Escape Key
        {
            break;
        }

        total++;
        won();
        lost();


        ratioo = ((double)win / (double)total) * 100;
        cout << "Games won: " << win << endl;
        cout << "Games lost: " << lose << endl;
        cout << "Ratio: " << ratioo << "%" << endl;
    }
    return 0;
}

Enjoy! And I wish you well towards learning C!

armitus
  • 712
  • 5
  • 20