using namespace std

This is a discussion on using namespace std within the C++ Programming forums, part of the General Programming Boards category; couple questions...... 1. what does using namespace std exactly do? 2. does it do something to the way eof works? ...

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

    using namespace std

    couple questions......

    1. what does using namespace std exactly do?

    2. does it do something to the way eof works?
    i had a program that worked when i didnt have using namespace std but i needed to add it for other reasons and now it seems like my eof (end of file) doesnt work and my program just goes into an endless loop. i was using eof because i need to keep getting data from a file until it reaches the eof.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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
    well that makes sense now.......how do i fix it then?

    my while loop looked like this
    while( ! in_stream.eof())
    {
    }

    i'm a pretty new programmer and a lot of that stuff that you sent me to kind of went over my head.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Use the return code from the function doing the reading to determine when eof is reached.

    Post the bit that is doing the reading.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    57
    heres the whole code......the eof while statement takes place in the input function

    Code:
    #include <iostream>
    #include <fstream>
    #include <stddef.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string>
    
    using namespace std;
    
    struct StackFrame
    {
    	string data;
    	StackFrame *link;
    };
    
    typedef StackFrame* StackFramePtr;
    
    
    class search
    {
    public:
        bool check();  //declare the function check
        void output(StackFramePtr top); //declare the function output
        void input(search sight, char *file);
    	void input2(search sight, char *file, StackFramePtr top);
    	int a;
    	int b;
    	int total3;
        
    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 ?
        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 next;//variable for next character in
        
    	
    };
    
    int main()
    {
    	char *name;
        search sight1;
    	sight1.a = 0;
    	sight1.b = 0;
    	sight1.total3 = 0;
    	StackFramePtr top;
    	top = new StackFrame;
    	for(int h = 0; h < 1; h++)
    	{
    		if(h == 0)
    		{
    		   name = "text1.txt";
               sight1.input(sight1, name);
    	       sight1.input2(sight1, name, top);
    		}
    		if(h == 1)
    		{
    			name = "adams.txt";
    			sight1.input(sight1, name);
    			sight1.input2(sight1, name, top);
    		}
    		if(h == 2)
    		{
    			name = "jefferson.txt";
    			sight1.input(sight1, name);
    			sight1.input2(sight1, name, top);
    		}
    		if(h == 3)
    		{
    			name = "madison.txt";
    			sight1.input(sight1, name);
    			sight1.input2(sight1, name, top);
    		}
    	}
    	sight1.output(top);
    	cout << " " << sight1.total3;
    	return 0;
    	
    
    }
    
    void search::input(search sight, char *file)
    {
    for(s = 0; s < 1000; s++)
    {
       for(m = 0; m < 30; m++)
          temp[s][m] = 'c';
       
    }
    for(i = 0; i < 1000; i++)
    {
    	for(j = 0; j < 30; j++)
    	   array[i][j] = 'd';
    	
    }
    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))
    			//invariant: next is either a letter or a number and next is a part of the file
    			{
    				temp[s][m] = tolower(next);//put next into temp array
    			    m++;//go to next place in temp array
    				total2++;//add one to total number of temp characters in temp string
    				in_stream.get(next);//get next character from file
    			}
    		
    			temp[s][m] = '\0';
    			m = 0;//bring back to first character in temp string
    			if(check())//if temp string already in array
    			{
    				in_stream.get(next);//get next character from file
    			    s++;
    			}
                
    	        else//if temp string not already in array
    			{
    				for(n = 0; n < total2; n++)//for every character in temp array
    				//invariant: n is less than total2 and has a place in the array
    				{
    					array[i][j] = temp[s][n];//add temp character to current place in array
    				    j++;//go to next place in current array
    				}
    				array[i][j] = '\0';
    			    in_stream.get(next);//get next character from file
    				i++;//start a new string
    				total++;//add one to total number of strings
    				total2 = 0;//reset number of characters in temp string back to zero
    				s++;
    				if(! in_stream.eof())
    					j = 0;
    			}
    		}
    	
    }
    in_stream.close();
    }
    
    void search::output(StackFramePtr top)
    {
        while(top != NULL)
    	{
    		string result;
    		result = top->data;
    		StackFramePtr temp;
    		temp = top;
    		top = top->link;
    		delete temp;
    		cout << result;
    	}
    
    }
    
    bool search::check()//function defenition
    {
    	int sum = 0;//represents sum of coorect letters
    	int answer = 0;
    	int numstring;//represents string of array
    	int x;
    	int l = 0;
    	int f = 0;
        int totalchar;
    	if(total == 0)
    		return false;
    	for(numstring = 0; numstring < total; numstring++)//loops through all strings in array
    	//invariant: numstring is less than c and a[numstring][f] is part of an array
    	{   
    		totalchar = 0;
            for(f = 0; array[numstring][f] != '\0'; f++)
    		//invariant: a[numstring[f] does not equal a null character and a[numstring][f] is part of an array
    		{
    			if(isalnum(array[numstring][f]))
    				totalchar++;
    		}
            l = 0;
    		sum = 0;
    		x = 0;
    		while((array[numstring][l] != '\0') || (temp[s][x] != '\0'))
    		// a[numstring][l] and b[t][x] are both not null characters and they are both parts of arrays
    		{
    			 if(array[numstring][l] == temp[s][x])
    			 {
    			    sum++;
    				if(temp[s][x] != '\0')
    			      x++;
    				if(array[numstring][l] != '\0')
    				  l++;
    			 }
    		     else
    			 {
    				 if(temp[s][x] != '\0')
    				   x++;
    				 if(array[numstring][l] != '\0')
    			       l++;
    			 }
    		     if((sum == total2) && (x == l) && (totalchar == sum))
    		        answer = 1;
    		}
    		
    	}
    	if(answer == 1)
    		return(true);
        else
    	    return(false);
    	
    }
    
    void search::input2(search sight, char *file, StackFramePtr top)
    {
    	int j;
    	j = 0;/*
    	array[i][j] = '?';//add a question mark to current place in array to signal end of array
        i = 0;//reset current string to first string
    	j = 0;//reset current character to first character
    	top = new StackFrame;
    	top->data = array[i];
    	top->link = NULL;
    	i++;
    	while(array[i][j] != '?')//while not end of array
    	//invariant: array[i][j] is not a question mark and array[i][j] is part of an array
    	{
    		StackFramePtr temp;
    		temp = new StackFrame;
            temp->data = array[i];
    		i++;
    		temp->link = top;
    		top = temp;
    	}*/
    }

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Rather than me deciphering your code, tell me what it is supposed to do. What does the input look like, what are you trying to do with it.

    That input function looks a mess (sorry!). I would suggest you have only one call to the read function (.get() in this case), and control the loop around that. But once I know what you're trying to do, I can advise you better.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    57
    i'm trying to read text from a file and store the words in an array, i then want to to store the words as strings in a linked list so that they point to what files the words appear in. basically a search engine. i'm pretty sure the problem has something to do with me using namespace std and that screwing up the eof while loop i use in the input function

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why std:: and not using namespace std?
    By swappo in forum C++ Programming
    Replies: 4
    Last Post: 07-03-2009, 03:10 PM
  2. std:: or namespace std
    By C-+ in forum C++ Programming
    Replies: 16
    Last Post: 12-04-2007, 11:30 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. need ; before using namespace std
    By wwfarch in forum C++ Programming
    Replies: 7
    Last Post: 05-11-2004, 10:42 PM
  5. Site tutorials
    By Aalmaron in forum C++ Programming
    Replies: 20
    Last Post: 01-06-2004, 01:38 PM

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