Input numbers by keyboard into array but only 1 line - c++

Is there any way to enter numbers (seperated by spaces) on a single line into an array ? I mean, I used to write like this:
First, I entered sizeofarray. Then, I used [for] loop to enter each number into each element. In this method, I had to press enter for each time
So what I want is:
First, enter sizeofarray. Then, on a single line, enter all numbers for all elements, each of it seperated by a space
Ex: 7, enter
1 5 35 26 5 69 8, enter
So that all numbers are stored into elements dedicated.
I know my English is not good and I'm not a good coder. So please explain it easy. Thanks :D

I don't know why everyone is trying to do it in String way..
it's simple that C++ std::cin can get it so easy
int main (){
int a[1000],sizeOfA;
cin>>sizeOfA;
for (int i=0;i<sizeOfA;i++)
cin>>a[i];

If you are going to enter all numbers in a single line, then it is completely unnecessary to begin by entering the number of numbers that will follow.
You are going to need to read the entire line into a string, (char[]) and then parse that string to find substrings separated by spaces, and then you are going to need to parse each substring into a number.
Precisely how to do this, we won't tell, because stackoverflow is not about having others do your homework for you.

Read in the input as a string, then split by spaces to get the individual numbers:
int main()
{
using namespace std;
int* nums;
int size;
cout << "Enter size of array";
cin >> size;
nums = new int[size];
string input;
cout << "Enter numbers, separated by single space:\n";
getline(cin, input);
istringstream iss(input);
string s;
int i = 0;
while (getline(iss, s, ' ') && i < size) {
int num = atoi(s.c_str());
nums[i] = num;
printf("%d\n", num);
++i;
}
return 0;
}

the most optimal and safe way is use containers, iterators and streams. If 'istream_iterator ' extract from the stream value other than 'int', it will be equal to 'end', so reading from the stream being to the first non-int, or until the end
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
int main()
{
using namespace std;
size_t size = 0;
cin >> size;
cin.ignore();
string buffer;
getline(cin, buffer);
stringstream ss(buffer);
istream_iterator<int> iter(ss);
istream_iterator<int> end;
vector<int> vec;
vec.reserve(size);
for (size_t i = 0; i < size && iter != end; ++i, ++iter)
{
vec.push_back(*iter);
}
}

Related

Read multiple integers in the same line with cin in C++

I have a problem reading integers from the user. I could use
int a, b, c;
cin >> a >> b >> c;
But I don't know how much integers the user will introduce. I have tried this:
int n;
cin >> n; //Number of numbers
int arrayNumbers[n];
for(int i=0;i<n;i++){
cin>>arrayNumbers[i]
}
And the input of the user will be like:
1 2 3
I mean, in the same line. Using my previous code, it only gets the fist number and not the rest.
How could I do it?
Use std::getline() to read the whole line into a string first. Then create a stringstream from the input string. Finally use a istream_iterator to iterate over the individual tokens. Note that this method will fail at the first input that is not an integer. For example if the use inputs: " 1 2 ab 3" then your vector will contain {1,2}.
int main() {
std::string str;
//read whole line into str
std::getline(std::cin, str);
std::stringstream ss(str);
//create a istream_iterator from the stringstream
// Note the <int> because you want to read them
//as integers. If you want them as strings use <std::string>
auto start = std::istream_iterator<int>{ ss };
//create an empty istream_iterator to denote the end
auto end= std::istream_iterator<int>{};
//create a vector from the range: start->end
std::vector<int> input(start, end);
}
Rather than using a static array int arrayNumbers[n] which requires you to know how many numbers the user will enter prior to compilation, you should use a dynamic array such as std::vector<int> which can change size during run time.
For example:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vecNumbers; // a dynamic array of integers
while(!cin.fail())
{
int value;
cout << "Enter a value: ";
cin >> value; // don't forget the semicolon
if(!cin.fail()) // not having this will add another 0 to the vector
vecNumbers.push_back(value);
}
// print the final vector (can ignore for your question)
vector<int>::const_iterator vecItr;
cout << "\nvecNumbers = [";
for(vecItr = vecNumbers.begin(); vecItr != vecNumbers.end(); vecItr++)
{
if(vecItr != vecNumbers.end()-1)
cout << *vecItr << ", ";
else
cout << *vecItr << "]";
}
return 0;
}
Output:
Enter a value: 5
Enter a value: 6
Enter a value: 7
Enter a value: 8
Enter a value: s
vecNumbers = [5, 6, 7, 8]
Notice that !cin.fail() detects when the input type doesn't match and thus ends the loop. So you could have a statement that mentions this, like "when done, enter any character to end".
Also note that I #include <vector> and using namespace std, if you don't then you can simply use std::vector<int>, std::cout, and std::cin.

c++ count whitespaces between numbers (not strings)

