Thread: getchar

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    86

    getchar

    The following is code for a VERY SIMPLE help menu

    Code:
    //help.h
    //help menu
    
    #include <iostream>
    
    using namespace std;
    
    void help(void){
    
        int hop;
        
        cout<<"Type 1 for simple calc help\nType 2 for root calc help\nType 3 for powers help\nType 4 for circumference\nType 5 for equation help\nType 6 for vector help:\n";
        
        cin>>hop;
        
        if(hop==1){
        
            cout<<"Enter a simple problem, such as 4+7.8\nYou can use decimals or integers\nYou can use +-/* as functions\n";
            
        }else if(hop==2){
        
            cout<<"Select an option of which root\nEnter a number\nYou will recieve an approximate root\n";
            
        }else if(hop==3){
        
            cout<<"Enter a number and a power in the format x^y\nYou must include the ^ [carrot]\nYou recieve x to y power\n";
            
        }else if(hop==4){
        
            cout<<"Enter a number (r)\nYou receive the circumference\n";
            
        }else if(hop==5){
        
            cout<<"Enter an equation of one of the following formats:\n\nax+b=c\nc=ax+b\nb+ax=c\nax=c\nx+b=c\nc=x+b\nc=x+b\nc=ax\nc=b+ax/b=c+ax\nax=c+b/ax=b+c\nc+b=ax/b+c=ax\n";
            
        }else if(hop==6){
        
            cout<<"The program can return the sum/difference of two vectors\nThe product/quotient of a vector and a scalar\nOr the dot product of two vectors\n\n";
            cout<<"Firstly you will be prompted to enter your first vector.\nAfter this, you will be prompted for a symbol.\n\n";
            cout<<"You enter a symbol of + - * / or .\n\n";
            cout<<"Depending on your symbol, you will be prompted for a vector or scalar\n\n";
        
        }else{
        
            cout<<"Invalid option\n";
            
        }//end else/if
        
        cout<<"Keep in mind, that due to the formatting of all numbers,\nall answers are APPROXIMATE\n";
        
        getchar();
        
    }//end void help
    Everything in there works, except for the very last (or second to last if you want to be picky about it) statement; getchar()

    the program in question is being implemented on a MAC, so getchar() is the equivallent of the windows getch() or getche() and doesn't need to be included

    for some reason, whenever I access the function in which getchar() is contained, it alwasy ignores the getchar() statement

    This provides some considerable problems concerning the easy reading of the output

    any ideas?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    1. dont use getchar as it is a c stand function which resides in stdio.h file ( in your code u need to include stdio.h file)

    2. sometimes buffer would be containing some enter key left in there, need to ignore that from the buffer

    u can use cin::ignore(10000,'\n')
    3. and the most effective way to get a char in c++ is

    Code:
    cin.ignore();
    cin.get();
    replace this with the getchar in your code

    regards
    ssharish

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    21
    getchar() does not do the same thing as getch(). I began my coding on a Mac, and had a similiar problem. I have some code that will emulate getch(), but I need to find it. I'll look for it and post if I can find it.

  4. #4
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Code:
     cin>>hop;
    Code:
     getchar();
    getchar() is not being ignored. cin leaves the newline in the stream and getchar() successfully takes it out of the stream and thus passing program execution onto the next statement which exists the function.

    You need 2 cin.get() statements, which is what you should be using considering this is C++.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    cin skips all leading whitespace, then reads in data and stops reading in data when the first white space character is encountered(e.g. spaces, tabs, and newlines). The terminating whitespace character is left in the stream. This line:

    cin>>hop;

    waits for the user to enter data. Then, the user types something in and hits Return. Hitting Return enters an invisible \n after the input. So, if the user types in a 1 and hits Return, the input stream looks like this:

    1\n

    1) cin skips any leading whitespace--there's no whitespace in front of the 1, so
    2) cin reads in the 1, and
    3) cin stops reading when it encounters the whitespace character \n
    4) cin leaves the \n in the input stream.

    Then, further down in your program, this line is executed:

    getchar();

    which gets the next character out of the input stream. Since, there is a \n left in the input stream, getchar() doesn't need to wait for input and does it's thing and reads in the \n.

    The lesson is: if you are going to be switching between cin and get() functions for reading in input, you need to remove the trailing whitespace that cin leaves in the stream. You can do that like this:

    cin>>hop;
    cin.ignore(1);

    cin.ignore() will remove the designated number of characters from the stream. Note that cin.ignore() is not necessary if you do this:

    cin>>hop;

    and the user enters:

    1\n

    and then you do something like this:

    cin>>hop2;

    Since cin skips leading whitespace, cin skips over the \n looking for input, and since it can't find any, it waits for the user to enter something.
    Last edited by 7stud; 02-13-2005 at 08:40 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getchar() problem
    By jlharrison in forum C Programming
    Replies: 6
    Last Post: 01-25-2006, 02:49 PM
  2. getchar buffer size
    By oncemyway in forum C Programming
    Replies: 3
    Last Post: 08-02-2005, 12:49 AM
  3. getchar() problem from K&R book
    By anemicrose in forum C Programming
    Replies: 13
    Last Post: 04-04-2004, 11:06 PM
  4. help with getchar lol
    By Taco Grande in forum C Programming
    Replies: 5
    Last Post: 03-18-2003, 09:25 PM
  5. Can anybody take a look at this?
    By TerryBogard in forum C Programming
    Replies: 10
    Last Post: 11-21-2002, 01:11 PM