CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2009
    Posts
    7

    Exclamation Why getting male sex symbol? char push help

    I need to add the operands and then push the result onto the stack, when pushed; it pushes a symbol (add 6 and 5, you get the male symbol). What am I doing wrong, I need the symbol to be 11 (when using 6 and 5)?


    Code:
    stack myStack;
    char arg1;  // this will hold the left operand.
    char arg2; //this will hold the right operand.
    char result; // result of every calculation, to be pushed onto stack.
    
    
    int main()
    {
    	string s1;
    	cout << "Input the numbers followed by operator(s): " << endl;
    	cin >> s1;
    
    	for (int i=0;s1[i]!='\0';i++) 
    	{
    		if (isdigit(s1[i]))  //used isdigit to check if number, if true; push.
    			myStack.push(s1[i]);
    		else if(s1[i]=='+'){
    			//int x = int(mystring[i]) - 48;
    			myStack.pop(arg1); //pops operand off top of stack and into arg1 which is a char data
    			myStack.pop(arg2); //pops operand off top of stack and into arg2 which is a char data
    			result = int(arg1)-48 + int(arg2)-48;  
    			//if result is initialized as char, pushes symbol and result is symbol
    			//if result is initialized as int, pushes symbol but result is correctly outputed below
    			cout << "arg1 and arg2 and result: " << arg1 << " " << arg2 << " " << int(result) << endl;  //The numbers are correct here , 6 5 11.  If int() not used, male sex symbol is result.
    
    			myStack.push(result); //HOW TO INPUT RESULT????? tried using int(result) and //intresult-48, both do not give the correct 11 when '65+' is unput.
    		} else
    			cout << "else called from client" << endl;
    			
    	}
    
    	myStack.displayAll(); //displays contents of stack which should be 11 but comes our as the male sex symbol when inputed '65+' into program. should be 11.
    }

  2. #2
    Join Date
    Apr 2009
    Posts
    598

    Re: Why getting male sex symbol? char push help

    The codification of numbers is different from the codification of Ascii string of characters.

    A number: e.g. 0, 1, 2, ... 11, 12, ...

    An Ascii string of characters repsenting a number: e.g. 48, 49, 50, ... 49 49, 49 50, ...

    In your code, you use the magic number, 48. It is the Ascci code of '0'.
    You remove that number, which is a good thing.
    But, later, you forgot to add again that number, in order to have a string of characters that can be displayed or printed.

    The problem is somehow more complex since the results has two digitis instead of only one for your input (5 and 6). Therefore you need to have a loop for each digit of your result, where you would add 48. Or, a more simple and common approach is to convert the number to an ascii string of characters using one of the available possibilities of C++.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured