基本的c ++代码问题

时间:2016-03-30 00:01:36

标签: c++ windows graphics

我需要一些C ++代码的帮助。我正在尝试制作一个程序,其中单词up up down down down down the screen。我希望它这样做3次。我的程序是成功的,其中的单词做我想要他们做的事情。但是,它没有做3次。我把它放在for循环中,所以我不确定是什么问题。我正在学习,所以我只需要帮助了解我做错了什么。这是我的代码:

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

int main() 
{ 
    HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE); 
    COORD posUp = {5,25}; 
    COORD posDown = {68,1}; 

    SetConsoleCursorPosition(screen, posUp); 
    cout<<"Up"<<endl; 
    SetConsoleCursorPosition(screen, posDown); 
    cout<< "Down"<<endl; 
    Sleep(400); 
    for(int comeback=1; comeback<=3; comeback++) { 
        while(posUp.Y > 1 && posDown.Y < 24) { 
            SetConsoleCursorPosition(screen,posUp); 
            cout<<" "<<endl; 
            posUp.Y--; 
            SetConsoleCursorPosition(screen, posUp); 
            cout<<"Up"<<endl; 
            SetConsoleCursorPosition(screen, posDown); 
            cout<<" "; 
            posDown.Y++; 
            SetConsoleCursorPosition(screen, posDown); 
            cout<<"Down"<<endl; 
            Sleep(100); 
        } 

        SetConsoleCursorPosition(screen, posUp); 
        cout<<" "<<endl; 
        SetConsoleCursorPosition(screen, posDown); 
        cout<< " "<<endl; 
        Sleep(100); 
    } 
    return 0; 
}

1 个答案:

答案 0 :(得分:0)

我看起来像你的递减posUp并在while循环中递增posDown但是没有重置for循环结束时的值。

for(int comeback=1; comeback<=3; comeback++) { 
    while(posUp.Y > 1 && posDown.Y < 24) { 
        SetConsoleCursorPosition(screen,posUp); 
        cout<<" "<<endl; 
        posUp.Y--; 
        SetConsoleCursorPosition(screen, posUp); 
        cout<<"Up"<<endl; 
        SetConsoleCursorPosition(screen, posDown); 
        cout<<" "; 
        posDown.Y++; 
        SetConsoleCursorPosition(screen, posDown); 
        cout<<"Down"<<endl; 
        Sleep(100); 
    } 

    SetConsoleCursorPosition(screen, posUp); 
    cout<<" "<<endl; 
    SetConsoleCursorPosition(screen, posDown); 
    cout<< " "<<endl; 
    posUp = {5,25}; 
    posDown = {68,1}; 
    Sleep(100); 
}