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
Solved

Insert integers into an array

Posted on 2003-10-29
10
370 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
  • 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 

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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

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…
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…
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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

808 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