spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Reading numbers from textfile into array


You are not reading a into your numbers, Change your code to this:

if (!inputFile){
    //error handling
    cout << "File can't be read!";
}
else{
    int a;
    while (inputFile >> a){
        //Should loop through entire file, adding
the index to the array
        for(int i=0; i<a; i++){
            DataFromFile [i] = a; // fill array
        }
    }
}

If you are looping through the file, the array will be overwritten with the new number everytime. This is probably not what you intended to do. You probably wanted to fill out 100 locations with 100 different number? In that case, use the following code:

if (!inputFile){
    //error handling
    cout << "File can't be read!";
}
else{
    int i = 0;
    while (inputFile >> a){  // Whilst an
integer is available to read
        DataFile[i] = a;   // Fill a location with
it.
        i++;               // increment index
pointer
    }
}

Categories : C++

Related to : Reading numbers from textfile into array
Reading a txt file, and putting each word into an array, without punctuation or numbers
Do a .replaceAll(regex, "") which will replace all matching characters with the contents of the second parameter (an empty string in this case). Matching is done with a regular expression. Second step is to use .split(regex) on your returned string from replaceAll, and split the string on space ( .split(" ") ). This will return an array with all the words separate. An easy way to write regular e

Categories : Java
Find triplets in an array such that sum of two numbers is also a number in the given array
At first glance, I can't see an algorithm that is less than O(n²). (Unless it is an extra-ordinarily clever one) This problem is very similar to the infamous 3SUM question: Basically, the idea is that given a set of n elements, are there any triples that sum to zero? An algorithm solving 3SUM faster than O(n²) is not known - This is an open problem in computer science... (See here: http://en.

Categories : Arrays
Java reading .txt file to array
Tip #1: Initialize the array with 0. That way, when you process the file, all you have to worry about is to replace the index locations with the digits obtained from your file. Tip #2: You have to do some repeated division by 10 and modulus operation to extract the digits from the number (or binary shift if you prefer). For example, to split the the digits from '27', you can do 27 % 10 (7) an

Categories : Java
How to add numbers between two string array in c
Edit This Answer does not allow carry overs but infinity long add operations. It does not solve the problem of the user. But it is an implementation example and the user asked for one. This is why I will let the answer stay here and not delete it. You can use atoi (ascii to int) Do you realy mean C or C++? This code can't calculate 8+3 = 11 but 5+3 = 8. There is no carry over. int temp; const

Categories : C
Reading from an Input File to an Integer Array
When you call Scanner.nextInt() it only consume the int, and it leaves any trailing whitespace or newline there. Instead you might use something like, Scanner inputFile = new Scanner(hotlist); while (inputFile.hasNext()) { customerName[i] = inputFile.nextLine(); System.out.println(customerName[i]); String custId = inputFile.nextLine(); customerID[i] = Integer.parseInt(custId);

Categories : Java
Recently Add
VS2013 - Error with multiple includes of same header
Multiplying two large integers stored as integer vectors
How Do I Create a max Functor?
Eigen sparse matrix multiplications seem to compute full matrix
zlib compress() produces awful compression rate
How to delete a window?
passing variable without a name to function
OMNeT++ disassembling received messages
How do I ignore certain file types with cppcheck?
Posting a message to UI from business logic
STL algorithm/functional
su -c replacement using c++
How to use a sdk (non-ros header/lib/binaries) in ros package?
using std::chrono::high_resolution_clock to write a frame 30 times per second
Extracting ints and shorts from a struct using AVX?
Finding Bridges in a graph C++ (BOOST)?
Qt Creator cannot find library with custom cmake step
running visual studio release build .exe file in different machines
class oop realions cpp
Declare interface and Implement the interface using multi-inheritance in C++
How to access all elements of boost::multi_index::multi_index_container<> by their indices?
Understanding what a double pointer to a reference does C++
ld: symbol(s) not found for architecture x86_64 when compiling c++
Disconnected node during Graph traversal
Const behavior in operator >>
Structuring C++ Application (directory and folders)
Sendkey Function Enter Key Issue
Why the memory do not release after funciton call?
Passing C++ functions as arguments without function pointer/inlined?
How to create multiple objects inside of for loop with different names?
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.