C++ ifstream 嵌套循环

C++ ifstream nested loops

我正在做一个命理学项目,我必须输入一个名字并添加分配给名字的每个字母的值。我已经使用循环 运行 通过字符来处理单个单词。 我现在想通读一个列表并使用 name_value..

获得输出

没有 while 循环或 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;
}

虽然使用 fstream,但我无法通过列表将其获取到 运行。它只是没有从 names.txt 文件中获取值。它不会抛出任何错误。可能有点傻..我想不通..
使用 ifstream 的代码如下

#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;
}

names.txt 是一个包含以下内容的文本文件

   tom
   sally
   mary

它应该显示以下数字

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

你的代码很难看懂,所以我写了下面的,


#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;
}

输出:


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

如果您对范围循环使用感到满意,我使用普通循环是因为您提到您是新手

读取文件:

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

参考:

1)vector

2)Enter EOF in terminal

您没有正确提及所需文件选项的路径。如果您不提供文件选项,那么它也可以工作。 我已经尝试使用完全限定路径的相同代码及其下面的 运行 更改:

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

我得到以下输出:

15
11
8