2 Replies - 2757 Views - Last Post: 05 February 2007 - 10:31 PM Rate Topic: -----

#1 dh000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-February 07

using cin.get() to decipher a block of text

Posted 03 February 2007 - 10:22 PM

#include <iostream>
#include <fstream>
using std::fstream;

using namespace std;

int main(int argc, char *argv[])
{
	char c;
	
	ifstream infile("C:\\document01.cry");
	if ( !infile )
	{
		cerr << "File could not be opened" << endl;
		exit( 1 );
	}
	while (! infile.eof())
	{
		c = infile.get();
		char c = cin.get();
		cout.put(c);
	}
}



hello this is my first post and i need help on an asignment in my class. im trying to change a block of text (which in the beginning is just a bunch of jumbled up words) by getting rid of every other letter in that text. so basically, i'm having trouble with the part after while (! infile.eof()). any help or hints is much appreciated. thanks

Is This A Good Question/Topic? 0
  • +

Replies To: using cin.get() to decipher a block of text

#2 Antiokus  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 192
  • Joined: 06-September 06

Re: using cin.get() to decipher a block of text

Posted 04 February 2007 - 05:20 PM

could you explain what you were trying to do with that while loop? it looks like you're assigning 'c' multiple times.
Was This Post Helpful? 0
  • +
  • -

#3 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: using cin.get() to decipher a block of text

Posted 05 February 2007 - 10:31 PM

View Postdh000, on 3 Feb, 2007 - 10:22 PM, said:

while (! infile.eof())
{
  c = infile.get();
  //what is this for???
  char c = cin.get();
  //this will print what u took on above line and not something u got from infile.get()
  cout.put(c);
}



im trying to change a block of text (which in the beginning is just a bunch of jumbled up words) by getting rid of every other letter in that text. so basically, i'm having trouble with the part after while (! infile.eof()).



I am confused about the statement cin.get() :sleepy: . I am not getting what exactly you are trying to do there. Please explain your inputs and needed output.

What I understood is you want to print every alternate character from the file.
If this is right then you can use some variable which will work as flag for you.

eg

#include <iostream>
#include <fstream>
using std::fstream;

using namespace std;

int main(int argc, char *argv[])
{
   char c;
   int flag =0;
   ifstream infile("C:\\document01.cry");
   if ( !infile )
   {
	  cerr << "File could not be opened" << endl;
	  exit( 1);
   }
   while (! infile.eof())
   {
	  c = infile.get();
	  if(flag == 0)
	  { 
		 cout.put(c);
		 flag = 1;
	  }
	  else
	   flag = 0;
   }
}




I am expecting this is going on right track, is that? :huh:
I hope this will help you in some way.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1