Search code examples
c++ifstream

C++ ifstream nested loops


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..

Solution

  • 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