CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2017
    Posts
    1

    How to Input Multiple Integer Using CIN C++

    How do I input 1 integer A and input multiple integer based on the number of integer A in one line using cin?

    example input:
    3 3 1 2 (the first input is 3, so I want to input 3 variables)
    2 7 8 (the first input is 2, so I want to input 2 variables)
    4 5 3 7 2 (the first input is 4, so I want to input 4 variables)

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: How to Input Multiple Integer Using CIN C++

    It sounds like it would be appropriate to use nested loops: the outer loop loops over each line of input, reading the first number of each line and then going into the inner loop; the inner loop loops as many times as the first number of the line, hence reading the remaining input on the line.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    May 2009
    Location
    Boston
    Posts
    364

    Re: How to Input Multiple Integer Using CIN C++

    If you use cin to dump your input to a string, you can then parse the string on your delimiter (in this case space) into a vector and then manipulate the resulting vector however you need to. If you are programming in c++, you may as well use containers.

    In this case, the first element of the vector would contain the number of variables and the rest of the vector would contain the variable values.

    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    int main() {
    
       // declare string to hold input and initialize to blank
       string input_string;  input_string.assign("");
    
       cout << endl;
       cout << "input space delimited series of integers" << endl;
       cout << endl;
    
       // read input from standard in
       cin >> input_string;
    
       cout << endl;
       cout << "processing input:" << endl;
       cout << input_string << endl;
       cout << endl;
    
       // to parse input string
       stringstream input_parser_stream;
    
       //load row string to stringstream
       input_parser_stream << input_string;
    
       // temp string to hold each parsed cell value
       string input_element;  input_element.assign("");
    
       // input delimiter
       string input_delimiter;  input_delimiter.assign(' ');
    
       // vector to hold parsed input
       vector<string> parsed_input;
    
       // parse input string into elements on delimiter
       while(getline(input_parser_stream, input_element, input_delimiter)) {
    
          // remove '\r' EOL chars
          input_element.erase(remove(input_element.begin(),input_element.end(),'\r') , input_element.end());
    
          // add parsed element to vector
          parsed_input.push_back(input_element);
    
          // reinitialize element string
          input_element.assign("");
       }
    
       // clear the buffer
       input_stream.clear();
    
       // print input
       cout << endl;
       cout << "line: " << input_string << endl;
       // first element of parsed_input[] contains the number of input variables
       cout << "has " << parsed_input[0] << "  variables" << endl;
       // print all elements after the first
       for(unsigned int i=1; i<parsed_input.size(); i++) { cout << parsed_input[i] << endl; }
       cout << endl;
    
       // clear the parsed input vector for next use
       parsed_input.clear();
    
    // int main() endbrace
    }
    There isn't any need to evaluate the number of inputs if you implement something similar to this. If you were going to evaluate multiple strings, you would put the cin statement into a loop and it would be best to create a separate function to process each input string.

    Keep in mind that how you store any data is related to how you need to retrieve it later so that is something you need to think about sooner rather than later.

    Note 1: The values in parsed_input[] are stored as standard strings, so if you need to use them as integers (such as to do math), you will need to do a type conversion at some point. That would probably be best to do before you push each parsed element into the vector. If you need ints in the end, use a vector of int and convert each element string to int using something like stoi() before pushing it.

    Code:
    // vector to hold parsed input
    vector<int> parsed_input;
    // convert from string to int and add parsed element to vector
    parsed_input.push_back( atoi( input_element.c_str() ) );
    Note 2: I did not compile the above, so there could be an issue here or there. Let me know if what I posted isn't clear.

    LMHmedchem
    Last edited by LMHmedchem; October 18th, 2017 at 06:36 PM.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: How to Input Multiple Integer Using CIN C++

    Code:
    // read input from standard in
    cin >> input_string;
    This will only extract data from cin until the first white-space char following non-white space chars. So if the input is
    3 3 1 2

    then only the first 3 will be extracted to input_string. Consider

    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <vector>
    using namespace std;
    
    int main()
    {
    	const char input_delimiter = ' ';
    
    	string input_string;
    
    	cout << "Input space delimited series of integers" << endl;
    	getline(cin, input_string);
    
    	stringstream input_parser_stream (input_string);
    	vector<string> parsed_input;
    
    	for (string input_element; getline(input_parser_stream, input_element, input_delimiter); parsed_input.push_back(input_element));
    
    	cout << "The values are" << endl;
    
    	for (const auto& p : parsed_input)
    		cout << p << endl;
    
    	cout << endl;
    }
    This has the issue, however, that irrespective of the value of the first integer entered, all the data values entered will be parsed and stored. Doing it this way doesn't really require an initial count of numbers to be read.

    Based upon lasrlight's post #2, consider

    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main()
    {
    	int num;
    	vector<int> vi;
    
    	cout << "Input space delimited series of integers" << endl;
    	cin >> num;
    
    	for (int n = 0; n < num; ++n) {
    		int val;
    		cin >> val;
    		vi.push_back(val);
    	}
    
    	cout << "\nThe numbers are" << endl;
    
    	for (const auto& v : vi)
    		cout << v << endl;
    
    	cout << endl;
    }
    Last edited by 2kaud; October 19th, 2017 at 03:29 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured