繁体   English   中英

初始化并传递数组

[英]Initialization and passing an array

我正在尝试创建一个将使用一维数组进行绘制的程序,但是我很难仅使用一个cin语句来初始化该数组。 用户应该看起来像的样本输入

1<space>2<space>34<space>3<space>2<space>1<space>0<space>10


#include<iostream>
using namespace std;
/*---------------------------------------------------------------------------------------
Prototypes
These are the prototype function(s) that will be used to to draw the row and columns
---------------------------------------------------------------------------------------*/
void draw(int nums);
//---------------------------------------------------------------------------------------
int main(){
    const int MAX = 100;
    int chart[MAX];
    int nums;

    cout << "Enter numbers for the chart" << endl;
    cin >> nums;
    draw(nums);

return 0;
}

void draw(int nums) {
     cout << endl;
     int row;

     for (row = 0; row < nums; ++row) {
         cout << "*" << endl;
     }
}

如何使用给定的示例输入初始化数组,然后将其传递给用于绘制的函数

这是一个简单的(也许不安全,但为了安全起见,请不要再次使用std :: cin)实现,似乎可以读取数字:

#include <iostream>
#include <list>
#include <sstream>
int main()
{
    std::cout << "Input numbers: ";
    // get input line
    std::string input;
    std::getline(std::cin, input);
    std::stringstream ss(input);
    // read numbers
    std::list<int> numbers;
    while(ss) {
        int number;
        ss >> number;
        ss.ignore();
        numbers.push_back(number);
    }
    // display input
    for(const auto number: numbers) {
        std::cout << number << std::endl;
    }
    return 0;
}

这是一个示例运行:

$ ./a.out
Input numbers: 1 2 3 4
1
2
3
4

我认为您需要解析来解码输入。 如下所示:

void parse(const std::string& input, int output[], int MaxNum)
{
    // parse the integer from the string to output.
}

int main(){
    ......
    std::string input;
    cout << "Enter numbers for the chart" << endl;
    cin >> input;
    parse(input, chart, MAX);
    ......
}

在此处输入图片说明

这是该程序的一个版本,可让您在stringstream的帮助下仅用一个cin行输入一系列数字,但是唯一的区别是它将输入存储在vector 然后根据输入绘制直方图。

只需按两次<ENTER>键,程序就会知道您已经输入了数字。

#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <sstream>
using namespace std;

vector<int> Vector;
string line;

void drawchart(int max);


int main() {

    cout<<"Chart drawing program ( Histogram) \n";
    cout<<"Enter a series of numbers. \n";
    cout<<"Seperate with a space, press <ENTER> TWICE to end input \n";
    cout<<" (e.g  2 3 4 5 6)  >  ";

    if(!getline(cin, line)) return 1;
    istringstream iss(line);

    copy( istream_iterator<int>(iss), istream_iterator<int>(),  back_inserter(Vector));

    copy(Vector.begin(), Vector.end(), ostream_iterator<int>(cout, ", "));

    cout<<"\nDrawing chart.. \n\n";

    drawchart( Vector.size() );


    cout<<"Press ANY key to close.\n\n";    
    cin.ignore();cin.get();

return 0;
}

// draws a chart or hjistogram
void drawchart(int max){
    for( int i = 0; i < max ; i++){
        for(int j = 0; j < Vector[i]; j++)  cout << "*";
        cout << endl;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM