Solved

Insert integers into an array

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

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now