容易c ++循环与暂停,但输出是非常奇怪! [英] easy c++ loop with pause but output is very weird!

查看:115
本文介绍了容易c ++循环与暂停,但输出是非常奇怪!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是试图写一个程序,输出一系列数字在控制台屏幕的同一行重写。像10 9 8 7 6等。

I'm just trying to write a program which outputs a series of numbers overwriting one another on the same line of the console screen. like 10 9 8 7 6 etc.

我在xcode中使用xcode和编译。而这输出10 121469 121468,我做错了什么?为什么看起来不那么明显?

I'm using xcode and compiling in xcode. And this outputs "10 121469 121468", what am I doing wrong? Why doesn't it seem so obvious?

#include <iomanip>
#include <iostream>
using namespace std;

#ifdef __GNUC__
#include <unistd.h>
#elif defined _WIN32
#include <cstdlib>
#endif

int main()
{

  cout << "Description: This program will show you how much change" << endl;
  cout << "you will need to complete a transaction using a already" << endl;
  cout << "specified denomination" << endl << endl;

  cout << "CTRL=C to exit...\n";

  for (int units = 10; units > 0; units--)
  {
    cout << units << ' ';
    cout.flush();

#ifdef __GNUC__
    sleep(1); //one second
#elif defined _WIN32
    _sleep(1000); //one thousand milliseconds
#endif

    cout << '/r'; // CR
  }

  return 0;
} //main


推荐答案

不知道这是否回答了你的答案,但我已经看到你的 CR 是错误的。

I don't know if this answers your answer but I've seen that your CR is wrong.

cout << '/r'; // CR

为2个字符(这是您的 12146 打印在屏幕上)。正确的值必须为

is 2 characters (which is your 12146 printed on the screen). The correct value must be

cout << '\r'; // CR

这篇关于容易c ++循环与暂停,但输出是非常奇怪!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