Thread: file input program

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    2

    file input program

    I am trying to get my program to read a file. The file looks like:
    15
    63.4
    66.1
    67.2
    58.5
    61.9
    68.2
    65.3
    70.0
    72.9
    71.8
    70.1
    70.3
    69.5
    66.1
    70.8
    5
    56.5
    57.1
    55.0
    58.0
    66.2
    and continues.
    I can get my program to read the first 15 numbers, but nothing afterwards. I don't know how to make my program read every new header. I used a for loop to read the first 15 numbers. I tried to use a nested for loop to read all of the other numbers, but it is not working right. Can anyone please assist?

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    A loop is certainly what you need, not a for-loop though. Why don't you show us your code, the one you tried to expand to read the whole file but failed, we may be able to assist.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Oct 2017
    Posts
    2
    I am required to write this program with a nested for loop. Is it possible to do that?

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Of course it is, but it won't function as a for-loop. For example:
    Code:
    for (; fscanf(infile, "%d", &amount) == 1;) {
        // Read following numbers
    }
    Last edited by GReaper; 10-15-2017 at 08:04 PM.
    Devoted my life to programming...

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by c0vergal View Post
    I am required to write this program with a nested for loop. Is it possible to do that?
    I hardly think instructors give out imposable assignments, rather they give out assignments that require deep thought at times in order to get the assignment to work, so he or she can see how your brain works, and perhaps help you to develop it to work properly in the contexts of logical thought.

    logic boards ( logic flow), that is a big part of programming. if I do this and that happens now what do I do, but if something different happens what do I do then? logic.

    along with,
    what steps are needed to take place before execution of program, then what steps are needed to be preformed during execution of program,then what steps need to take place in order for execution of program to be completed? which bleeds back to the second part, and can be considered redundant.

    then break that down into functions, where it applies. blocks of code that do something then turn it into a function so it completes a step that needs to take place within your program.

    look at what you got to work with, functions in headers ( api ) to see what they do and what is the return value of the function. Study other peoples code and see what they did and how they did it. It is like listening to other peoples music and then drawing from that to write your own music.

    how to put on my shoes and socks.

    what data types do I need to do that?

    struct socks;
    struct shoes;

    ( then they build on that struct and made it into a class, adding its own functions to go with the data types) but still either way it now can be any type of sock and shoe. (ie, shoes color: brown, black, blue. shoes size, big, small, really small: socks type: argyle, sports, ankle socks, socks color, blue, yellow, black, advanced programming)

    What steps are needed to take place in order for that to be competed, and in what order do they need to be executed before I can move on to the next step and end up with a proper put on set of socks and shoes on my feet line of applied thought?
    Last edited by userxbw; 10-16-2017 at 06:44 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-30-2014, 11:07 PM
  2. Reading input from a file. Program crashes
    By inu11byte in forum C Programming
    Replies: 2
    Last Post: 11-10-2012, 11:31 PM
  3. Replies: 6
    Last Post: 05-08-2009, 08:51 PM
  4. Program not reading file input :?
    By FoxeySoulSLayer in forum C++ Programming
    Replies: 1
    Last Post: 04-14-2009, 02:55 PM

Tags for this Thread