2016-08-31 33 views
0

我想创建一个程序,它将使用一维数组来绘制,但是我很难仅使用一个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; 
    } 
} 

我将如何初始化给定,然后将样品输入数组它传递给函数用于绘制

回答

1

下面是一个简单的(或许不安全但随后又不要使用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 
0

我认为你需要一个解析来解码输入。类似以下内容:

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); 
    ...... 
} 
0

enter image description here

这里是一个程序,让你输入的一个版本,一系列的数字只有一个cinstringstream的求助热线,但唯一不同的是,它卖场输入在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; 
    } 
}