-1

I am working on a numerology project where i have to input a name and add values assigned to each letter of the name. I have already got it working for a single word using a loop to run through the characters. I now want to read through a list and get an output with the name_value..

code without the while loop or ifstream.

#include <iostream>
#include <string>
using namespace std;

//struct for multiple data types
struct chaldean {
    string myword;
    bool in_mychar(string test)
    {
        for (unsigned int strpos = 0; strpos < myword.length(); strpos++) {
            if (myword.substr(strpos, 1) == test) {
                return true;
            }
        }
        return false;
    }
};

//function declaration
int value(chaldean chl[], string& st);

int main()
{
    string chaldstrings[8] = { "aijqy", "bckr", "gls", "dmt", "en", "uvwx", "oz", "fhp" };
    chaldean chald[8];
    string name{ 0 };
    int name_value{ 0 };
    cout << "Enter a name : ";
    cin >> name;
    for (int n = 0; n < 8; n++) {
        chald[n].myword = chaldstrings[n];
    }
    name_value = value(chald, name);
    cout << name_value << endl;
    return 0;
}
//function definition
int value(chaldean chl[], string& st)
{
    int total = 0;
    for (int n = 0; n < 8; n++) {
        for (unsigned int s = 0; s < st.length(); s++) {
            if (chl[n].in_mychar(st.substr(s, 1))) {
                total += n + 1;
            }
        }
    }
    return total;
}

I am not able to get it to run through the list though using fstream.. it just is not taking the values from names.txt file.. It does not throw any error..probably something silly.. i am not able to figure out..
code using ifstream as under

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

//struct for multiple data types
struct chaldean {
    string myword;
    bool in_mychar(string test)
    {
        for (unsigned int strpos = 0; strpos < myword.length(); strpos++) {
            if (myword.substr(strpos, 1) == test) {
                return true;
            }
        }
        return false;
    }
};

//function declaration
int value(chaldean chl[], string& st);

int main()
{
    string chaldstrings[8] = { "aijqy", "bckr", "gls", "dmt", "en", "uvwx", "oz", "fhp" };
    chaldean chald[8];
    string name{ 0 };
    int name_value{ 0 };
    ifstream input_file("names.txt");
    while (input_file >> name) {
        for (int n = 0; n < 8; n++) {
            chald[n].myword = chaldstrings[n];
        }
        name_value = value(chald, name);
        cout << name_value << endl;
    }
    return 0;
}
//function definition
int value(chaldean chl[], string& st)
{
    int total = 0;
    for (int n = 0; n < 8; n++) {
        for (unsigned int s = 0; s < st.length(); s++) {
            if (chl[n].in_mychar(st.substr(s, 1))) {
                total += n + 1;
            }
        }
    }
    return total;
}

The names.txt is a text file containing the following

   tom
   sally
   mary

it should display the following numbers

15
11
8

