Hello, I'm trying to create a character array that would find the absolute value of each value. Ex. Candadite: 111111 / Target: 444444. abs(1-4)(1-4)(1-4)(1-4)(1-4)(1-4) answer: 333333. I have the code running, but I get a consistant answer of 700, even when I move to a new Target set. If anyone could help I'd appreciate it greatly, thanks :)

Sorry if this is poorly discribed, I have attatched my project's word document if you need further details. Thanks again

#include <iostream>
#include <cmath>
#include <iomanip>
#include <Windows.h>
#include <fstream>

using namespace std;

void readit();
void calcit(char[10][6], char[3][6], char a, char b, char c, char d);
void writeit(int score1, int score2, int score3);

int main()
{
    readit();

    return 0;
}

void readit()
{
    char candidate[10][6], target[3][6], a, b, c, d;

    ifstream candidates("D:\\School\\C++ Programs\\Project 3\\candidates.txt");

    ifstream targets("D:\\School\\C++ programs\\Project 3\\targets.txt");

    //Declaring the format of table
    for (char a = 0; a < 10; a++)
        for (char b = 0; b < 6; b++)
            {
                //Declaring format of table "candidate"
                candidates >> candidate[a][b];
            }

        for (char c = 0; c < 3; c++)
            for (char d = 0; d < 6; d++)
            {
                //Declaring format of table "target"
                targets >> target[c][d];
            }
    calcit(candidate, target, a, b, c, d);
}

void calcit(char candidate[10][6], char target[3][6], char a, char b, char c, char d)
{
    int score1 = 0, score2 = 0, score3 = 0;

    //Stating that all candidates and the first row of targets are to be compared
    //and the absolute value is to be found
    for (char a = 0; a < 10; a++)
        for (char b = 0; b < 6; b++)
            for (char c = 0; c < 1; c++)
                for (char d = 0; d < 6; d++)
                    score1 = score1 + abs(b - d);

    ////Stating that all candidates and the second row of targets are to be compared
    //and the absolute value is to be found
    for (char a = 0; a < 10; a++)
        for (char b = 0; b < 6; b++)
            for (char c = 1; c < 2; c++)
                for (char d = 0; d < 6; d++)
                    score2 = score2 + abs(b - d);

    //Stating that all candidates and the third row of targets are to be compared
    //and the absolute value is to be found 
    for (char a = 0; a < 10; a++)
        for (char b = 0; b < 6; b++)
            for (char c = 2; c < 3; c++)
                for (char d =0; d < 6; d++)
                    score3 = score3 + abs(b - d);

    writeit(score1, score2, score3);
}

void writeit(int score1, int score2, int score3)
{
    //This table is not finished, trying to find the correct answer before continuing
    cout << "   Target      Candidate       Score";
    cout << "---------------------------------------";
    cout << score1 << endl;
    cout << score2 << endl;
    cout << score3 << endl;
}

This looks like one problem here(line 42):

calcit(candidate, target, a, b, c, d);

This sits outside the loop and for the life of me I can't see why you're sending the loop variables to your function.

This whole function:

void calcit(char candidate[10][6], char target[3][6], char a, char b, char c, char d)

also doesn't make much sense. You're passing variables in, then you're re-declaring the same variable names in the loops and creating 3 separate sums but none of the values come from the candidate array or are written to the target array.

I think perhaps you need to re-think what you are doing.

My professor wants us to use cascaading functions so we can get used to them.

But thanks for the input

I'm not sure why you are using char a =0, char b=0, etc, instead of int a = 0, int b = 0. I actually tried compiling that in visual studio and it threw compile errors.

To tinstaffl's point, you don't need to send a, b, c, and d. They are only used in for loops and you initialize them each time in each new function so change your calcit function to

void calcit(char[10][6], char[3][6])

The next thing I see is in the calcit function, you send candidate and target, but you do nothing with them. you are performing abs functions on the values of the for loops so you aren't actually doing anything with the values in the .txt file. You repeat the same process in all three loops hence you are getting the same value of 700 every time. This is essentially what your calcit function is doing:

#include "stdafx.h"
#include "iostream"
using namespace std;

int main()
{
    int a, b, c, d, score2 = 0;

    for (a = 0; a < 10; a++)
        for (b = 0; b < 6; b++)
            for (c = 1; c < 2; c++)
                for (d = 0; d < 6; d++)
                    score2 = score2 + abs(b - d);
    cout << score2 << endl;
    system("pause");
    return 0;
}

compile this and run it, you'll get the answer of 700. Let me know if you figure it out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.