So I have a program that takes in integers from user and puts them in a vector and displays them. however, I want to be able to show the total number of whitespaces between the numbers I put while inputting the numbers also.. I had a look online but couldn't find anything that would work for ints or chars.. I cant use strings or getline right now so idk what to do..
here is my code
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int a,count;
vector<int> numbers;
while(cin>>a)
{
numbers.push_back(a);
// count whitespaces??
}
for(int i=0;i<numbers.size();i++)
{
cout<<numbers[i]<<endl;
}
return 0;
}
so if the input is 1(space)2(space)(space)3(space)
i want to be able to calculate the total spaces in between so in this case count would be 4
Try getline():
string str;
getline(cin,str);
and then count whitespace in str:
int wc = 0;
for (auto c: str) wc += (c == ' ');
cout << wc << endl;

How can I get the number of characters from an input string?

I am trying to count the number of characters in a string that is provided by the user. I know I can use string::length() and string::size() but when a space is encountered, the count is stopped. For example, say the user inputs "Bob Builder", the count should be 10 but what my code would display would be 3. Also I am trying to do this without using a character array. Any suggestions? An explanation would also greatly help.
int main()
{
string Name;
cin>>Name;
cout << name(Name);
return 0;
}
int name(string a)
{
int numChar;
/*for (int i=0; a[i] != '\0';i++)
{
if (!isspace(a[i]))
numChar++;
}*/
numChar=a.length();
return numChar;
}
How yu know when input is over?
If you want to read until end of line then this is a possible solution:
std::string line ;
std::cin.getline(line) ;
line.length() ;
You have to use getline() instead of cin to get all line up to newline. cin reads input up to whitespace.
std::getline (std::cin,Name);
If you use using namespace std;
getline (cin,Name);
If you want to count the input string excluding spaces, the code snippet helps you.
#include <algorithm>
#include <string>
int main()
{
std::string s = "Hello there, world!";
std::cout << std::count( s.begin(), s.end(), ' ' ) << std::endl;
}

String not getting stored in vector?

#include <iostream>
#include <vector>
using namespace std;
int main()
{
string n, m;
vector <string> dingdong;
cin >> n;
dingdong.push_back(n);
cin >> m;
dingdong.push_back(m);
for (int i = 0; i <2; i ++) {
cout << dingdong[i];
}
return 0;
}
When I run the program and I input "hay sombody there" and hit enter. The program prints "haysombody." So I figured if I increase 'i' to 3 the program will print "haysombodythere" but no, main just crashes. why is this happening and how do I make it so that the entire strings (including the spaces) get stored?
"why is this happening and how do I make it so that the entire strings (including the spaces) get stored?"
To get more than a single word from the input you should use
std::getline(cin,n);
instead of
std::cin >> n;
White spaces are used as delimiters by default, so each call of std::istream's operator>> will just store the text read up to the next white space character.
See a fully fixed version of your program here please.
Also if you really want to read into the vector word by word, you use a loop doing so
string word;
vector <string> dingdong;
while(cin >> word) {
if(word.empty) {
break;
}
dingdong.push_back(word);
}
and print out like
for (int i = 0; i < dingdong.size(); ++i) {
cout << dingdong[i];
}

How to read in user entered comma separated integers?

