Thread: need nelp

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    9

    need nelp

    I have a program that takes in ip addresses and breaks them up into 4 octets which are strings. Then I convert them into integers. I am having a problem with running the loop to take in each address. It only runs once so it takes in only 1 ip address.

    Here is my Code:
    Code:
    	ifstream in("input.txt");
    	ofstream out("output.txt");
    	
    
    
    	string line;
    	string octet1;
    	string octet2;
    	string octet3;
    	string octet4;
    
    	int i = 0;
    
    	while ( ! in.eof())
    	{
    	  while(getline(in,line))
    	  {
    		//out<< "[ " << line << " ]";
    
    		
    
    		while(line[i] != '.')
    		{
    			octet1 = octet1 + line[i];
    			i++;
    		}
    
    		cout<<octet1<<"   ";
    		i++;
    
    		while(line[i] != '.')
    		{
    			octet2 = octet2 + line[i];
    			i++;
    		}
    		cout<<octet2<<"   ";
    		i++;
    
    		while(line[i] != '.')
    		{
    			octet3 = octet3 + line[i];
    			i++;
    		}
    		cout<<octet3<<"   ";
    
    		i++;
    	
    		while(i != line.length())
    		{
    			octet4 = octet4 + line[i];
    			i++;
    		}
    		
    		cout<<octet4<<endl;
    	  }
    
    
    
    	int oct1;
    	int oct2;
    	int oct3;
    	int oct4;
    
    
    	oct1= atoi(octet1.c_str());
    	oct2= atoi(octet2.c_str());
    	oct3= atoi(octet3.c_str());
    	oct4= atoi(octet4.c_str());
    If I put a cout<<oct1 : it does nothing.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    First, remove the relic of the outer while loop, the one with the eof(). Then, reset i inside the loop. Finally, find some place to store the IP addresses as you read them.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    9
    I don't understand your first sentence. I put the statement
    int i = 0 in the inner loop.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    This line:
    while ( ! in.eof())
    is out of place. It's superseded by the next while loop, has no matching brace in your code and is faulty to begin with.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    9
    OK it has been taken out but it still does not read in anything more than the first line

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    9
    Here is my Code as of now:

    Code:
    ifstream in("input.txt");
    	ofstream out("output.txt");
    	
    	
    	string line;
    	string octet1;
    	string octet2;
    	string octet3;
    	string octet4;
    
    	
    
    	  while(getline(in,line))
    	  {
    		
    		int i = 0;
    		
    
    		while(line[i] != '.')
    		{
    			octet1 = octet1 + line[i];
    			i++;
    		}
    
    		cout<<octet1<<"   ";
    		i++;
    
    		while(line[i] != '.')
    		{
    			octet2 = octet2 + line[i];
    			i++;
    		}
    		cout<<octet2<<"   ";
    		i++;
    
    		while(line[i] != '.')
    		{
    			octet3 = octet3 + line[i];
    			i++;
    		}
    		cout<<octet3<<"   ";
    
    		i++;
    	
    
    		while(i != line.length())
    		{
    			octet4 = octet4 + line[i];
    			i++;
    		}
    		
    		cout<<octet4<<endl;
    
    
    	  
    
    
    
    	int oct1;
    	int oct2;
    	int oct3;
    	int oct4;
    
    
    	oct1= atoi(octet1.c_str());
    	oct2= atoi(octet2.c_str());
    	oct3= atoi(octet3.c_str());
    	oct4= atoi(octet4.c_str());
    
               }
    It does everything I want it to do for the first line of the input file but thats it. There about 10 lines of addresses but it only does this for the first one. Why isn't the loop not taking in the file line by line until its done?

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The only problem I can see is that the strings aren't cleared with each new loop iteration. The following works just peachy for me:
    Code:
    #include <cstdlib>
    #include <fstream>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
      ifstream in("input.txt");
      //ofstream out("output.txt");
    
      string line;
    
      while(getline(in,line))
      {
        int i = 0;
        string octet1;
        string octet2;
        string octet3;
        string octet4;
    
        while(line[i] != '.')
        {
          octet1 = octet1 + line[i];
          i++;
        }
        cout<<octet1<<"   ";
    
        i++;
        while(line[i] != '.')
        {
          octet2 = octet2 + line[i];
          i++;
        }
        cout<<octet2<<"   ";
    
        i++;
        while(line[i] != '.')
        {
          octet3 = octet3 + line[i];
          i++;
        }
        cout<<octet3<<"   ";
    
        i++;
        while(i != line.length())
        {
          octet4 = octet4 + line[i];
          i++;
        }
        cout<<octet4<<endl;
    
        int oct1;
        int oct2;
        int oct3;
        int oct4;
    
        oct1= atoi(octet1.c_str());
        oct2= atoi(octet2.c_str());
        oct3= atoi(octet3.c_str());
        oct4= atoi(octet4.c_str());
      }
    }
    My best code is written with the delete key.

  8. #8
    NewB at programming
    Join Date
    Dec 2004
    Posts
    1
    Hey have you tried a do..while loop?

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Hey have you tried a do..while loop?
    I don't see how a do..while loop would be warranted for this problem. They're notorious for breaking on empty files because the programmer was caught unawares.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed