Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Insert integers into an array

Posted on 2003-10-29
Medium Priority
392 Views
Last Modified: 2010-04-02
If a file contains:
123
234
124

--------------------------------------------------------------------------

ifstream input;
input.open(filewithdata);

while(input >> x) {
Array[arrayCount] = x;
arrayCount++;
}

Would this input the numbers correctly? Considering the arraycount is specified already.

A[0]=123
A[1]=234
A[2]=124

0
Question by:killer455
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions
• 3
• 3
• 2
• +1
10 Comments

LVL 18

Expert Comment

ID: 9646640
ifstream input;
input.open(filewithdata);

int x;
int arrayCount = <numElements>;

for (int i=0;i<arrayCount;i++) {
input >> x;
A[i] = x;
}
0

LVL 4

Expert Comment

ID: 9646660
you should consider using a vector<int> since you don't know how many ints you're getting. Could be quite a lot and this is the probably the best way to forget about size.
Another simpler way which is less versatile, is to create a big array (you guess how many ints there will be)
#define MAX_SIZE 200
int arr[MAX_SIZE];
for(int index = 0; index < MAX_SIZE && input >> arr[index]; ++index);
0

LVL 3

Expert Comment

ID: 9646713
The simpliest way is :

#include <algorithm>
#include <vector>
#include <fstream>

vector<int> numbers;
ifstream in("bla.dat");
copy(istream_iterator<int>(in), istream_iterator<int>(), back_inserter(numbers));
0

Author Comment

ID: 9646841
n_fortynine,

Actually I will know how many ints there will be.
I was basically wondering if it would take whole number, say "123" in.  Or would it take "1" first and then "2", then "3".  Since its coming from a file would the 'input >> x' take in the entire line or only a single digit at a time?
0

LVL 3

Expert Comment

ID: 9646924
"input >> x" will read from input into x.

If x is an int, it'll try to read an int ( 123, 24, -234, -12123, 0 ).
If x is a string, it'll try to read a word ( 123, haha, hqw23, w00t )
if x is a double, it'll try to read a double ( 123, 12.23, -23.513 )
if x is a char, it'll try to read a char ( '1', 'a', 'x' )

ETC

If the input doesn't match the type you're supposed to input to, it fails and input.fail() returns true.

e.g :

int x;
if(cin >> x)
cout << "input ok" << endl;
else
cout << "input NOT ok" << endl;

Then try to input "asd" or "-123" and look which one fails.
0

LVL 3

Expert Comment

ID: 9646930
to get a line from a stream, use getline :

string line;
getline(cin, line); // reads a line from standard input
0

Author Comment

ID: 9647024
Well im saying that if they are ints.  Like this file:

123
324
124

Will input >> x;   result in x=123 or x=1?
0

LVL 18

Expert Comment

ID: 9647481
x = 123... didn't you try it?
0

LVL 4

Accepted Solution

n_fortynine earned 100 total points
ID: 9648252
killer445, this is the basic thing about ">>": depending on what your variable type is it's going to get you the info you need. For example:
int x; //double or float or any *numeric* type
input >> x; //will get you the integral part up to the first occurence of a non-numeric
//character.
For example:
123243df,2545453ds
it will get you 123243 and stops at d. (try doing input >> x several times, input will refuse to get in the next integer until you've taken care of the non-numeric characters.

then, say you have
char ch;
input >> ch; //will get you the next character in the buffer and advance the pointer.

For example:
123243d 254 5453
int x;
char ch;
input >> x; //x = 123243
input >> ch; //ch = 'd'
input >> x; //x = 254 notice the space is skipped
input >> ch; // ch = ' '
input >> ch; //ch = '5'
input >> ch; //ch = '4'
input >> x; //x = 53

Hope that helps
0

LVL 4

Expert Comment

ID: 9648263
>>input >> ch; // ch = ' '
My mistake, >> ignores all spaces :) so the last 4 lines would be
input >> ch; // ch = '5'
input >> ch; //ch = '4'
input >> ch; //ch = '5'
input >> x; //x = 3
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. Aâ€¦
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [sâ€¦
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relatâ€¦
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Botâ€¦
###### Suggested Courses
Course of the Month8 days, 13 hours left to enroll

#### 721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.