-3

I'm having a problem on my program, i just don't know how can i add a counter variable inside my loop and initialize its value again to perform certain statement on my program. Every time i run the program, whenever i input a character the MessageBox Functions keeps showing on my screen depending on how many letters i inputted. I want to loop every time a user enters a letter.

Here's my code:

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

main()
{

    int x, y = 1000;
    bool check = true;
    do {

        cout << "Enter a Number Only: ";
        cin >> x;

        if (x >= check) {
            if (x > 1000) {
                MessageBox(NULL, "Program Ends.", "Ends", MB_OK | MB_ICONINFORMATION);
            }
        }
        else if (cin.fail()) {
            cin.clear();
            cin.ignore();
            MessageBox(NULL, "Value: Not a Money.", "Error", MB_OK | MB_ICONERROR);
        }

        system("cls");
    } while (x < 1000);

    return 0;
}
Clark
  • 169
  • 3
  • 11
  • Im pointing my post in this statement: if(counter2 == 1){ MessageBox(NULL, "Value: Not a Money.", "Error", MB_OK | MB_ICONERROR); counter2++; } The Message Box keeps showing even there is a counter in there. I can't fix the bug. – Clark Sep 27 '18 at 13:11
  • 1
    Please edit your question instead of adding comments with more information. – Max Langhof Sep 27 '18 at 13:12
  • I don't see how this code produces the result you say. I think you will need to debug. If you don't have a good debugger like the one in Visual Studio add some output so you can track what exactly is going on. – drescherjm Sep 27 '18 at 13:18
  • 3
    It's sounds more like an xy-problem (https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) , please edit your question and explain what exactly the problem that you are trying to solve. – Coral Kashri Sep 27 '18 at 13:21
  • @drescherjm Here's the output, it keeps showing that message box depends on the numbers of letter i entered. Link: https://imgur.com/a/x7CfquG – Clark Sep 27 '18 at 13:30
  • Your problem is probably caused by this line: `counter2 = 1;` if you have a debugger set a breakpoint on that line. If not I recommend you get a better IDE. – drescherjm Sep 27 '18 at 13:36
  • @drescherjm it is okay to share my program here? I'll upload the whole program just for you guys to understand. Im really sorry for my bad English. – Clark Sep 27 '18 at 13:40
  • 1
    You should edit your question and add the minimal example code in text that produces your problem. [mcve] Don't post a link to some off site resource to download your code. We don't want that. Remember that StackOverflow is not a forum. The main goal of a question is to help other readers years from now with the same problem. – drescherjm Sep 27 '18 at 13:41
  • loop continues to run when payment < total, and you are making counter2 = 1 when payment < total. Wheneven loop continues to run, counter2 value will be 1 and that's the reason MessageBox is displayed. You can use static counter. – RajeshDA Sep 27 '18 at 13:52
  • 1
    @drescherjm Thank you for giving me an advice :) I'm new to this sorry. But, by the way i already edited the codes, i hope it can now clearly understands. – Clark Sep 27 '18 at 13:53
  • @user1512 i made some changes in the code sorry, you can review it again to understant it more. – Clark Sep 27 '18 at 13:59
  • I get a warning about `if (x >= check) {`. The comparison of x to check does not make sense. – drescherjm Sep 27 '18 at 14:05
  • I am somewhat confused why you only want to show the error message 1 time. I mean if they enter `a` get the message box. Then enter a letter again you don't want to alert them or do you? – drescherjm Sep 27 '18 at 14:09

1 Answers1

0

Here is the example code fixed to only display an error the first time:

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

int main() {

    int x, y = 1000;
    bool check = true;
    do {

        cout << "Enter a Number Only: ";
        cin >> x;

        if (cin.fail()) {
            // Input was not valid clear the fail status
            cin.clear();
            cin.ignore();

            // Did we display the error message? 
            if (check) {
                MessageBox(NULL, "Value: Not a Money.", "Error", MB_OK | MB_ICONERROR);
                check = false;
            }
        }
        else {
            // Input was a number
            if (x > 1000) {
                MessageBox(NULL, "Program Ends.", "Ends", MB_OK | MB_ICONINFORMATION);
                return;
            }
        }

        system("cls");
    } while (x < 1000);


    return 0;
}

I added some comments in the code to clarify my changes.

Edit here is the update to display the Message Box each time there is an error instead of just the first time:

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

int main() {

    int x, y = 1000;

    do {

        cout << "Enter a Number Only: ";
        cin >> x;

        if (cin.fail()) {
            cin.clear();
            cin.ignore();
            MessageBox(NULL, "Value: Not a Money.", "Error", MB_OK | MB_ICONERROR);
        }
        else {
            if (x > 1000) {
                MessageBox(NULL, "Program Ends.", "Ends", MB_OK | MB_ICONINFORMATION);
                return;
            }
        }

        system("cls");
    } while (x < 1000);


    return 0;
}
drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • The code isn't working for me. I'm using Dev c++ 5.11. What's the purpose of adding L inside the parameter of MessageBox Function? – Clark Sep 27 '18 at 14:20
  • I will get rid of that.. Visual Studio defaults to use wide characters unless you turn that off. – drescherjm Sep 27 '18 at 14:20
  • After removing those, the program runs. And yes the Error Message Displayed one time, but when the program loops then you entered a character again, the MessageBox Function did not appear on my screen. – Clark Sep 27 '18 at 14:25
  • I thought that was the behavior you wanted. If you want it to appear each time no need for the check variable / condition at all. I posted updated code with the check removed. – drescherjm Sep 27 '18 at 14:26
  • It shows the Error many times if i input a word, what im trying to achieve is, whenever a user enter a character or a word the Error Message Pop-up one time. Then the program loops, then if the user enter a character and a word again, it shows the error message one time only. Sorry im not a good in English – Clark Sep 27 '18 at 14:31
  • I believe that is what I have accomplished with the second code unless you are trying to read a character at a time without pressing enter. If that is the case you can't use std::cin. Here is a topic about reading a character at a time without pressing enter: https://stackoverflow.com/questions/421860/capture-characters-from-standard-input-without-waiting-for-enter-to-be-pressed – drescherjm Sep 27 '18 at 15:30