returning a array or int from function???

I'm doing an assignment for cs class but I'm having trouble with the logic?
Here is mine problem...Im reading a string to a function and returning an int, i have to do some calculations to the string and return it as an int but as a 5 digit int.Heres what i have so far..

int ZipCode::return_int_zip(string mystr)
{
int x=0,y=5;
int count=0;
int answer[5]={0,0,0,0,0};

for(int i=0; i<5; i++)
{
string temp = mystr.substr(x,y);

if( temp == "11000")
{
answer[i] = 0;
}
else if (temp == "00011")
{
answer[i] = 1;
}
else if( temp == "00101")
{
answer[i] = 2;
}
else if( temp == "00110")
{
answer[i] = 3;
}
else if( temp == "01001")
{
answer[i] = 4;
}
else if( temp == "01010")
{
answer[i] = 5;
}
else if( temp == "01100")
{
answer[i] = 6;
}
else if( temp == "10001")
{
answer[i] = 7;
}
else if( temp == "10010")
{
answer[i] = 8;
}
else if( temp == "10100")
{
answer[i] = 9;
}

x = x + 5;
}

cout<<"The Bar Code is: ";
for (int i=0; i<5; i++)
{
cout<<answer[i];
}



return count;
}
since i can't figure out a way to return it as int for now i return just an variable...none the less the assignment requires returning it as 5 digit int. any help
or hints would be greatly appreciated. Thanks
simply combine the number using the powers of 10:

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <cmath>

using namespace std;

int ZipCode::return_int_zip(string mystr)
{
int x=0,y=5;
int count=0;
int answer[5]={0,0,0,0,0};

for(int i=0; i<5; i++)
{
string temp = mystr.substr(x,y);	

if( temp == "11000")
{
answer[i] = 0;
}
else if (temp == "00011")
{
answer[i] = 1; 
}
else if( temp == "00101")
{
answer[i] = 2;
}
else if( temp == "00110")
{
answer[i] = 3;
}
else if( temp == "01001")
{
answer[i] = 4;
}
else if( temp == "01010")
{
answer[i] = 5;
}
else if( temp == "01100")
{
answer[i] = 6;
}
else if( temp == "10001")
{
answer[i] = 7;
}
else if( temp == "10010")
{
answer[i] = 8;
}
else if( temp == "10100")
{
answer[i] = 9;
}


x = x + 5;

return returnVal;
} 

cout<<"The Bar Code is: ";
int returnVal = 0;

for(long i = 0; i < 5; i++)
{
    returnVal += pow(10,i)*answer[i];
}


return returnVal;
}


How about that? hope that helps!

And next time use [code][ /code] wrapper and indents. I got lost in your code.
Last edited on
Topic archived. No new replies allowed.