0

I am trying to create a program that will draw using a single dimensional array however I am having a hard time initializing the array using only one cin statement. A sample input that the user in supposed to look like

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;
     }
}

How would I initialize the array with the sample input given and then pass it to a function to be used to draw

3 Answers 3

1

Here's a simple (perhaps unsafe but then again don't use std::cin for safety) implementation that seems to work for reading in the numbers:

#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;
}

And here's a sample run:

$ ./a.out
Input numbers: 1 2 3 4
1
2
3
4
Sign up to request clarification or add additional context in comments.

Comments

0

I think you need a parse to decode the input. something like following:

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

Comments

0

enter image description here

Here is a version of a program that lets you input a series of numbers with only one cin line with the help of stringstream, but the only difference is that it stores the input in a vector. It then draws a histogram chart based on the input.

Just press the <ENTER> key twice to let the program know that you are done with the inputting of the numbers.

#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;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.