I'm writing a program that prompts the user for:
Size of array
Values to be put into the array
First part is fine, I create a dynamically allocated array (required) and make it the size the user wants.
I'm stuck on the next part. The user is expected to enter in a series of ints separated by commas such as: 1,2,3,4,5
How do I take in those ints and put them into my dynamically allocated array? I read that by default cin takes in integers separated by whitespace, can I change this to commas?
Please explain in the simplest manner possible, I am a beginner to programming (sorry!)
EDIT: TY so much for all the answers. Problem is we haven't covered vectors...is there a method only using the dynamically allocated array I have?
so far my function looks like this. I made a default array in main. I plan to pass it to this function, make the new array, fill it, and update the pointer to point to the new array.
int *fill (int *&array, int *limit) {
cout << "What is the desired array size?: ";
while ( !(cin >> *limit) || *limit < 0 ) {
cout << " Invalid entry. Please enter a positive integer: ";
cin.clear();
cin.ignore (1000, 10);
}
int *newarr;
newarr = new int[*limit]
//I'm stuck here
}
All of the existing answers are excellent, but all are specific to your particular task. Ergo, I wrote a general touch of code that allows input of comma separated values in a standard way:
template<class T, char sep=','>
struct comma_sep { //type used for temporary input
T t; //where data is temporarily read to
operator const T&() const {return t;} //acts like an int in most cases
};
template<class T, char sep>
std::istream& operator>>(std::istream& in, comma_sep<T,sep>& t)
{
if (!(in >> t.t)) //if we failed to read the int
return in; //return failure state
if (in.peek()==sep) //if next character is a comma
in.ignore(); //extract it from the stream and we're done
else //if the next character is anything else
in.clear(); //clear the EOF state, read was successful
return in; //return
}
Sample usage http://coliru.stacked-crooked.com/a/a345232cd5381bd2:
typedef std::istream_iterator<comma_sep<int>> istrit; //iterators from the stream
std::vector<int> vec{istrit(in), istrit()}; //construct the vector from two iterators
Since you're a beginner, this code might be too much for you now, but I figured I'd post this for completeness.
A priori, you should want to check that the comma is there, and
declare an error if it's not. For this reason, I'd handle the
first number separately:
std::vector<int> dest;
int value;
std::cin >> value;
if ( std::cin ) {
dest.push_back( value );
char separator;
while ( std::cin >> separator >> value && separator == ',' ) {
dest.push_back( value );
}
}
if ( !std::cin.eof() ) {
std::cerr << "format error in input" << std::endl;
}
Note that you don't have to ask for the size first. The array
(std::vector) will automatically extend itself as much as
needed, provided the memory is available.
Finally: in a real life example, you'd probably want to read
line by line, in order to output a line number in case of
a format error, and to recover from such an error and continue.
This is a bit more complicated, especially if you want to be
able to accept the separator before or after the newline
character.
You can use getline() method as below:
#include <vector>
#include <string>
#include <sstream>
int main()
{
std::string input_str;
std::vector<int> vect;
std::getline( std::cin, input_str );
std::stringstream ss(str);
int i;
while (ss >> i)
{
vect.push_back(i);
if (ss.peek() == ',')
ss.ignore();
}
}
The code is taken and processed from this answer.
Victor's answer works but does more than is necessary. You can just directly call ignore() on cin to skip the commas in the input stream.
What this code does is read in an integer for the size of the input array, reserve space in a vector of ints for that number of elements, then loop up to the number of elements specified alternately reading an integer from standard input and skipping separating commas (the call to cin.ignore()). Once it has read the requested number of elements, it prints them out and exits.
#include <iostream>
#include <iterator>
#include <limits>
#include <vector>
using namespace std;
int main() {
vector<int> vals;
int i;
cin >> i;
vals.reserve(i);
for (size_t j = 0; j != vals.capacity(); ++j) {
cin >> i;
vals.push_back(i);
cin.ignore(numeric_limits<streamsize>::max(), ',');
}
copy(begin(vals), end(vals), ostream_iterator<int>(cout, ", "));
cout << endl;
}
#include <iostream>
using namespace std;
int main() {
int x,i=0;
char y; //to store commas
int arr[50];
while(!cin.eof()){
cin>>x>>y;
arr[i]=x;
i++;
}
for(int j=0;j<i;j++)
cout<<arr[j]; //array contains only the integer part
return 0;
}
The code can be simplified a bit with new std::stoi function in C+11. It takes care of spaces in the input when converting and throws an exception only when a particular token has started with non-numeric character. This code will thus accept input
" 12de, 32, 34 45, 45 , 23xp,"
easily but reject
" de12, 32, 34 45, 45 , 23xp,"
One problem is still there as you can see that in first case it will display " 12, 32, 34, 45, 23, " at the end where it has truncated "34 45" to 34. A special case may be added to handle this as error or ignore white space in the middle of token.
wchar_t in;
std::wstring seq;
std::vector<int> input;
std::wcout << L"Enter values : ";
while (std::wcin >> std::noskipws >> in)
{
if (L'\n' == in || (L',' == in))
{
if (!seq.empty()){
try{
input.push_back(std::stoi(seq));
}catch (std::exception e){
std::wcout << L"Bad input" << std::endl;
}
seq.clear();
}
if (L'\n' == in) break;
else continue;
}
seq.push_back(in);
}
std::wcout << L"Values entered : ";
std::copy(begin(input), end(input), std::ostream_iterator<int, wchar_t>(std::wcout, L", "));
std::cout << std::endl;
#include<bits/stdc++.h>
using namespace std;
int a[1000];
int main(){
string s;
cin>>s;
int i=0;
istringstream d(s);
string b;
while(getline(d,b,',')){
a[i]= stoi(b);
i++;
}
for(int j=0;j<i;j++){
cout<<a[j]<<" ";
}
}
This code works nicely for C++ 11 onwards, its simple and i have used stringstreams and the getline and stoi functions
You can use scanf instead of cin and put comma beside data type symbol
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a[10],sum=0;
cout<<"enter five numbers";
for(int i=0;i<3;i++){
scanf("%d,",&a[i]);
sum=sum+a[i];
}
cout<<sum;
}
First, take the input as a string, then parse the string and store it in a vector, you will get your integers.
vector<int> v;
string str;
cin >> str;
stringstream ss(str);
for(int i;ss>>i;){
v.push_back(i);
if(ss.peek() == ','){
ss.ignore();
}
}
for(auto &i:v){
cout << i << " ";
}