用c ++编写程序,该程序使用循环获取数字并打印到屏幕上 [英] writing a program in c++ that uses a loop to get numbers and print to screen

查看:49
本文介绍了用c ++编写程序,该程序使用循环获取数字并打印到屏幕上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何编写一个执行以下操作的简单程序.
读取五个数字,例如12345
并显示如下
1
2
3
4
5

how do i write a simple program that does the following..
reads five numbers eg.12345
and displays them as follows
1
2
3
4
5

推荐答案

您做了什么?
您可以用文字描述如何完成此任务吗?

您正在使用c ++进行编码,因此请查看以下内容:

std::cin输入值.
std::cout输出值
std::vector存储读取的值.
for迭代向量.
What have you done ?
Can you describe in words how to do this task ?

You are coding in c++, so have a look at the following things:

std::cin to input values.
std::cout to output values
std::vector to store the values read.
for to iterate the vector.


一种实现此任务的方法是

One way to achieve this task is

int _tmain(int argc, _TCHAR* argv[])
{
    int number;
    scanf("%d",&number);

    char buf[10] = {0};
    itoa(number,buf,10);
    for(int count = 0; count < strlen(buf);count++)
        printf("%c\n",buf[count]);

    return 0;
}


只需将循环从1更改为特定的数字(您认为这是长度,例如12345,在这种特殊情况下,您将运行5次循环)将比每次迭代都必须得到余数并将其放入数组中,然后按相反顺序打印数组
just make your loop from 1 to specific number(that you think will be the length eg 12345 in this particular case you will run loop five time) after that than in each iteration you have to get a remainder and put it in array than print the array in reverse order


这篇关于用c ++编写程序,该程序使用循环获取数字并打印到屏幕上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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