Thread: infinite loop problem

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    57

    infinite loop problem

    wondering if anyone could help, i have a program that is supposed to take in characters from a text file and store them in a two demensional array so that i can store them as words. i use a while loop using the isalnum function to store them. this while loop using the isalnum function seems to be where the infinite loop occurs. anyways, heres the code......can anyone see why this is happening?

    Code:
    #include <iostream>
    #include <fstream>
    #include <stddef.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string>
    
    using namespace std;
    
    class search
    {
    public:
        bool check();  //declare the function check
        void output(); //declare the function output
        void input(search sight, char *file);
    	void input2(search sight, char *file);
        
    private:
        int total;//variable for total number of strings
    	int total2;//variable for number of characters in temp array
    	int i;//variable for number of strings in array
    	int j;//variable for number of characters in a string
    	int m;//variable for number of characters in temp string
    	int n;//variable for the insert loop
    	int s;//variable for ?
    	int a;
        ifstream in_stream; //create an instream
    	ofstream out_stream;//creat an outstream
    	char array[1000][30];//create an array
    	char temp[1000][30];//create an array
    	char system[3000][30];
    	char next;//variable for next character in
        
    	
    };
    
    void main()
    {
    	char *name = "text1.txt";
        search sight1;
        sight1.input(sight1, name);
    
    }
    
    void search::input(search sight, char *file)
    {
    s = 0;
    m = 0;
    total2 = 0;
    total = 0;
    i = 0;
    j = 0;
    in_stream.open(file);
    in_stream.get(next);
    while(! in_stream.eof())//while next is not end of file
    {
         //invariant: next is not the end of the file and next is a part of the file
    	
    		if(ispunct(next))
    			in_stream.get(next);
    		if(isspace(next))//if next is space
    		{
    			in_stream.get(next);//get next character from file
    			array[i][j] = '\0';//put null to end current string
    		    j = 0;//bring number of characters back to zero
    			i++;//start a new string
    			total++;//add one to total number of strings
    		}
    		else//if next is not a space
    		{
    			while(isalnum(next))
    			{
    			    in_stream.get(next);
    				cout << next;
    }       }   }
    }
    Last edited by Gil22; 03-07-2003 at 11:45 AM.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Read this then edit your post accordingly
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    57
    ok fixed it......can anyone help now? i narrowed it down to my header declerations. if i change "include# fstream to include fstream.h it works but i need the fstream for other things in my program.
    Last edited by Gil22; 03-07-2003 at 12:23 PM.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You code as it was wouldn't compile for me. I removed the first parameter from the input() function, making it:
    >>void input(char *file);
    and changed the call accordingly:
    >>sight1.input(name);

    Now it compiles for me. But...

    >>in_stream.open(file);
    You didn't check that this worked. You must make sure the file is actually open before using it.

    >>while (!in_stream.eof())
    Read this

    Fix these and see how you go. I expect your loop is caused the file open failing though.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  2. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  3. weired problem in while loop
    By avisik in forum C Programming
    Replies: 14
    Last Post: 12-01-2008, 01:41 PM
  4. Problem with a loop
    By wiggsfly in forum C Programming
    Replies: 5
    Last Post: 11-30-2008, 12:45 PM
  5. Infinite loop problem
    By Xanth in forum C++ Programming
    Replies: 2
    Last Post: 02-20-2005, 12:08 AM