Thread: getchar( )

Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407

    getchar( )


    Does anyone knows why getchar( ) is not working on the following code?
    Code:
    int Calculate( int f, char c, int s )
    {
    	char e;
    	
      	  cout << "\nNow Calculating, Please hold...\n\n";
    	
    	  switch( c )
    	  {
    		 case '+': cout << "Calculation: " << ( f + s ) << "\n"; break;
    		 case '-': cout << "Calculation: " << ( f - s ) << "\n"; break;
             case '*': cout << "Calculation: " << ( f * s ) << "\n"; break;
             case '/': cout << "Calculation: " << ( f / s ) << "\n"; break;	
             case '^': cout << "Calculation: " << ( f ^ s ) << "\n"; break;			
    	  }	
    	
    	e = getchar( );   
    	  
    	cout << "\nPress any key to exit...\n";
    	  
    	return 0;  
    }
    Coding up a simple calculator for learning perposes.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    337
    Rep Power
    10
    you do know that the the "Press and key..." will not show up until after you press a key, right?? Did you include conio.h??
  4. #3
  5. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    Originally Posted by Spectre5
    you do know that the the "Press and key..." will not show up until after you press a key, right?? Did you include conio.h??
    conio.h?????, and yes.. i do know that.. i have it that way.. because it didnt work any other way, anyways.. in c this would work with out conio, but i dont want to use conio.. is there another way? like maybe.. getch( )? I dont see it anyware in any c++ doc
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Location
    Australia, Malaysia
    Posts
    28
    Rep Power
    0

    how it doesn't work?


    you didn't give details on how it does work here. ..only think i can see here is you didn't print the value of "e" anywhere in the function...try to print it and see what happen...btw,
    i assume you've include the libary (#include <stdio.h>) in your function.

    have a look at http://www.cplusplus.com/ref/cstdio/getchar.html

    regards,
    marc
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    10
    Rep Power
    0
    Try define e as int
    Code:
    int e;
    ...
    e = getchar ();
    The format of the function is int getchar (void)
  10. #6
  11. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    yes i did include <stdio.h> and i changed it to
    Code:
    int e;
    e = getchar( );
    but the problem still excists, it should work but.. its not

    update
    Code:
    int Calculate( int f, char c, int s )
    {
    	int e;
    	
      	  cout << "\nNow Calculating, Please hold...\n\n";
    	
    	  switch( c )
    	  {
    		 case '+': cout << "Calculation: " << ( f + s ) << "\n"; break;
    		 case '-': cout << "Calculation: " << ( f - s ) << "\n"; break;
             case '*': cout << "Calculation: " << ( f * s ) << "\n"; break;
             case '/': cout << "Calculation: " << ( f / s ) << "\n"; break;	
             case '^': cout << "Calculation: " << ( f ^ s ) << "\n"; break;			
    	  }	
    	  
    	cout << "\nPress any key to exit...\n";
    	e = getchar( );   
    	  
    	return 0;  
    }
    The program just exits it self still, even if i printed e
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Location
    Australia, Malaysia
    Posts
    28
    Rep Power
    0

    it works fine for me


    i tested on ur program like below and it work fine.





    #include <stdio.h>
    #include <iostream>
    #include<fstream>

    using namespace std;

    int main()
    {
    int e;
    int f = 5;
    int s=5;
    char c ='+';
    cout << "\nNow Calculating, Please hold...\n\n";

    switch( c )
    {
    case '+': cout << "Calculation: " << ( f + s ) << "\n"; break;
    case '-': cout << "Calculation: " << ( f - s ) << "\n"; break;
    case '*': cout << "Calculation: " << ( f * s ) << "\n"; break;
    case '/': cout << "Calculation: " << ( f / s ) << "\n"; break;
    case '^': cout << "Calculation: " << ( f ^ s ) << "\n"; break;
    }

    cout << "\nPress any key to exit...\n";
    e = getchar( );

    return 0;
    }


    output

    Now Calculating, Please hold...

    Calculation: 10

    Press any key to exit...



    it did hold there

    i suspend the error is somewhere else?

    ps: check ur function import argument is in the valid dataype..otherwise program will force to close due to this...try to use strace to debug..

    regards,
    marc
  14. #8
  15. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    hm could it be that im on linux? if so.. shouldnt it still work?
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Location
    Australia, Malaysia
    Posts
    28
    Rep Power
    0
    I am tesing this on linux :)

    check the previous post's ps

    also, try and copy the code i tested just now and run on your machine. see it work or not..if not, sorry i have no idea :)


    regards,
    marc
  18. #10
  19. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    hm weird.. here is the whole code
    Code:
    #include <iostream>
    #include <stdio.h>
    
    using namespace std;
    
    int Calculate( int f, char c, int s );
    
    int main( )
    {
        char simble[] = { '*', '+', '-', '/', '^' };
    	char calc;
    	int x, y;
    	
    	cout << "\nEnter Calculation (i.e. Calculation 1+1 or 1*1 etc )\n\n";  
    	cout << "+ For Addition\n";
    	cout << "- For Subtraction\n";
    	cout << "* For Multiplication\n";
    	cout << "/ For Division\n";
    	cout << "^ For Power\n\n";
    	
    	cout << "First Value: ";
    	
    	if( cin >> x )
    	{
    		cout << "Type: ";
    		cin >> calc;
    		
    		cout << "Second Value: ";
    		
    		if( cin >> y )
    		{	
    	        for( int check = 0; check < sizeof( simble ); check++ )
                {
    		         if( simble[check] == calc )
    		         {	
                         Calculate( x, calc, y );
    				     return 1;
    			     }	 
                }
    			
    		}else{
     
                cout << "\nThat is not a correct mathamatical value!\n\n";
    	        return 1;           			
    		}
    		
         }else{
    	
    	    cout << "\nThat is not a correct mathamatical value!\n\n";
    	    return 1;
         }	
    	
    	cout << "Error!!!!\n"; 
        return 0;	
    }
    
    
    int Calculate( int f, char c, int s )
    {
    	int e;
    	
      	  cout << "\nNow Calculating, Please hold...\n\n";
    	
    	  switch( c )
    	  {
    		 case '+': cout << "Calculation: " << ( f + s ) << "\n"; break;
    		 case '-': cout << "Calculation: " << ( f - s ) << "\n"; break;
             case '*': cout << "Calculation: " << ( f * s ) << "\n"; break;
             case '/': cout << "Calculation: " << ( f / s ) << "\n"; break;	
             case '^': cout << "Calculation: " << ( f ^ s ) << "\n"; break;			
    	  }
    	  
    	cout << "\nPress any key to exit...\n"; 
            e = getchar( );
    	return 0;  
    }
    I mean i dont see any error :confused:
    Last edited by xlordt; September 10th, 2004 at 03:51 AM.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    10
    Rep Power
    0
    Since you're using C++, why don't you use cin.get() instead getchar() ? It should be doing the same thing...
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jul 2004
    Location
    Middle Europa
    Posts
    1,200
    Rep Power
    13
    you said 'learning perposes'
    so: you use one-char variable names :( :(
    suppose you must change the variable name 'n' in a 2000 lines
    src code ???
    how do you do it?
    NOTA: no probls using a reasonable editor, the Q is how do
    you handle the exceptions? i mean:
    int n; is to change
    printf("\n") or case 'n': are NOT to change
    suppose you used 'nnn' instead of 'n' ...........
  24. #13
  25. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    Originally Posted by guggach
    you said 'learning perposes'
    so: you use one-char variable names :( :(
    suppose you must change the variable name 'n' in a 2000 lines
    src code ???
    how do you do it?
    NOTA: no probls using a reasonable editor, the Q is how do
    you handle the exceptions? i mean:
    int n; is to change
    printf("\n") or case 'n': are NOT to change
    suppose you used 'nnn' instead of 'n' ...........
    say what?!?
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jul 2004
    Location
    Middle Europa
    Posts
    1,200
    Rep Power
    13
    is my english so bad, you cannot read it?
    should i say it in german, french or italian, maybe in 'c' ?
    Last edited by guggach; September 10th, 2004 at 04:29 AM. Reason: typo
  28. #15
  29. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    Originally Posted by guggach
    is my english so bad, you cannot read it?
    anyone can read it, but as far as understanding it.. then that is a whole different story, so to answer your question no.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo