get several integer numbers with c++-CIN and put them into array - c++

i need to get couple integer numbers and put them into an array in c++.
assume the count of numbers in CIN are the same count as array length.
int numbers[10];
cin>>numbers;
In fact i want to enter 10 numbers to cin within one line somehow it automatically allocates the numbers to array. how should i do it?

Here is a C++11 solution using std::vector and std::copy_n.
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
int main()
{
std::vector<int> v;
std::copy_n(std::istream_iterator<int>(std::cin), 3, std::back_inserter(v));
}

You can use a loop:
#include <iostream>
int main()
{
int numbers[10];
for (int i = 0; i < 10; ++i)
std::cin >> numbers[i];
}
UPDATE:
If it has to be one line then you could use this (somewhat clumsy solution):
#include <iostream>
int main()
{
int numbers[3];
std::cin >> numbers[0] >> numbers[1] >> numbers[2];
}
Alternatively, you can use std::cin.getline and then parse the string.
UPDATE (again):
#include <iostream>
int main()
{
int numbers[3];
int* input = numbers;
while (std::cin >> *input++ and input != 3 + numbers) ;
}

Related

Changing size value for an array c++

Making a program that reads integers from a file and creates an array, I have that part completed however I am trying to figure out how to change the SIZE value depending on how many ints are in the file. This file has 15 integers but another file may have more and this array will not take in all the ints.
using namespace std;
const int SIZE = 15;
int intArray[SIZE];
void readData(istream& inFile) {
for (int i = 0; i < SIZE; i++){
inFile >> intArray[i];
cout << intArray[i] << " ";
}
}
int main() {
ifstream inFile;
string inFileName = "intValues.txt";
inFile.open(inFileName.c_str());
int value, count = 0;
while(inFile >> value){
count += 1;
}
cout << count;
readData(inFile);
return 0;
}
As you can see I have a while loop counting the number of ints in the file however when I assign that to the size value I was running into many different issues.
A fixed-sized array simply cannot be resized, period. If you need an array whose size can change at runtime, use std::vector instead.
More importantly, you are reading through the entire file just to count the number of integers, and then you are trying to read the values from where the previous loop left off. You are not seeking the ifstream back to the beginning of the file so you can re-read what you have already read.
Try something more like this instead:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
int main() {
string inFileName = "intValues.txt";
ifstream inFile(inFileName.c_str());
int value, count = 0;
while (inFile >> value){
++count;
}
cout << count;
std::vector<int> intArray;
intArray.reserve(count);
inFile.seekg(0);
while (inFile >> value){
intArray.push_back(value);
cout << value << " ";
}
// use intArray as needed...
return 0;
}
Alternatively, don't even bother counting the integers, just let the std::vector grow as needed, eg:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
int main() {
string inFileName = "intValues.txt";
ifstream inFile(inFileName.c_str());
vector<int> intArray;
int value;
while (inFile >> value){
intArray.push_back(value);
cout << value << " ";
}
// use intArray as needed...
// you an get the count from intArray.size()
return 0;
}

no matching function for call to ‘begin(int [n])’

