Reading Numbers from files...

This is a discussion on Reading Numbers from files... within the C++ Programming forums, part of the General Programming Boards category; quzah dejavu to you too all you do is complain about people i can honestly say ive looked at everything ...

  1. #16
    Self-Taught Noob
    Join Date
    Jan 2005
    Location
    Ohio
    Posts
    38
    quzah dejavu to you too

    all you do is complain about people i can honestly say ive looked at everything you shown me so far and i dont get it and so i post ive tried coding didnt get how so i post

    how the hell can i code something then post if i dont know how
    No one died when Clinton lied.

    Compiler: Borland C++ Builder
    OS: Windows XP

  2. #17
    Registered User
    Join Date
    Aug 2003
    Posts
    1,209
    The problem here is, he says he has tried and so on but he doesnt show us what he tried. for all I know it could be a Hello World app he has tried with.

  3. #18
    Self-Taught Noob
    Join Date
    Jan 2005
    Location
    Ohio
    Posts
    38
    well ive used them in actual apps and i know what im talking about for the most part. maybe im just in bad posts but quzah always is like that
    No one died when Clinton lied.

    Compiler: Borland C++ Builder
    OS: Windows XP

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by GamingMarvel
    quzah dejavu to you too
    Go look it up. You apparently have no idea what the term means.


    Quote Originally Posted by GamingMarvel
    all you do is complain about people i can honestly say ive looked at everything you shown me so far and i dont get it and so i post ive tried coding didnt get how so i post

    how the hell can i code something then post if i dont know how
    Yeah and I've looked at everything you've shown me so far, which is exactly jack I am sillyI am sillyI am sillyI am silly. Go read the forum rules. If you're stuck on code, post your attempt. We're not here to do everything for you.

    No, wait, it gets better:
    Quote Originally Posted by GamingMarvel
    well ive used them in actual apps and i know what im talking about for the most part. maybe im just in bad posts but quzah always is like that
    So you've "used them in actual apps" and... "i know what im talking about for the most part", but some how we get: "how the hell can i code something then post if i dont know how"

    Make up your mind. Which is it? You do or you don't know how?

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #20
    Self-Taught Noob
    Join Date
    Jan 2005
    Location
    Ohio
    Posts
    38
    okay let me specify myself. I have the basic idea of the things you are suggesting and i thank you for it. But i'm also saying that i've tried the code you gave me or another user has. and it works but not for what i need. From there on I'm confused on how to change it to work. I gave you something specific to go by and I don't see the point in reposting correct code. I'm also sorry if I've turned this post into a war zone that's not what i intended at all. I apologize I'm just a little frustrated because I'm tryin to do something that sounds really simple but i can't either understand or no one else seems to know understand or can provide the code and give me the required understanding.

    Oh and after words i realized i screwed up when i used the word Dejavu. Yes I know what it means. It's like reliving something that you have or thought you have lived once. It's French.
    Last edited by GamingMarvel; 01-27-2005 at 09:26 AM.
    No one died when Clinton lied.

    Compiler: Borland C++ Builder
    OS: Windows XP

  6. #21
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    This isn't C99, this is C++, I thought it was customary to NOT put void in the main:
    Quote Originally Posted by prog-bman
    Just made a few improvements on yours andy.
    Code:
    #include <iostream>
    #include <fstream>
    
    int main(void)
    {
      
      int i = 0, j;
      int number[10];  
      std::ifstream myFile;
      myFile.open("file.txt");
      
      if(!myFile)//file doesn't exist or some other reason for it failing
      {
        
        std::cout<<"Could Not Open file.txt"<<std::endl;
        std::cin.get();
        return 0;
        
      }
      
      while(myFile>>number[i] && i < 10)//input numbers in our file only up to 10 numbers
      {
        
        i++;
        
      }
      
      for(j = 0; j < i; j++)//only printing the numbers that we read in
      {
        
        std::cout<<number[j]<<std::endl;
        
      }
      std::cin.get();    
      return 0;  
      
    }

  7. #22
    Registered User
    Join Date
    Aug 2003
    Posts
    1,209
    It doesnt matter
    Code:
    int main(void) == int main() // in C++
    I believe using empty parenthese is the same as saying that no arguments should be taken.
    Correct me if im wrong.

  8. #23
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Yeah but progman was correcting somebody else's code, and I believe sticking a void in there isn't doing anything! And I believe that we all should agree on int main():

    http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    For no args in C++

  9. #24
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    The standard
    Change: In C + +, a function declared with an empty parameter list takes no arguments.
    In C, an empty parameter list means that the number and type of the function arguments are unknown"
    Example:
    int f(); // means int f(void) in C + +
    // int f(unknown) in C
    Rationale: This is to avoid erroneous function calls (i.e. function calls with the wrong number or type of
    arguments).
    Effect on original feature: Change to semantics of welldefined
    feature. This feature was marked as
    “obsolescent” in C.
    Difficulty of converting: Syntactic transformation. The function declarations using C incomplete declaration
    style must be completed to become full prototype declarations. A program may need to be updated
    further if different calls to the same (nonprototype)
    function have different numbers of arguments or if the
    type of corresponding arguments
    Woop?

  10. #25
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Yes exactly, for C programming we go like this:
    Code:
    int f(void);
    But the all-new C++ way is:
    Code:
    int f();
    So that's why I believe we should just use:
    Code:
    int main()
    for C++ programs, instead of being consistent! omg I don't know why I started this arguement, I'm an idiot!! *cries* IT DOESN'T EVEN MATTER
    |
    |
    |
    v

  11. #26
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    I was just saying my method is ok. Its all a style thing like alot of stuff
    Woop?

  12. #27
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I believe using empty parenthese is the same as saying that no arguments should be taken.
    Correct. C++ supports the C method of using void to specify no arguments. Also, in the case of main (which is rarely declared anymore, only defined), the following definitions are identical in both C and C++:
    Code:
    int main()
    int main(void)
    Though for consistency, the former is preferred in C++ for all declarations and definitons, and the latter is preferred in C. Following that guideline keeps things consistent in both languages and protects against problems concerning K&R style function declarations in C.
    My best code is written with the delete key.

  13. #28
    Registered User kotzy's Avatar
    Join Date
    Jul 2005
    Posts
    5
    Quote Originally Posted by prog-bman
    Just made a few improvements on yours andy.
    Code:
    while(myFile>>number[i] && i < 10)//input numbers in our file only up to 10 numbers
    {
        i++;
    }
    though i have problems with files and streaming, i managed to write this code. but that only reads numbers if there are ONLY numbers in a file. Well, the data in my text file represents kind of a MxM table of numbers which are divided by comma.
    I believe that i only need one more >> NONnumberCHAR after the while(myFile>>number[j]...).

    So it would look something like this:

    Code:
    int array[30];
    char NONnumberCHAR[];
    while ((myFile>> number >> NONnumberCHAR) && (j < 30))
    {
           array[j] = number;
           j++;
    }

  14. #29
    Registered User kotzy's Avatar
    Join Date
    Jul 2005
    Posts
    5
    i got it! and it almost works fine!
    content of my file:
    Code:
    -1,55,25,45,-1,-1,-1,-1
    55,-1,-1,-1,5,-1,-1,-1
    25,-1,-1,-1,40,-1,-1,-1
    45,-1,-1,-1,20,-1,-1,30
    -1,5,40,20,-1,35,15,-1
    -1,-1,-1,-1,35,-1,10,-1
    -1,-1,-1,-1,15,10,-1,50
    -1,-1,-1,30,-1,-1,50,-1
    Code:
    	while (!Graph.eof()) {
    		Graph>> Matrika[j][i]; // Matrika = matrice in which the numbers from the file are saved
    		Graph>> buu; // the comma character is read into buu
    		i++; // for every read, i is increased (i represents columns)
    		if (i == (m)) { // NOTE: m represents the size of the matrice, so when i is equal to m then we need to jump into next row and put i back to zero to begin writing in the first column of the next row
    			i = 0;
    			j++;
    		}
    	}
    as i said, it almost works fine. this is what I get:

    Code:
    -1 55 25 45 -1 -1 -1 -1
    5 -1 -1 -1 5 -1 -1 -1
    5 -1 -1 -1 40 -1 -1 -1
    5 -1 -1 -1 20 -1 -1 30
    1 5 40 20 -1 35 15 -1
    1 -1 -1 -1 35 -1 10 -1
    1 -1 -1 -1 15 10 -1 50
    1 -1 -1 30 -1 -1 50 -1
    NOTE: first character from every line but the first is missing. why,i have no clue? anyone?

  15. #30
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >NOTE: first character from every line but the first is missing. why,i have no clue? anyone?

    > Graph>> buu; // the comma character is read into buu
    So you skip commas, but guess what. There's no comma at the end of a line.

    Graph>> Matrika[j][i]; // Matrika = matrice in which the numbers from the file are saved

    should skip commas automatically, so you should be able to remove that Graph>>buu; line.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question: reading numbers into an array
    By Lince in forum C Programming
    Replies: 15
    Last Post: 11-15-2006, 03:41 AM
  2. pulling numbers from files....
    By Confuzz in forum C++ Programming
    Replies: 3
    Last Post: 09-07-2004, 08:49 PM
  3. Reading selective numbers from a file
    By supaben34 in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2002, 01:56 PM
  4. problem reading files in C
    By angelfly in forum C Programming
    Replies: 9
    Last Post: 10-10-2001, 12:58 PM
  5. Need Advice in reading files
    By jon in forum C Programming
    Replies: 4
    Last Post: 10-07-2001, 08:27 AM

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