Question

I have one two coding projects currently, both of which are giving me weird trouble that I just can't figure out. I've spent hours messing with every little thing to try and fix them and they just don't seem to work out.

The first is a simple program to step through a .txt file and assign values to the characters, adding to the totalvalue, which is printed. 'A'-'Z' are values 1-26, respectively, '-' is 0, and '.' is also 0. An example .txt file would be:

...............
..-............
.........A.....
..Z.........C..

The answer here would be 30. Initially I was getting "30%" as the output, and while messing with it to try and get rid of the random '%', I somehow screwed up my logic somewhere and now it is just outputting "0%" for every file. Here is my code:

#include <iostream>
#include <fstream>
#include <cmath>
#include <math.h>
#include <vector>

using namespace std;

int getValue(char indicator)
{
    int value;
    if (indicator = 'A')
    {
        value = 1;
    }
    else if (indicator = 'B')
    {
        value = 2;
    }
    else if (indicator = 'C')
    {
        value = 3;
    }
    else if (indicator = 'D')
    {
        value = 4;
    }
    else if (indicator = 'E')
    {
        value = 5;
    }
    else if (indicator = 'F')
    {
        value = 6;
    }
    else if (indicator = 'G')
    {
        value = 7;
    }
    else if (indicator = 'H')
    {
        value = 8;
    }
    else if (indicator = 'I')
    {
        value = 9;
    }
    else if (indicator = 'J')
    {
        value = 10;
    }
    else if (indicator = 'K')
    {
        value = 11;
    }
    else if (indicator = 'L')
    {
        value = 12;
    }
    else if (indicator = 'M')
    {
        value = 13;
    }
    else if (indicator = 'N')
    {
        value = 14;
    }
    else if (indicator = 'O')
    {
        value = 15;
    }
    else if (indicator = 'P')
    {
        value = 16;
    }
    else if (indicator = 'Q')
    {
        value = 17;
    }
    else if (indicator = 'R')
    {
        value = 18;
    }
    else if (indicator = 'S')
    {
        value = 19;
    }
    else if (indicator = 'T')
    {
        value = 20;
    }
    else if (indicator = 'U')
    {
        value = 21;
    }
    else if (indicator = 'V')
    {
        value = 22;
    }
    else if (indicator = 'W')
    {
        value = 23;
    }
    else if (indicator = 'X')
    {
        value = 24;
    }
    else if (indicator = 'Y')
    {
        value = 25;
    }
    else if (indicator = 'Z')
    {
        value = 26;
    }
    return value;
}

int main()
{
    string filename;
    ifstream txtfile;
    char indicator;
    int currentvalue;
    int totalvalue = 0;
    cin >> filename;
    txtfile.open(filename.c_str());
    while(txtfile >> indicator)
    {
        if (indicator = '.')
        {
            currentvalue = 0;
        }
        else if (indicator = '-')
        {
            currentvalue = 0;
        }
        else
        {
            currentvalue = getValue(indicator);
        }
        totalvalue += currentvalue;
    }
    cout << totalvalue;
    return 0;
}

My second issue is possibly a bit more complicated. This program converts a decimal into binary. I'm only allowed to make my own functions and edit the dec2bin function. Everything else is off limits, including int main(). The comments in the code are all from my TAs to provide tips on making the dec2bin function. I have made the exponential function, as we are not allowed to add any extra #includes. I will first show my code and then show what the output looks like.

#include <iostream>
#include <string>

using namespace std;

bool isdecimal(string &input)
{
    // see bin2dec for description of how the below code works

    return input.find_first_not_of("0123456789") == string::npos;
}

void reverse(string &input) 
{
    int n = input.length();
    for (int k=0; k<n/2; k++) 
      swap(input[k], input[n-1-k]);
}

int exponential(int &exponent)
{
    int multiple = 1;
    for (int i = 0; i < exponent; i++)
    {
        multiple *= 10;
    }
    return multiple;
}

string dec2bin(string &decstr)
{
    // convert string decstr to integer decval:
    // 1) reverse to obtain LSB-MSB digit order
    reverse(decstr);
    // 2) for each ascii digit, subtract '0' to 
    //    obtain integer value, multiply by 10^k
    //    and continually add result to decval
    unsigned int i;
    int addval = 0;
    int remainder = 0;
    int decval = 0;
    int currentval = 0;
    char binchar;
    string newstring = "";
    for (i = 0; i < decstr.size(); i++)
    {
        currentval = (decstr[i] - '0');
        addval = currentval * exponential(i);
        decval += addval;
    }
    // apply successive-halving
    while(decval > 0) {
        remainder = decval % 2;
        decval = decval / 2;
        binchar = remainder;
        newstring.push_back(binchar);
    }
    // reverse to restore MSB-LSB bit order
    reverse(newstring);
    return newstring;
}

int main()
{
    string decstr;
    while (cin >> decstr) {
        if (isdecimal(decstr) == false)
            cout << "input error\n";
        else
            cout << dec2bin(decstr) << "\n";
    }
}

Since I am not allowed to post images, I will describe what my output looks like. Regardless of the integer I input, it returns some combinations of whitespace and weird square smiley faces (I'm using CodeLite for this, in Unix it prints nothing.)

Thank you in advance to absolutely anyone who helps me. i am just stumped on this. Also, feel free to be as vague as possible. Just some pointers may help. I'm sure the first one is some small syntax error or something like that, but I simply cannot find it.

Was it helpful?

Solution 2

Solved your first problem, you were assigning indicator and not comparing it, so you would always get 0. Also, I tidied up a bit.

#include <iostream>
#include <fstream>

using namespace std;

int getValue(char indicator)
{
    int value = 0;
    switch(indicator)
    {
        case 'Z': value++;
        case 'Y': value++;
        case 'X': value++;
        case 'W': value++;
        case 'V': value++;
        case 'U': value++;
        case 'T': value++;
        case 'S': value++;
        case 'R': value++;
        case 'Q': value++;
        case 'P': value++;
        case 'O': value++;
        case 'N': value++;
        case 'M': value++;
        case 'L': value++;
        case 'K': value++;
        case 'J': value++;
        case 'I': value++;
        case 'H': value++;
        case 'G': value++;
        case 'F': value++;
        case 'E': value++;
        case 'D': value++;
        case 'C': value++;
        case 'B': value++;
        case 'A': value++;
                  break;
        default: value = 0;
    }
    return value;
}

int main()
{
    string      filename;
    ifstream    txtfile;
    char        indicator;
    int         currentvalue,
                totalvalue = 0;

    cin >> filename;
    txtfile.open(filename.c_str());

    while(txtfile >> indicator)
    {
        if (indicator == '.') // = is for assignment, == is for comparison
            currentvalue = 0;
        else if (indicator == '-')
            currentvalue = 0;
        else
            currentvalue = getValue(indicator);
        totalvalue += currentvalue;
    }
    cout << totalvalue << endl;
    return 0;
}

And I get 30 with your input:)

OTHER TIPS

Why are you using an assignment = operator?

main.c

while(txtfile >> indicator)
{
  if (indicator = '.')
  {
     currentvalue = 0;
  }
  // SNIP!
}

Surely you meant this:

while(txtfile >> indicator)
{
  if (indicator == '.')
  {
     currentvalue = 0;
  }
  // SNIP!
}

Big difference between = and ==, one is an assignment, the other is equality checking of the contents.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top