Thread: Why wont my LOOP LOOP?

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    4

    Why wont my LOOP LOOP?

    Need some noob help, so go easy.

    I cant seem to figure out why my variable "count" will not increment.

    Any help appreciated!

    -Dave

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    using namespace std;
    
    int main()
    
    
    {
    
    
        int count;
        char Extract_Number[4];
        char File_Name[20];
        
        cout << "Enter File to Read : " ;
        cin >> File_Name;
        count = 1;
        ifstream myfile(File_Name);
        if (myfile.is_open())
      {
        while ( myfile.good() )
    
              { 
                myfile >> Extract_Number;
                cout << "Etracted Number is : " << Extract_Number << endl;
                cout << "The count is : " << count << endl;
                cout << "incrmenting count " ;
                count++ ;
                }}
    return 0;
        
    }

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    IS your file open?....
    Last edited by rogster001; 03-22-2011 at 07:01 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Code:
    1. cin.getlin() // read about it
    2. cout<<File_Name; //check what it is showing.
    3. if (myfile.is_open())
      {
        while ( myfile.good() )
    
              { 
                myfile >> Extract_Number;
                cout << "Etracted Number is : " << Extract_Number << endl;
                cout << "The count is : " << count << endl;
                cout << "incrmenting count " ;
                count++ ;
                }}
    else
    {
        cout<<"File is not found."<<endl;
    }
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    I guess I should have re-titled it "why wont my counter count"?


    The the int count will never increment, the file open/read/print part works fine.

    -Dave

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    1. What is the output you do see? If you are seeing output with that code then the loop is being executed and the count should be incremented however...
    2. The variable count is not initialized.
    3. Your read loop should be more along the lines of:
      Code:
      while(myfile >> Extract_Number) { ... }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    char Extract_Number[4];
    And how many digits are there in the numbers in the file? If they are longer than 3 characters, then you get a buffer overflow, and one possible result would be that the counter variable gets overwritten.
    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).

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    4

    Smile

    Quote Originally Posted by anon View Post
    Code:
    char Extract_Number[4];
    And how many digits are there in the numbers in the file? If they are longer than 3 characters, then you get a buffer overflow, and one possible result would be that the counter variable gets overwritten.

    Right on, that was the problem....

    Ok...Why?

    My number data is text...each a group of four numbers 0021 0046 etc... Why do I only have three places in the array?


    Thanks again,

    -Dave

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Dave Wave View Post
    Right on, that was the problem....

    Ok...Why?

    My number data is text...each a group of four numbers 0021 0046 etc... Why do I only have three places in the array?


    Thanks again,

    -Dave
    You don't. You've got room for four: For the first, second and third digit and for the 0-byte that terminates C-strings. You can store all four characters in there, but many functions won't work with it as there is no 0-byte and the functions don't know the length of the string.

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    ... but many functions won't work with it ...
    ... including cin >> char*, which will try to zero-terminate the string.
    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).

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You need to use an array of std::string, which is a real string, and not a character. For arrays, it is also worth mentioning std::vector.
    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.

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    Thanks for the help, I got it all working now!


    -Dave

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Poll event loop
    By rogster001 in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2009, 04:28 AM
  2. need help with a loop
    By Darkw1sh in forum C Programming
    Replies: 19
    Last Post: 09-13-2009, 09:46 PM
  3. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2009, 11:54 PM
  4. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM