newbie coding problem

This is a discussion on newbie coding problem within the C++ Programming forums, part of the General Programming Boards category; Here is the code for the program I am having trouble with, I realize that with buffered input it doesn't ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    52

    Question newbie coding problem

    Here is the code for the program I am having trouble with, I realize that with buffered input it doesn't stop when the @ symbol is entered. Thats not the problem. I'm using g++ and am getting no output after the input is entered? Thanks for any help given!

    // upper lower case conversion prblm 1 pg 137
    #include <iostream>
    using namespace std;
    #include <cctype>


    int main()
    {
    char ch;
    cout << "Changes lower case to upper and upper to lower case.\n";
    cout << "Enter a phrase:";
    cin.get(ch);
    while (ch != '@')
    {
    if(isalpha(ch))
    {
    if(islower(ch))
    ch=toupper(ch);
    else
    ch=tolower(ch);
    cout << ch;
    }
    cin.get(ch);
    }
    return 0;
    }

  2. #2
    Unregistered
    Guest
    Why not just use -1 to end, since the program is meant to change the case to upper and lower then the program should stop or at least print an error message when it encounters a non alphabetic character.
    Code:
    // upper lower case conversion prblm 1 pg 137
    #include <iostream>
    #include <cctype>
    
    using namespace std;
    
    int main(void)
    {
      char ch;
      cout<<"Changes lower case to upper and upper to lower case.\n";
      cout<<"Enter a phrase (-1 to exit):";
      cin.get(ch);
      while(ch != '-1'){
        if(isalpha(ch)){
          if(islower(ch))
            ch=toupper(ch);
          else
            ch=tolower(ch);
          cout<<ch<<flush;
        }else{
          cout<<"input not valid!"<<flush;
        cin.get(ch);
      }
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    52

    Question Alphabetical

    The problem description in c++ primer plus third edition, said use @ to end input, but I think you answered the problem anyhow though I hadn't yet learned to flush the buffer. It did say not to print digits entered just alphabetical chars. I would like to know how to do this w/o using somthing the book hasn't taught yet, and I don't understand why my version doesn't work.

    QUOTE]Originally posted by Unregistered
    Why not just use -1 to end, since the program is meant to change the case to upper and lower then the program should stop or at least print an error message when it encounters a non alphabetic character.
    Code:
    // upper lower case conversion prblm 1 pg 137
    #include <iostream>
    #include <cctype>
    
    using namespace std;
    
    int main(void)
    {
      char ch;
      cout<<"Changes lower case to upper and upper to lower case.\n";
      cout<<"Enter a phrase (-1 to exit):";
      cin.get(ch);
      while(ch != '-1'){
        if(isalpha(ch)){
          if(islower(ch))
            ch=toupper(ch);
          else
            ch=tolower(ch);
          cout<<ch<<flush;
        }else{
          cout<<"input not valid!"<<flush;
        cin.get(ch);
      }
      return 0;
    }
    [/QUOTE]

  4. #4
    Unregistered
    Guest
    This works fine for me
    Code:
    #include <iostream>
    using namespace std;
    #include <cctype>
    
    int main()
    {
      char ch;
      cout<<"Changes lower case to upper and upper to lower case.\n";
      cout<<"Enter a phrase:";
      cin.get(ch);
      while (ch != '@'){
        if(isalpha(ch) || isspace(ch)){
          if(islower(ch))
           ch=toupper(ch);
          else
           ch=tolower(ch);
          cout<<ch;
        }
        cin.get(ch);
      }
      return 0;
    }

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    52
    I add the or isspace function as you did in the code. It compiles fine as it did before, but I still get no output. I'm using the g++ compiler in linux.
    Where you using visual studio?
    Here is a listing of my code once again cut and pasted.


    // upper lower case conversion prblm 1 pg 137
    #include <iostream>
    using namespace std;
    #include <cctype>


    int main()
    {
    char ch;
    cout << "Changes lower case to upper and upper to lower case.\n";
    cout << "Enter a phrase:";
    cin.get(ch);
    while (ch != '@')
    {
    if(isalpha(ch) || isspace(ch))
    {
    if(islower(ch))
    ch=toupper(ch);
    else
    ch=tolower(ch);
    cout << ch;
    }
    cin.get(ch);
    }
    return 0;
    }

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    52
    sorry about the lack of indentation, thats the way the code appeared after it posted.

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    52

    still not working

    I still haven't gotten any output from my program. I even tryed cout << flush; at the end of the program as well as in the line cout << ch << flush; but am still getting no output. I guess flush is supposed to "flush the output buffer" really don't know though because this hasn't been taught yet in this book.

    Here is a similar program from the book that works just fine when I compile it.

    //ifelse.cpp pg208
    #include <iostream>
    using namespace std;
    int main()
    {
    char ch;

    cout << "Type, and I shall repeat.\n";
    cin.get(ch);
    while (ch != '.')
    {
    if (ch == '\n')
    cout << ch;
    else
    cout << ch+1;
    cin.get(ch);
    }
    cout << "\nPlease excuse the slight confusion.\n";
    return 0;
    }

    this makes me think that the function calls ch=toupper(ch);
    ch=tolower(ch);
    are somehow causing the problem. Any ideas?

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    52
    when I say no output I mean no output after I enter the phrase. common people doesn't anyone have an answer to this?
    I thought maybe it had to do with the cin.get(ch) if it returned the eol char the last thing entered in the buffer that would be the first thing the next cin.get(ch) would find and there for not work, but I think it should returrn the very first char in the buffer not the last, being just a newbie I'm beginning to speculate wildly. PS II'd like to move on to the next problem but am stuck here

  9. #9
    Registered User
    Join Date
    Jan 2002
    Posts
    52
    Apparently the problem has to do with flushing the buffer. If I cout << "\n another sentencte\n" at the end of the program the rest of the output is also flushed. I wonder why when I tryed cout << flush; it didn't work? Well solves that problem.

  10. #10
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    For some reason cout<< doesn't always work for me in different sections of my prog, it just doesn't display anything, now using printf() always works for me.

  11. #11
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    Hmm, cout << flush should flush the output buffer.
    Try cout.flush(); not cout << flush.
    Maybe you shoud include <iomanip>
    try this code
    Code:
    #include <iostream>
    #include <iomanip>
    #include <cctype>
    
    using namespace std;
    
    int main()
    {
      char ch;
      cout<<"Changes lower case to upper and upper to lower case.\n";
      cout<<"Enter a phrase:";
      cin.get(ch);
      while (ch != '@'){
        if(isalpha(ch) || isspace(ch)){
          if(islower(ch))
           ch=toupper(ch);
          else
           ch=tolower(ch);
          cout<<ch;
    
          cout.flush();  //flush the output buffer so it displays ****
    
        }
        cin.get(ch);
      }
      return 0;
    }
    You can use CODE inside []'s and /CODE inside [] to start and end a code block.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  2. Replies: 9
    Last Post: 03-20-2009, 05:22 PM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. Newbie ... looping problem
    By StupidIntel in forum C++ Programming
    Replies: 12
    Last Post: 05-13-2004, 06:45 PM
  5. Problem with code (newbie question)
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-31-2002, 01:39 AM

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