8 Replies - 706 Views - Last Post: 27 January 2010 - 04:17 AM Rate Topic: -----

#1 complexity  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 07-October 08

splitting a text into 2 parts

Posted 27 January 2010 - 12:02 AM

i would like to split a text file into 2 parts where the first letter of the original text file goes to the 1st para and the second letter would go to the 2nd para, the third letter would go back to the 1st para and fourth would go back to 2nd and so on...below is wad i have done and i don't seem to get the results i wanted. instead it just display out the whole text without sorting the orginal text into 2 parts
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

string filename, key, fileline;
fstream myfile;
	
int main (int argc, char * const argv[]) 
{
	cout << "Please enter the ciphertext filename:";
	cin >> filename;
		myfile.open(filename.c_str());
while (!myfile.eof())
		{
			getline (myfile, fileline);
			int p= 2;
			string para1, para2;
			
			for (int i = 0, j = 0; j<fileline.length(); j++)
			{
				if (p = 2)
				{
					if (i = 1)
					{
						para1 = para1 + fileline[j];
						i = 2;
						
					}
					if (i = 2)
					{
						para2 = para2 + fileline[j];
						i = 1;
					
					}
				
				}
				
			}
			cout << para1 << endl;
			cout << para2 << endl;

		}
	return 0;
}



can any kind soul help me with this? thxs alot!

This post has been edited by complexity: 27 January 2010 - 12:13 AM


Is This A Good Question/Topic? 0
  • +

Replies To: splitting a text into 2 parts

#2 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: splitting a text into 2 parts

Posted 27 January 2010 - 12:06 AM

The code you have posted does not compile! Try correcting and resubmit please.
Was This Post Helpful? 0
  • +
  • -

#3 complexity  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 07-October 08

Re: splitting a text into 2 parts

Posted 27 January 2010 - 12:14 AM

View PostMartyn.Rae, on 26 Jan, 2010 - 11:06 PM, said:

The code you have posted does not compile! Try correcting and resubmit please.


ok its edited! it compiles now.
Was This Post Helpful? 0
  • +
  • -

#4 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: splitting a text into 2 parts

Posted 27 January 2010 - 12:25 AM

You are flip between i=1 and i=2 in the loop but i is set to zero.

		   for (int i = 0, j = 0; j<fileline.length(); j++)
			{
				if (p = 2)
				{
					if (i = 1)
					{
						para1 = para1 + fileline[j];
						i = 2;
						
					}
					if (i = 2)
					{
						para2 = para2 + fileline[j];
						i = 1;
					
					}
				
				}
				
			}



This should read

		   for (int i = 1, j = 0; j<fileline.length(); j++)
			{
				if (p = 2)
				{
					if (i = 1)
					{
						para1 = para1 + fileline[j];
						i = 2;
						
					}
					if (i = 2)
					{
						para2 = para2 + fileline[j];
						i = 1;
					
					}
				
				}
				
			}


Hope that helps-

This post has been edited by Martyn.Rae: 27 January 2010 - 12:26 AM

Was This Post Helpful? 0
  • +
  • -

#5 Guest_c.user*


Reputation:

Re: splitting a text into 2 parts

Posted 27 January 2010 - 12:48 AM

if (p = 2)
it is an assignment

use something like this
	for (int i = 0; i < s.length(); i++)
		if ((i+1) % 2 != 0)
			s1 += s[i];
		else
			s2 += s[i];


This post has been edited by c.user: 27 January 2010 - 12:50 AM

Was This Post Helpful? 1

#6 complexity  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 07-October 08

Re: splitting a text into 2 parts

Posted 27 January 2010 - 12:55 AM

View PostMartyn.Rae, on 26 Jan, 2010 - 11:25 PM, said:

You are flip between i=1 and i=2 in the loop but i is set to zero.

		   for (int i = 0, j = 0; j<fileline.length(); j++)
			{
				if (p = 2)
				{
					if (i = 1)
					{
						para1 = para1 + fileline[j];
						i = 2;
						
					}
					if (i = 2)
					{
						para2 = para2 + fileline[j];
						i = 1;
					
					}
				
				}
				
			}



This should read

		   for (int i = 1, j = 0; j<fileline.length(); j++)
			{
				if (p = 2)
				{
					if (i = 1)
					{
						para1 = para1 + fileline[j];
						i = 2;
						
					}
					if (i = 2)
					{
						para2 = para2 + fileline[j];
						i = 1;
					
					}
				
				}
				
			}


Hope that helps-



thanks for the help but i still didnt get the result i wanted. the output was the same as before as it just show everything in the text without sorting them and i still cant figure out wads the problem. :angry:
Was This Post Helpful? 0
  • +
  • -

#7 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: splitting a text into 2 parts

Posted 27 January 2010 - 01:03 AM

@c.user - yes you are right ... :blink:

@complexity ... the code should read

			for (int i = 1, j = 0; j<fileline.length(); j++)
			{
					if (i == 1)
					{
						para1 = para1 + fileline[j];
						i = 2;
						
					}
			else
					{
						para2 = para2 + fileline[j];
						i = 1;
					
					}			   
			}



That will work!
Was This Post Helpful? 1
  • +
  • -

#8 complexity  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 07-October 08

Re: splitting a text into 2 parts

Posted 27 January 2010 - 01:15 AM

View PostMartyn.Rae, on 27 Jan, 2010 - 12:03 AM, said:

@c.user - yes you are right ... :blink:

@complexity ... the code should read

			for (int i = 1, j = 0; j<fileline.length(); j++)
			{
					if (i == 1)
					{
						para1 = para1 + fileline[j];
						i = 2;
						
					}
			else
					{
						para2 = para2 + fileline[j];
						i = 1;
					
					}			   
			}



That will work!


it worked!!!! thanks a lot martyn.rae and c.user! :D
Was This Post Helpful? 0
  • +
  • -

#9 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: splitting a text into 2 parts

Posted 27 January 2010 - 04:17 AM

View Postcomplexity, on 27 Jan, 2010 - 07:02 AM, said:

while (!myfile.eof())
{
	getline (myfile, fileline);
Be very careful with eof - it doesn't do what you probably think it does, and your program is likely to overrun past the end of the file. I've discussed this issue in more detail here: http://www.dreaminco...topic145699.htm
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1