I was trying to take user input in a vector in c++, as vector is a dynamic data structure i wanted it to take input as long as user wants and when the user hit enter key it should stop taking the input (just as a string) only difference i want to do it in an integer vector, but i am unable to do so let me know if it is possible or not to take user input in a vector like the way i described.

I have searched the web for various ways but still my code isn't running the way i want, I have tried breaking the loop of input by using EOF symbols but it is not stopping the loop of input. I have tried using cin.ignore() function but it also takes spacebar in count i only want to stop input when enter key is pressed. below is the most recent code i made:

#include<bits/stdc++.h>
using namespace std;
int main(){
vector<int> v;
char c;
for(int i=0;;i++){
    cin>>c;
    if(c=='\n'){
        break;
    }
    int x = c - '0';// typecasting char into integer.
    v.push_back(x);
}
cout<<v.size()<<endl;// just to check if it is done taking input and what is the size of vector now.
}

3 Answers

1
DimChtz On Best Solutions

You can use std::getline() to get the entire line and then std::istreamstream to get the integers into a vector:

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <iterator>

int main() {

    std::string input;

    std::getline( std::cin, input );

    std::istringstream is( input );

    std::vector<int> v( ( std::istream_iterator<int>( is ) ), std::istream_iterator<int>() );

    std::cout << v.size() << "\n";

    return 0;
}
0
ivangalban On

Read the entire line as string and build each number. Something like that:

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    string s; getline(cin, s);
    int num = 0;
    vector<int> v;
    for(int i = 0; i < s.size(); ++i)
        if(s[i] == ' ')
            v.push_back(num), num = 0;
        else
            num = num*10 + (s[i]-'0');
    v.push_back(num);

    cout << v.size() << endl;

    return 0;
}
-1
Bill Smith On
  1. When you read from std::cin, it will accept an entire line from the console. Your cin>>c will retrieve the first character of the line and put that character into the variable c.
  2. This means your if(c=='\n') is pretty much pointless since the line will be consumed and you won't see an explicit '\n' as the first character...EVER.
  3. c - '0' will convert a single ASCII character that is a digit (i.e. 0123456789) to a number you can use, but that is NOT typecasting a char into an integer. That's converting an ASCII character that happens to be a number into a numeric value. A type cast is when you change the TYPE of a value to another TYPE.
  4. I think you want to do something like the following...

    #include <iostream>
    #include <vector>
    #include <stdio.h>
    #include <conio.h>
    
    int main( int cArguments, const char** apszArguments ) {
        std::vector<char> vchars;
        char c;
    
        do {
          c = getche();
          vchars.push_back( c );
        } while( c != '\n' );
    
        std::cout << vchars.size() << " characters received" << std::endl;
        return 0;
    }
    

Note that on macOS and some Linux variants, getch/getche are not defined and you'll need to use termios or ncurses functions instead.