C++ C++;在循环内添加计数器变量并再次初始化其值

C++ C++;在循环内添加计数器变量并再次初始化其值,c++,C++,我的程序有一个问题,我只是不知道如何在循环中添加一个计数器变量,并再次初始化它的值,以便在我的程序上执行某些语句。每次我运行程序时,只要我输入一个字符,MessageBox函数就会根据我输入的字母数在屏幕上不断显示。我想在用户每次输入一封信时循环 这是我的密码: #include <iostream> #include <windows.h> using namespace std; main() { int x, y = 1000; bool che

我的程序有一个问题,我只是不知道如何在循环中添加一个计数器变量,并再次初始化它的值,以便在我的程序上执行某些语句。每次我运行程序时,只要我输入一个字符,MessageBox函数就会根据我输入的字母数在屏幕上不断显示。我想在用户每次输入一封信时循环

这是我的密码:

#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;
}
#包括
#包括
使用名称空间std;
main()
{
int x,y=1000;
布尔检查=真;
做{
cout>x;
如果(x>=检查){
如果(x>1000){
消息框(NULL,“程序结束”,“结束”,MB|U OK | MB|U图标信息);
}
}
else if(cin.fail()){
cin.clear();
cin.ignore();
MessageBox(NULL,“值:不是钱。”,“错误”,MB_OK | MB_ICONERROR);
}
系统(“cls”);
}而(x<1000);
返回0;
}

以下是修复的示例代码,用于第一次仅显示错误:

#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;
}
#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;
}
#包括
#包括
使用名称空间std;
int main(){
int x,y=1000;
布尔检查=真;
做{
cout>x;
if(cin.fail()){
//输入无效清除失败状态
cin.clear();
cin.ignore();
//我们是否显示了错误消息?
如果(检查){
MessageBox(NULL,“值:不是钱。”,“错误”,MB_OK | MB_ICONERROR);
检查=错误;
}
}
否则{
//输入是一个数字
如果(x>1000){
消息框(NULL,“程序结束”,“结束”,MB|U OK | MB|U图标信息);
返回;
}
}
系统(“cls”);
}而(x<1000);
返回0;
}
我在代码中添加了一些注释以澄清我的更改

Edit here是每次出现错误时显示消息框的更新,而不仅仅是第一次:

#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;
}
#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;
}
#包括
#包括
使用名称空间std;
int main(){
int x,y=1000;
做{
cout>x;
if(cin.fail()){
cin.clear();
cin.ignore();
MessageBox(NULL,“值:不是钱。”,“错误”,MB_OK | MB_ICONERROR);
}
否则{
如果(x>1000){
消息框(NULL,“程序结束”,“结束”,MB|U OK | MB|U图标信息);
返回;
}
}
系统(“cls”);
}而(x<1000);
返回0;
}

Im在这句话中指出了我的帖子:如果(counter2==1){MessageBox(NULL,“Value:nota Money.”,“Error”,MB|u OK | MB| icon);counter2++}即使有计数器,消息框也会一直显示出来。我无法修复该错误。请编辑您的问题,而不是添加更多信息的注释。这听起来更像一个xy问题(),请编辑您的问题并解释您正试图解决的问题。您应该编辑您的问题,并在文本中添加产生问题的最小示例代码。不要发布到一些非站点资源的链接来下载您的代码。我们不想那样。请记住,StackOverflow不是一个论坛。一个问题的主要目的是帮助其他读者从现在起几年内解决同样的问题。@drescherjm谢谢你给我的建议:)对不起,我是新手。但是,顺便说一下,我已经编辑了代码,我希望它现在可以清楚地理解。代码对我不起作用。我正在使用devc++5.11。在MessageBox函数的参数中添加L的目的是什么?我将去掉它。。Visual Studio默认使用宽字符,除非您将其关闭。删除这些字符后,程序将运行。是的,错误信息显示了一次,但是当程序循环时,你又输入了一个字符,MessageBox函数没有出现在我的屏幕上。我想这是你想要的行为。如果希望每次都显示,则根本不需要检查变量/条件。我发布了更新的代码,删除了复选框。如果我输入一个单词,它会多次显示错误,我试图实现的是,每当用户输入一个字符或单词时,错误消息就会弹出一次。然后程序循环,如果用户再次输入字符和单词,它只显示一次错误消息。对不起,我的英语不好