I'm trying to write a program that switches ! for $ and whenever it encounters # just ignores it; however, i get a weird output.
When I write: Now!is#the!time#for!fun#!
I should get: Now$isthe$timefor$fun$
But instead I get: Nwitetmfrfn
What am I doing wrong?
Here's the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
usingnamespace std;
int main()
{
char ch;
cout << "Enter a phrase: ";
while (cin.get(ch))
{
if(ch == '!')
cin.putback('$');
else
cout << ch;
while (cin.peek() == '#');
cin.ignore(1,'#');
}
return 0;
}
I'm having trouble understanding where to use peek()? After which line? And can you please provide the syntax like do I do:
cout << cin.peek() << endl; (after line 11)
By the way that's what I tried and got:
Enter the phrase: Now!is#the!time#for!fun#!
Nwitetmfrfn
So I'm obviously doing something wrong I just don't know what :(