0

This code I'm writing for one of my classes will not print what it's written to. I've asked around and nobody can figure it out. Could it be something relating to the vectors or any strings? The furthest cout will work is where I print out the inputted command line arguments. When compiled on cygwin, it doesn't run into any errors that it can print out.

#include <iostream>
#include <sstream>
#include <vector>
#include <cmath>

using namespace std;

int main(int argc, char *argv[])
{
        //declare variables
        string possibleValues;
        string number;
        int inBase;
        int numLength;
        int outBase;
        int outcome;
        int baseTenTemp;
        vector<int> placeHolder;
        vector<int> finalTranslator;

        //turn base strings into integers

        baseTenTemp = 0;
        possibleValues = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        istringstream(argv[1]) >> inBase;
        istringstream(argv[3]) >> outBase;
        number = argv[2];

        cout << "inBase is " << inBase << " outBase is " << outBase << " and number is " << number << endl;

        //nothing beyond this point prints...
        //convert number into an int vector

        numLength = number.length();
        for(int i = 0; i<numLength; i++)
        {
                for(int j = 0; j <possibleValues.length(); j++)
                {
                        if(number.substr(i,i++)==possibleValues.substr(j,j++))
                        {
                                placeHolder.push_back(j);
                        }
                }
        }

        cout << "will this print?" << endl; //nope

        //convert number into base ten

        for(int i = 0; i < numLength; i++)
        {
                baseTenTemp = baseTenTemp + (placeHolder.at(i)*pow(inBase,numLength-i));
        }

        cout << baseTenTemp << endl;

        //convert base ten number into outBase number

        while(baseTenTemp>0)
        {
                finalTranslator.push_back(baseTenTemp%outBase);
                baseTenTemp=baseTenTemp/outBase;
        }

        //print converted number

        for(int i = finalTranslator.size()-1; i >=0; i--)
        {
                cout << finalTranslator.at(i);
        }
        cout << "checker" << endl;
        return 0;
}
  • 3
    The `if` statement in the loop [will cause demons to fly out of your nose](http://www.catb.org/jargon/html/N/nasal-demons.html) because function parameter evaluation order is unspecified (even if it was specified, the loop's algorithm is obviously flawed). Additionally, you'll be surprised to learn that in C++ `pow(10,2)` is not 100, [because floating point math is broken](https://stackoverflow.com/questions/588004/is-floating-point-math-broken), so your base 10 conversion code is broken too. Those are just the first few problems that are immediately apparent, in the shown code. – Sam Varshavchik Feb 16 '20 at 00:10
  • Consider turning on some warnings. I use these: " -Wall -Wextra -Wshadow -pedantic -Werror=vla -Wcast-align -Wcast-qual -Wconversion -Wsign-conversion -Wsign-compare -Wsign-promo -Wpointer-arith -Wunused -Wold-style-cast -Woverloaded-virtual -Wsequence-point -Wdelete-incomplete -Wmaybe-uninitialized -Wmisleading-indentation -Wunreachable-code -Wnon-virtual-dtor " and your code generates >12 warnings. I would fix each. – 2785528 Feb 16 '20 at 00:33
  • To make progress after you clean up the warnings and undefined behaviour, we will need test inputs, with expected outputs, to be able to reproduce your error. – 2785528 Feb 16 '20 at 00:40

0 Answers0