Solved

Insert integers into an array

Posted on 2003-10-29
10
378 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
Comment
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

by:bobbit31
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

by:n_fortynine
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

by:EarthQuaker
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:killer455
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

by:EarthQuaker
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

by:EarthQuaker
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

by:killer455
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

by:bobbit31
ID: 9647481
x = 123... didn't you try it?
0
 
LVL 4

Accepted Solution

by:
n_fortynine earned 25 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

by:n_fortynine
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

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

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

Join & Ask a Question