-
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.
-
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.
-
I don't understand your first sentence. I put the statement
int i = 0 in the inner loop.
-
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.
-
OK it has been taken out but it still does not read in anything more than the first line
-
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?
-
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());
}
}
-
Hey have you tried a do..while loop?
-
>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.