ASCII strikes again...happy face?

So I have a smiple job to do, and being that I learned a little java( tricycle ) before tackling C++ (Mountain bike)( what REAL programs do, according to my professor...I have grounds to believe him)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <math.h> // for later use	
#include <iostream>
#include <string>
using namespace std;
 string breakNum (int num)
{
	
	string answer;
	answer = num%10;
	//answer  = num/10;
	//num  = num%10;
	//num  = num/10;
	 return answer;
}

  Int main()
{
int num = 132;
cout << "Test runs" << endl;
cout << 132%10 << endl;
cout << 132/10 << endl;
cout << 13%10 << endl;
cout << 13/10 << endl;
cout << "below is the function example of breakNum" << endl;
cout << breakNum(a) << endl;
cout <<" code gets here" << endl;
}


now I should be getting at the least 2 but instead I am getting a happy face?...which makes me unhappy. What I am trying to do is eventually get each number out of 132 by its self. Thanks to all who read or cared to comment.
now I should be getting at the least 2


You are.

but instead I am getting a happy face?


A happy face is 2 (with the character encoding you happen to be using).

The problem is... 2 (the integer) and '2' (the character) are two different things. You have the former... but you want the latter.

The character '2' is actually == 50. That is... the ASCII character '2' has a numerical representation of 50.



Since numeric digits are stored in order in ASCII, you can convert from a single-digit integer to the correct ASCII representation of that character, merely by adding the character '0':

 
answer = char( (num%10)+'0' );


That will get you your '2' character.



EDIT:

updated my answer to explicitly cast to char
Last edited on
Assuming you changed
1
2
3
Int main()
// to 
int main()

and you intended
1
2
3
cout << breakNum(a) << endl;
// to be
cout << breakNum(num) << endl;

you still have mismatched types in you function.
1
2
3
4
5
6
7
8
9
10
 string breakNum (int num)
{
	
	string answer;
	answer = num%10; // = string answer = 132 % 10
	//answer  = num/10;
	//num  = num%10;
	//num  = num/10;
	 return answer;
}


<edit>
Disch gave better explanation
Last edited on
you still have mismatched types in you function.


I thought this would have errored also, since it's converting an int to a string. However it worked for me on VS. It might be a VS oddity.

I think it's calling the assignment operator that accepts a single char... and the % operation must be casting the int to a char. Either that or VS is silently casting int to char which would be weird.


EDIT:

After some testing, it looks like VS is just silently casting the int to a char. That is very strange.
Last edited on
I did not try to compile it other than the built in dealy, I was surprised it did not throw an error also.
There is assigment operator overload which takes single char as second parameter: http://en.cppreference.com/w/cpp/string/basic_string/operator%3D (4)
Both int and char are integral types and compiler can implicitely cast between them (though GCC warns about type narrowing here)
WOW thank you, to all of you. I hadn't notice that my type at the start of the function was a string type(string literal)...I think thats why I was getting a happy face just like you describe Disch. Now it makes sense because I played with some other number and got back a 3 leaf or 4 leaf clover ha ha which again left me unlucky with my result.
Topic archived. No new replies allowed.