Putback() help

This is a discussion on Putback() help within the C++ Programming forums, part of the General Programming Boards category; Hi! Im trying to write a code that replaces the first letter of every line with a capital letter. So ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    796

    Putback() help

    Hi!

    Im trying to write a code that replaces the first letter of every line with a capital letter.
    So for starters that would be the part of the code for replacing.

    The problem:
    for ex. input : abc
    output: aAbBC

    It only replaces the last letter and not the first ones.
    Maybe im using the wrong funtion for that. Thanks.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
     char ch;
     cout << "Enter a phrase: "; 
     while ( cin.get(ch))
    {
               if (ch == 'a')
                      cin.putback('A');
                      if (ch == 'b')
                      cin.putback('B');
                      if (ch == 'c')
                      cin.putback('C');
    
              else
                     cout << ch;
    
    }
      system("PAUSE");
      return 0;
    }
    Last edited by Ducky; 12-09-2007 at 09:25 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    Another example of why good indentation and brace usage will prevent dumb mistakes.
    Code:
    if (ch == 'a') {
      cin.putback('A');
    }
    
    if (ch == 'b') {
      cin.putback('B');
    }
    
    if (ch == 'c') {
      cin.putback('C');
    } else {
      cout << ch;
    }
    Your 'else' is not the exception to ALL your if statements, only the last one.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,433
    putback() is not what you want to use. It manipulates cin's buffer so you effectively get to read a character again on the next call to cin.get().

    Simply read the first character from cin, convert it to upper case, and output it. Then loop as long as required, reading one character from cin and outputting it without conversion. No need to push things back into cin's buffer at all.

    Incidentally, it doesn't really matter in this case (simple code) but it helps if your code indenting matches what the code does. Keep in mind that the compiler ignores indenting.

    And system("PAUSE") is windows specific.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    Thanks a lot Salem and Grumpy , that helped me a lot.

    And how do i convert it to upper case without "output()"?
    Code:
    char ch[222];
    cout << "Enter a phrase: ";
    while ( cin.get(ch[222]))
    {
      if (ch[0] == 'a')
                   {
                   ch[0] = 'A';
                   }
    Like this?
    Last edited by Ducky; 12-09-2007 at 01:10 PM.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Code:
    ch[0] = toupper(ch[0]);

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    Thanks a million Cyberfish!
    Just what i needed.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21