首页 > 解决方案 > C++ ifstream 嵌套循环

问题描述

我正在从事一个命理项目,我必须输入一个名称并添加分配给名称的每个字母的值。我已经使用循环遍历字符来处理单个单词。我现在想通读一个列表并获得带有 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 文件中获取值。它不会抛出任何错误。可能是一些愚蠢的东西。我无法弄清楚out..
使用 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..

标签: c++ifstream

解决方案


您的代码很难理解,所以我编写了以下代码,


#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)向量

2)在终端输入EOF


推荐阅读