I tried many things but still, it's giving an error:
no matching function for call to ‘begin(int [n])’
What's the best approach?
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n;
cin >> n;
int arr[n];
for(int i = 0; i < n; i++){
cin >> arr[i];
}
reverse(begin(arr), end(arr));
for(int j = 0; j < n; j++){
cout << arr[j] <<" ";
}
return 0;
}
error:no matching function for call to ‘begin(int [n])’
This is because of that you have used non-standard Variable Length Array, here:
cin >> n;
int arr[n] ;
Therefore, it's not possible to apply the standard algorithms like std::reverse to this kind of non-standard arrays.
if you change it normal array with a size, like:
const int n = 3;
int arr[n] ;
The code what you wrote is valid and will work. See here
However, now you can not input the size of the array.
Whats the best approach?
Use std::vector instead.
Now you also have the option for reverse printing without using std::reverse, but using std::vector::reverse_iterator.(If that's all you wanted)
For instance: See output here
#include <vector>
#include <iostream>
#include <algorithm>
int main()
{
int n;
std::cin >> n;
std::vector<int> arr(n);
for(int& ele: arr) std::cin >> ele;
//std::reverse(std::begin(arr), std::end(arr));
//for(const int ele: arr) std::cout << ele << " ";
//or simply
std::for_each(arr.rbegin(), arr.rend(),[](const int ele){ std::cout << ele << " "; });
return 0;
}

C++ Using std::getline in place of cin >>

In a problem were i have to take n number of strings as input and count the ones containing a given substring(case insensitive).
Here's my code:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include<string>
using namespace std;
int main()
{
std::string str2 = "hello";
std::string str3 = "HELLO";
short int n,count=0,i;
cin>>n;
std::string str1[n];
for(i=0;i<n;i++)
{
std::getline(std::cin,str1[i]); //here getline is taking only n-1 inputs
std::size_t found1=str1[i].find(str2);
std::size_t found2=str1[i].find(str3);
if(found1!=std::string::npos || found2!=std::string::npos)
count++;
}
cout<<count;
return 0;
}
Since i cant use cin as string includes spaces or cin.getline() as have to use string type in place of char[].
Problem with my code is std::getline() is only taking n-1 inputs.Cant figure out why?
The first getline after cin reads the remainder of the line, which is probably empty. This is why when reading user input, it is usually better to use getline and process input using code.
After cin >> n the input stream is positioned just after the number n. You can use a getline just to read the newline and then throw it away to position to the start of the next line.
This code should work
#include <iostream>
#include <string>
using namespace std;
int main() {
int n = 0, count = 0;
cin >> n;
do {
string str;
getline(cin, str);
if (str.find("HELLO") != string::npos || str.find("hello") != string::npos) {
count++;
}
} while (n-- && n >= 0);
cout << count << endl;
return 0;
}

Why is getline taking one extra line?

why the for loop execute m-1 times instead of m.
I have used getline() to enter string instead of cin>>.
Here is my code.
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
#include <cstdio>
using namespace std;
int main() {
int n;
int m;
int a;
cin >> n >> m;
int arr[10000];
for(int i = 0; i < n; i++) {
cin >> arr[i];
}
char s[200];
ostream_iterator<int> screen(cout," ");
for(int i = 0; i < m; i++) {
cin.getline(s,20);
int p = s[2]-48;
cout << p << endl;
}
}
Because this:
cin>>n>>m;
Has not read the end of line character from the first line.
Thus the first time through the loop.
cin.getline(s,20);
Will read an empty line.
PS. Prefer to use the string reading version of getline(). That way you guarantee that you can always read a full line.
After last cin>>arr[i], there is a newline remaining in the stream. So the new line will be assigned to s at the first iteration in the for loop without your input, so it looks like the for loop only iterates m-1 times. See this link for the explanation and solution.

get list of numbers from stdin and tokenize them

How would I get a list of numbers from the user and then tokenize them.
This is what I have but it doesn't get anything except for the first number:
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
int main()
{
string line = "";
cin >> line;
stringstream lineStream(line);
int i;
vector<int> values;
while (lineStream >> i)
values.push_back(i);
for(int i=0; i<values.size(); i++)
cout << values[i] << endl;
system("PAUSE");
return 0;
}
Related Posts:
C++, Going from string to stringstream to vector
Int Tokenizer
Here is probably the easiest way to read values from cin into a container:
#include <iostream>
#include <iterator>
#include <vector>
int main()
{
std::vector<int> values;
std::copy(
std::istream_iterator<int>(std::cin),
std::istream_iterator<int>(),
std::back_inserter(values));
// For symmetry with the question copy back to std::cout
std::copy(
values.begin(),
values.end(),
std::ostream_iterator<int>(std::cout,"\n"));
}
I believe cin >> breaks on whitespace, which means you're only getting the first number entered.
try:
getline(cin, line);
Like Donnie mentioned cin breaks on whitespace, so do overcome this we can use a 'getline()', the following example works nicely:
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
int main()
{
string line = "";
::getline(std::cin,line,'\n');
std::stringstream lineStream(line);
int i;
std::vector<int> values;
while (lineStream >> i)
values.push_back(i);
for(int i=0; i<values.size(); i++)
cout << values[i] << endl;
system("PAUSE");
return 0;
}
on top of main
string line = "";
getline (cin, line );
stringstream lineStream(line);
Yep, and is the string version of getline, no the istream one.
OK: Pavel Minaev has the best answer.
But all the people mentioning that cin breaks on white space.
That is a good thing (because it also ignores white space);
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
int main()
{
int i;
vector<int> values;
// prefer to use std::copy() but this works.
while (std::cin >> i)
{
values.push_back(i);
}
// prefer to use std::copy but this works.
for(vector<int>::const_iterator loop = values.begin();loop != values.end();++loop)
{
cout << *loop << endl;
}
return 0;
}