null and int arrays

This is a discussion on null and int arrays within the C++ Programming forums, part of the General Programming Boards category; Hey everyone I am brand new to this board and still a novice at c++ Basically I am at my ...

  1. #1
    Village id10t
    Join Date
    May 2008
    Posts
    57

    Question null and int arrays

    Hey everyone

    I am brand new to this board and still a novice at c++

    Basically I am at my wits end. I am trying to store integers in an array. The source of these integers is a file. Now i get the numbers(from the file) stored in the array but it seems that i doesn't know when the numbers are done in the file. after the last number it just keeps going.

    why wont while(Numbers[i]!='/0') work? ( I tried it but to no avail)

    Please assist!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It would work if you mark the end of the list with a 0 [and assuming that zero is not a "useful" number in your list].

    Of course, if you do something like this:
    Code:
    void readNumbers(istream fin, int arr[])
    {
       int i;
       for(i = 0; fin >> arr[i]; i++);
    }
    the content of arr[i] will be undefined at the end of that loop.
    The following should work:
    Code:
    void readNumbers(istream fin, int arr[])
    {
       int i;
       for(i = 0; fin >> arr[i]; i++);
       arr[i] =0;
    }
    And finally, '\0' and 0 are essentially the same thing, but '\0' is technically a char, so comparing an integer to a char is not "the correct thing to do" in general. Just use zero.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,777
    I think you need to show a little code about how you're going about this because what you're trying to explain makes no sense.
    You can read from a file until you hit eof (eof bit will be set in the iostream objects), so you know when there's no more data in the file.
    I don't know how you're reading into the array either.

    Code:
    while(Numbers[i]!='/0')
    Is also incredibly wrong. Only C-strings are null terminated, and the null char is \0, not /0. "/0" is, in fact, TWO characters (3 if you count the null char at the end), not one.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Code:
    while(Numbers[i]!='/0')
    Is also incredibly wrong. Only C-strings are null terminated, and the null char is \0, not /0. "/0" is, in fact, TWO characters (3 if you count the null char at the end), not one.
    Ah, good catch, I didn't spot the wrong way slash - although, as it's still got single quotes, it's actually a 'multicharacter constant', so it's value is 0x2F, 0x30 (combined in whatever byte order the target system uses).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Village id10t
    Join Date
    May 2008
    Posts
    57
    As per Elysia's request

    Code:
     
    #include <fstream>
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
    
    ifstream in_stream1;
    
    int Numbers[50],i=0;
    
    
    in_stream1.open("InputFile1.dat");
    
    if (in_stream1.fail())
    {
    cout<<"Error opening file."<<endl;
    exit(1);
    }
    
    
    while((Numbers[i]=!'\0')){
    in_stream1>>Numbers[i];
    i++;
    }
    
    i=0;
    while(i<50){
    cout<<Numbers[i]<<endl;
    i++;
    }
    
    in_stream1.close();
    
    
    return 0;
    }

    The contents of the file is as follow:

    3 8 12 15 21 25 31 39 49 63

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Certainly, numbers[i] will not be set to '\0' when you reach the end of file - it will just be whatever happens to be on that location.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Just forget about the idea about '\0'.

    The easiest way is to a) use a vector if you don't know how large the array must be, and b) rely on the knowledge that if input fails (mostly because of reaching the eof), your ifstream evaluates to false.

    Code:
    ifstream in_stream1("InputFile1.dat");
    //error detection
    
    vector<int> Numbers;
    int input;
    while (in_stream1 >> input) {
        Numbers.push_back(input);
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Village id10t
    Join Date
    May 2008
    Posts
    57
    Waht is the difference between Vectors and Dynamic arrays?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MarlonDean View Post
    Waht is the difference between Vectors and Dynamic arrays?
    What I would call "dynamic arrays" is when you manually allocate and/or reallocate memory for an array. Vector does that too, but it's done behind the scenes so you don't need to worry about it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Village id10t
    Join Date
    May 2008
    Posts
    57
    Thank you everybody!!! it works, it works!!! bwahahahaha

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21