C ++输入和输出 [英] C++ input and output

查看:135
本文介绍了C ++输入和输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想让我的程序变得像我输入的数字一样,在0-50 51-100的范围内,它表示在astricks中有多少数字是0-50而在5-100中是相同的


例如我输入23,34,89,99,67,1,2。



i希望它像0-50 ****

51-100 ***



但我现在输入数字后得到了星号,所以我说0-50显示*,51-100显示!,每个数字后面显示符号而不是取所有数字并显示符号结束。



我的第一次实际编码所以我知道我很笨。



我有什么试过:



#include< iostream>



使用命名空间std;



int main()

{

int PSI;



for(int i = 0; i< 7; i ++)

{

cin>> PSI;



if(PSI> = 0,PSI< = 50){



cout< < *<< endl;

}

else if(PSI> = 51,PSI< = 100)

cout<< *<<结束;



}



返回0;

}

so i wanna make my program go like if i enter few numbers, in range of 0-50 51-100 it shows how many numbers are 0-50 in astricks and same for 5-100

eg i enter 23, 34,89,99,67,1,2.

i want it to be like 0-50 ****
51-100 ***

but my currently i get the asterick after i enter the number, so i i say 0-50 display *, 51-100 display !, after each number it shows the symbol instead of taking all the numbers and display the symbol the the end.

my first time actually coding so i know i am dumb.

What I have tried:

#include <iostream>

using namespace std;

int main()
{
int PSI;

for (int i =0; i<7; i++)
{
cin >> PSI;

if (PSI>=0 , PSI<=50){

cout << "*" << endl;
}
else if (PSI>=51,PSI<=100)
cout << "*" << endl;

}

return 0;
}

推荐答案

你应该维护范围命中的计数器,例如

You should maintain counters for range hits, e.g.
#include <iostream>
using namespace std;

int main()
{
  int pres;

  int inrange[2]={0};
  string range[2]  = {"  0- 50 ", " 51-100 "};

  for (int i =0; i<7; i++)
  {
    cin >> pres;
    if ( (pres>= 0) && (pres <= 50) )
      ++inrange[0];
    else if ( (pres > 50) && (pres <=100))
      ++inrange[1];
    else
    {// handle 'out of range' pressure

    }
  }

  for (int i=0;i<2; ++i)
  {
    cout << range[i];
    for (int j=0; j<inrange[i]; ++j)
      cout << "*";
    cout << endl;
  }
}


这篇关于C ++输入和输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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