Trouble with functions.

Hey guys.
I've been working through a c++ guidebook I bought and have been recently tip toeing through functions. The exercise is basically to read in a numbered string from any base which are separated by commas (EX 1001,110, 1111 or in hex 32, 19, 5A)I'm supposed to parse these into 3 different variables (Twice if I'm punching it into an xyz distance formula) and (I'm a little confused on this part) either change the string to an integer sub-string by sub-string and then change it from the given base to decimal or vice versa.

So as I count it I need to make 3 functions (Not counting the distance formula which I'm pretty sure I've gotten nailed down, I'll throw it up for critique and for the low chance of it helping anyone else though)

1) Parse the main string into an x,y, and z (3 different) sub-strings.
2) Change the string to integers
3) Given the base, translate the sub-strings (or integers) into decimal base.

FWI: All of my data would be read from a file. for example

(4150,504C,4500)
Mordor
16


I'd like to hope I've gotten like 80% of the thought process done, but there are some points in functions where I'm confused and because of how interrelated these functions are. But here are the functions

To sum up some lengthy reading. I'm having trouble with converting a string into a line of integers, no matter what changes I make it just comes back at zero, I fear a logic error I'm just not seeing. Some problems with equating a variable to my parse function. And taking a base x to decimal, it either screws up binary to decimal or disregards A-F. Otherwise it works perfectly.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  /*
   Makes string lines to Integer lines
 */
int StringToInt (string inputString){
   int i;
   int newIntegerLine;

      for (i=0; i<inputString.length();i++){
         //subtracts the ascii number of 0 from
         //whatever character you input
         newIntegerLine= (inputString.at(i)- '0');
      }

   return newIntegerLine;

}

I thought this would honestly work, I tried using the ASCII code to get each charactered number to just integer numbers, but in my dummy program I can't get it to anything else besides zero. And I know that this completely disregards anything above base 10... I think. But other than a nested if statements I'm unsure how to approach this. And considering the close relationship this function will have on the base x to base 10 function, I'm currently running around in circles trying to figure out how one will affect the other.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
   With a given base and with either a string of a number
   or the integer converted equivalent (given by another
   function) create a function that converts any based
   number to decimal
*/
int baseXToDecimal (int numberInput, int bases){
   int i, modulusSum;
   modulusSum=numberInput%10;
   for(i=bases;(numberInput/=10)!=0;i*=bases)
      modulusSum+=numberInput*i;
   return modulusSum;

   }


This works 90% of the time, the only time it gives me an error is when I start from decimal and goes to decimal. And I have no idea how to tackle A-F. Perhaps a loop? Which is why I thought of doing the conversion while the digits are characters. But I still run into a problem with A-F. A-'0' isn't 10 after all, it's 17. Perhaps nested if statements? But that seems so messy.

I thought that maybe I should rethink those two functions and somehow put them together?



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
/*
   Takes a string with substrings seperate by commas and
   breaks them into 3 individual strings.
   example. (123,456,789)
*/
void ParsingStringByComma (string strings){
   int pos, i;
   string stringParseNum1;
   string stringParseNum2;
   string stringParseNum3;

   /*
      try to get rid of the ( and )
   */
   for (i = 0; i < strings.length(); i++){

      while (strings[i] == '('){
         getline (cin,strings);
      }
   }
   for (i= 0; i < strings.length(); i++){
      while (strings[i]== ')'){
         getline (cin,strings);
      }
   }

   pos= strings.find(',');
   //From beginning to first pos
   stringParseNum1 = strings.substr(0,pos);
   //From first pos to second pos
   stringParseNum2 = strings.substr(pos+1, pos);
   //everything after the ssecond pos
   stringParseNum3 = strings.substr((strings.size()-pos+1), strings.size());


   cout << stringParseNum1 << endl;
   cout << stringParseNum2 << endl;
   cout << stringParseNum3 << endl;
   return;
}

I'm actually REALLY proud of this one. I was able to get the function to ignore the ( and ) that comes with every coordinates. But I'll have call this function twice for origin and destination, a total of 6 variables. I've read that I can't simply return more than one item with a normal function, so I can't just do
1
2
3
4
5
    const string ORIGIN = "(123,456,789)"

    //code...

    originX1= ParsingStringByComma(ORIGIN); 

To get my variable values can I? And I can't seem to manipulate stringParseNum# after I call it. But perhaps a For statement would work? The question is if I recode it to something I can return a value with how would

I'll still be working diligently on this, but any hints or concepts I'm probably mistaken on will be a godsend.
Topic archived. No new replies allowed.