The working of the output is as under
letters a,i,j,q,y have the value 1
letters b,c,k,e have the value 2
similarly all the alphabets are valued between 1 to 8
tom would mean t =4, o=7, and m=5.... therefore 4+7+4=15..
  • 1
    Why are you using an array of `chaldean`s? Just use a `std::vector` instead. – cigien Oct 12 '20 at 13:47
  • Please edit the code as readable. – srilakshmikanthanp Oct 12 '20 at 13:54
  • Think about your `for` loop, what is it really doing? What is it supposed to do? Does it really need to run for *every* line of input? – Some programmer dude Oct 12 '20 at 13:55
  • I am sorry .. i am new to C++.. r u saying that what i am trying to do does not work on arrays.. or just that vectors are easier to loop through ? – radhika iyer Oct 12 '20 at 13:56
  • 1
    Please explain what it should do! What is the input and expected output. – Marek R Oct 12 '20 at 14:01
  • @Marek R.. Hi.. It should take the names tom, sally and mary from names.txt file and display 15, 11,8 .. I have edited the post .. – radhika iyer Oct 12 '20 at 14:06
  • @Srilakshmikanthan.. Hello.. I am new to this forum.. apologies if i am making mistakes in posting.. is the code not readable.. do i need to increase font ? .. – radhika iyer Oct 12 '20 at 14:07
  • Addition to input and output, Why it should display 15,11,8 that is what you want to do with the names ?. – srilakshmikanthanp Oct 12 '20 at 14:08
  • @radhikaiyer we do not read your mind. This numerology is a total mastery to us. And code is understandable not because of font, but by bad formatting (I've fixed that), bad naming of symbols and unclear intention what this code should do. – Marek R Oct 12 '20 at 14:10
  • @Someprogrammerdude.. there are three input values in my input file.. names.txt.. so it should read through each record yes.. what i thought was while the data is being read from the file.. each input line should be broken up and letters assigned from mapped values (i could be wrong in the way i am approaching it :) – radhika iyer Oct 12 '20 at 14:11
  • @MarekR - thank you for helping a newbie out...post this i have to output each persons name.. convert the numbers to a single digit and give some qualities which are predefined from 1 to 9.. I am stuck at this level.. @ Srilakshmikanthan.. .. hope this answers your query also – radhika iyer Oct 12 '20 at 14:17
  • Could you please edit the post by explaining the things, what is the relation between input string and output number for example, **what is the relation between tom and 15, How did you manually do the the work to convert tom to 15** ?. – srilakshmikanthanp Oct 12 '20 at 14:25
  • @ Sri lakshmi kanthan.. I have added the relation between the name and numbers.. also included manual calculation.. – radhika iyer Oct 12 '20 at 14:33
  • https://stackoverflow.com/a/63931843/2129218 – Build Succeeded Oct 12 '20 at 15:06
  • @BuildSucceeded.. thanks.. tried that.. still doesnt work.. – radhika iyer Oct 12 '20 at 15:11

2 Answers2

0

Your code is hard to understand so i have write the following one,


#include <iostream>
#include <string>
#include <vector>

const std::vector<std::string> value{"aijqy", "bckr", "gls", "dmt",
                                     "en", "uvwx", "oz", "fhp"};

void read_names(std::istream &in, std::vector<std::string> &obj)
{
    std::string name;

    while (std::getline(in, name))
    {
        obj.push_back(name);
    }
}

void calc_value(const std::vector<std::string> &names, std::vector<int> &values)
{
    for (size_t i = 0; i < names.size(); i++)
    {
        int val = 0;

        for (size_t j = 0; j < names[i].size(); j++)
        {
            for (size_t k = 0; k < 8; k++)
            {
                if (value[k].find(names[i][j]) != std::string::npos)
                {
                    val += (k + 1);
                    break;
                }
            }
        }

        values.push_back(val);
    }
}

int main()
{
    std::vector<std::string> names;
    std::vector<int> values;

    read_names(std::cin, names);
    calc_value(names, values);

    for (size_t i = 0; i < values.size(); i++)
    {
        std::cout << values[i] << std::endl;
    }

    return 0;
}

output:


tom
sally
mary
(ctrl+d) for eof in linux
15
11
8

If you are comfort with range loop use that, i used ordinary loop because you mentioned that you are newbie

To Read From File:

std::fstream file("names.txt");
read_names(file, names);

Reference:

1)vector

2)Enter EOF in terminal

srilakshmikanthanp
  • 2,231
  • 1
  • 8
  • 25
  • Rather, newbies should use range-`for` instead of indexing, because it removes one more place where they can get the indexing wrong or get different index variables mixed up, etc. – underscore_d Oct 12 '20 at 15:13
  • @Sri laksmi kanthan.. Hey.. Thanks.. I must be missing something here.. is this reading from a file ?.. can you point out where it is reading from names.txt ... or any input file.. – radhika iyer Oct 12 '20 at 15:28
  • It will work with both Files and Terminal For Terminal You Shuld press `ctrl + d` for linux and `ctrl + z` for windows. For file just replace with fstream or ifstream object with `std::cin` like above. – srilakshmikanthanp Oct 12 '20 at 15:32
  • And i run your code now It also gives same but is did't know what is the problem and i did't want debug your code because it is hard to understand. Feel Free to use above code. – srilakshmikanthanp Oct 12 '20 at 15:34
  • @underscore_d Thanks for your suggestion, The reason to use ordinary loop is I did't know OP did know about range loop or not. So why i used ordinary loop. OP can Learn Range loop by searching google. – srilakshmikanthanp Oct 12 '20 at 15:40
  • @radhikaiyer Feel free to accept any of answer, if you solved your problem. – srilakshmikanthanp Oct 12 '20 at 15:55
  • @ Srilakshmi kanthan.. @ Build suceeded.. Hey guys.. its working.. Thanks a lot.. I really appreciate taking time to help out a new comer!! – radhika iyer Oct 12 '20 at 16:00
0

You have not mentioned the path correctly with the required file option. If you not give file option then as well it works. I have tried same code with fully qualified path and its running with below change:

 ifstream input_file("C:\\Users\\Debug\\names.txt", ios::in);

I got the below output:

15
11
8
Build Succeeded
  • 1,153
  • 1
  • 10
  • 24