Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,709 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,280 people online right now. Registration is fast and FREE... Join Now!




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

 
Reply to this topicStart new topic

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

dh000
3 Feb, 2007 - 09:22 PM
Post #1

New D.I.C Head
*

Joined: 3 Feb, 2007
Posts: 1


My Contributions
CODE
#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
User is offlineProfile CardPM
+Quote Post

Antiokus
RE: Using Cin.get() To Decipher A Block Of Text
4 Feb, 2007 - 04:20 PM
Post #2

D.I.C Head
**

Joined: 6 Sep, 2006
Posts: 128



Thanked: 1 times
My Contributions
could you explain what you were trying to do with that while loop? it looks like you're assigning 'c' multiple times.
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Using Cin.get() To Decipher A Block Of Text
5 Feb, 2007 - 09:31 PM
Post #3

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,153



Thanked: 44 times
Dream Kudos: 125
My Contributions
QUOTE(dh000 @ 3 Feb, 2007 - 10:22 PM) *

CODE

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.gif . 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

CODE

#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.gif
I hope this will help you in some way.

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 04:25AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month