How can I write this without putback or peek?

My book is asking me to rewrite this without putback or peek. Can anyone shed some light on some alternatives for me? Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  #include <iostream>

int main()
{
	char ch;
    std::cout <<"Enter a phrase: ";
    while (std::cin.get(ch))
    {    if (ch == '!')
        std::cin.putback('$');
        else
            std::cout<<ch;
            while(std::cin.peek()=='#')
                std::cin.ignore(1,'#');
    }
}
You need two things:

1. You have to know what the program should do. In this case you need to understand what that program does.

2. You need to know what tools you have and how to use them.


You could start from http://www.cplusplus.com/reference/istream/istream/
i have had a look and i cant see any functions that would replace putback and peek, can you tell me which functions i can use to replace them? Thanks.
I have never used putback() or peek().
How could I possibly know what your program does?
If I don't know that, how could I know what are equivalent solutions?
it replaces a hash with a dollar sign multiple times in a sentence.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using std::cin;
using std::cout;
int main()
{
   char ch;
   bool first = true;
   cout <<"Enter a phrase: ";
   while ( cin.get( ch ) )
   {
      if ( ch == '!' ) cout << '$';
      else if ( ( ch == '#' && first ) || ch != '#' ) cout << ch;
      first = false;
   }
}


Enter a phrase: # abc d! e# f##
# abc d$ e f
Last edited on
Topic archived. No new replies allowed.