Solved

Insert integers into an array

Posted on 2003-10-29
10
385 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
Industry Leaders: 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

734 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