C ++两个函数程序 - 在两个函数之间来回传递值[debug]

时间:2013-10-04 07:05:09

标签: c++ xcode macos

我认为我正在接近一个更优雅的问题解决方案。我正在尝试创建一个程序,输入数字的文本文件,读取所有的第一个数字,计算值1-9的出现总数,然后显示分析信息。我第一次尝试这样做时,我用9种不同的条件运行了一次analyzeData函数。我已经移动尝试通过countLines(字符串文件名,整数)传递一个基本循环[for(int i; 1< = 9; i ++)],然后显示新的计数。

然而,我已经到了一个点,我认为它已经完成但它不会运行。 我在第59,81和88行有三个错误。我回过头来查看括号和半冒号。我花了相当多的时间和精力来努力创造一些有效的东西,而不是一个巨大的复制和粘贴的厄运。

旁注 - 我在Xcode工作,我能看到的唯一“输出”是因为它不会运行“sh:PAUSE:command not found”。

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

// function to pass digit value to. analyzeData will
// call this function for each digit value 1-9

int countLines(std::string filename, int number) 
{
    int count=0;
    std::ifstream file;
    std::string first_digit;

    file.open( filename.c_str() );
    if ( !file.good() )
    {
        std::cout << "**ERROR: file not found!" << std::endl;
        exit(-1);
    }
    else
    {
        file >> first_digit;

        if (number == 0)// calling analyzeData with number=0 will compute total # lines / input values
        {
            while (!file.eof())
            {
                count++;
            }
        }
        else if (number != 0)
        {
            if (first_digit[0] == number)
            {
                count++;
            }
        }

    file.close();
    return count;
}

void analyzeData (std::string filename)
{
        std::ifstream file;
        std::string first_digit;

        file.open( filename.c_str() );
        if ( !file.good() )
        {
            std::cout << "**ERROR: file not found!" << std::endl;
            exit(-1);
        }
        else
        {
            std::cout << filename << std::endl;
            std::cout << countLines(filename,0) << std::endl; //total lines
            for (int i=1;i <= 9;i++) //sends i through countLines for each digit value
            {
                std::cout << i << ": " << countLines(filename, i) << std::endl;
            }
            std::cout << std::endl;
            file.close()
        }
}

int main(int argc, const char * argv[])
{
    analyzeData("***put file path here***");
    analyzeData("***put file path here***");
    analyzeData("***put file path here***");
    system("PAUSE");
    return EXIT_SUCCESS;
    return 0;
}

欢迎任何帮助或建设性批评。

在我写这篇文章时,我有一个实现的时刻!在第45行,我为字符串索引字符设置了一个条件,使其等同于一个int变量....这必须以某种方式成为错误的来源。任何人都可以告诉我如何使int变量作为字符串读取? .c_str()?

0 个答案:

没有答案
相关问题