c++ - (C++) 需要帮助解决输出文件错误

标签 c++

优先级:

显然,我对此很陌生。我已经尝试阅读其他人的错误以了解我所拥有的但找不到修复方法。当我取出 ofstream 位并将 fOut 切换为 cout 时,程序工作正常,但我似乎无法将其输出到文件。我确实提前做了文件。

中学:

我假设也以某种方式使用 1 个循环,因为 x 的范围应该是 0 到 10,步长为 1,10 到 50,步长为 5(在 SquareMachine 函数中)。 main 中的位的 1 循环规则相同,以 1 度为增量从 0 到 15,以 5 度为增量从 15 到 45。我确信有一种我根本看不到的技术来组合我的循环或者可能是一个循环……洞……明白了吗?无论如何,主要需要输出文件方面的帮助。

感谢您的任何建议/帮助

错误:

week4.cpp: In function ‘void ShowProgramHeader()’:

week4.cpp:34: error: ‘fOut’ was not declared in this scope

week4.cpp: In function ‘int main()’:

week4.cpp:44: error: ‘struct std::ofstream’ has no member named ‘is’

week4.cpp: In function ‘int SquareMachine()’:

week4.cpp:92: error: ‘fOut’ was not declared in this scope

代码:

#include <cmath>
#include<stdlib.h>
#include <iostream>
#include t<ime.h>
#include<cstdlib>
#include<unistd.h>
#include<iomanip>
#include<fstream>


using namespace std;

//Global Variable(s)
long fact(long n);

// Prototype(s)
int SquareMachine();

// Program Header
void ShowProgramHeader()
{

    fOut << "Name" << endl;
    fOut << "Class and Date \n\n\n" << endl;
}


//Command Center
int main()
{

    ofstream fOut( "sTable.out", ios::out| ios::trunc);
    if( fOut.is.open())
    {

            ShowProgramHeader();
            SquareMachine();

            fOut << "Value---Output\n"<<endl;
            for( long t =0; t <=15; t++)
            {
                    fOut << setw(10) << t;
                    fOut << setw(20) << fact(t) << endl;
            }

            for( long t =20; t <=45; t=t+5)
            {
                    fOut << setw(10) << t;
                    fOut << setw(20) << fact(t) << endl;
                    fOut.close();
            }
    }

    else

            cout<<"Unable to Open the file: sTable.out";
            exit(-1);
}


long fact(long n)
{

    if( n ==0 || n==1 )
    return 1;
    else if( n==2 || n <= 15)
    return n * fact( n-1);
    else if( n <=15 || n <=45)
    return n * fact (n-5);


}


int SquareMachine()
{
    double x = 10;
    int n = 2;
    double z;


    fOut << "\nNumber    Sqrt      Exp       Pow\n";
    for ( z=0; z<=x; ++z)
            {
            fOut << setw(10) << left << z << setprecision(2);
            fOut << setw(10) << left << sqrt(z) << setprecision(3);
            fOut << setw(10) << left << exp(z) << setprecision(10);
            fOut << setw(10) << left << pow(z,n) << setprecision(4);
            fOut << "\n" ;
            }
    for ( z=15; z<=50; z= z+5)
            {
            fOut << setw(10) << left << z << setprecision(2);
            fOut << setw(10) << left << sqrt(z) << setprecision(3);
            fOut << setw(10) << left << exp(z) << setprecision(10);
            fOut << setw(10) << left << pow(z,n) << setprecision(4);
            fOut << "\n" ;
            }
     fOut << " \n End of Part 1\n"<< endl;


}

最佳答案

您的代码中有很多错误。主要是优化错误,还有一些错别字。但是总是,您应该首先听取您的编译器的意见,因为它可以帮助您找到问题所在。它就是为此而设计的!

有时它会直接说明在出现错误时您应该做什么(或不应该做什么)。

例如你的编译器说:

week4.cpp: In function ‘void ShowProgramHeader()’:
week4.cpp:34: error: ‘fOut’ was not declared in this scope

这意味着在该函数的作用域中,fOut 是不可见的。这是因为它是在 main() 函数中声明的,所以它是一个局部变量(只在一个范围内可用)而不是全局变量(在任何地方都可用)。如果你也想在其他函数中使用这个变量,最好使用引用或指针。 (我建议您仅在特殊情况下确实需要时才使用全局变量)

包含的 header :(不包含不必要的 header )

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath> // for Mathematical functions

函数原型(prototype):

void ShowProgramHeader(std::ofstream&);
long fact(long);
int SquareMachine(std::ofstream&);

客户端代码:

int main() {
    std::ofstream f_out("sTable.txt", std::ios::out | std::ios::trunc);

    if(f_out.is_open()) {
        ShowProgramHeader(f_out);
        SquareMachine(f_out);

        f_out << std::endl << std::left << std::setw(10) << "Number";
        f_out << std::left << std::setw(10) << "Output" << std::endl;

        long i = 0; // for fact(long)

        while(i <= 45) {
            if(i <= 15 || i >= 20) {
                f_out << std::left << std::setw(10) << i;
                f_out << std::left << std::setw(10) << fact(i) << std::endl;

                if(i <= 15) i++;
                else i += 5;

            } else i++;
        }

        f_out.close();
    }
    else {
        std::cerr << "Unable to Open the file: sTable.out";
        return -1;
    }

    return 0;
}

函数实现从这里开始!

Header(我不太确定你打算用这个函数做什么):

void ShowProgramHeader(std::ofstream& f_out) { f_out << "Name\nClass and Date\n"; }

方机:

int SquareMachine(std::ofstream& f_out) {
    f_out << std::endl << std::left << std::setw(10) << "Number";
    f_out << std::left << std::setw(10) << "Square";
    f_out << std::left << std::setw(20) << "Exp";
    f_out << std::left << std::setw(10) << "Power" << std::endl;

    float i = 0;

    while (i <= 50) {
        if(i <= 10 || i >= 15) {
            f_out << std::left << std::setw(10) << std::setprecision(2) << i;
            f_out << std::left << std::setw(10) << std::setprecision(3) << std::sqrt(i);
            f_out << std::left << std::setw(20) << std::setprecision(10) << std::exp(i);
            f_out << std::left << std::setw(10) << std::setprecision(4) << std::pow(i, 2) << std::endl;

            if(i <= 10) i++;
            else i += 5;
        } else i++;
    }

    f_out << std::endl << "End of Part 1" << std::endl;
}

最后是递归阶乘函数! (如果您打算使用阶乘方法,那么您的解决方案过于复杂了)。另请注意,当您的阶乘值变得如此之大时,您必须处理它。您应该找到一种可以存储比 long 更大的数字的类型!

long fact(long n) {
    if(n <= 1) return 1;
    return n * fact(n - 1);
}

输出(我使用了sTable.txt而不是sTable.out)

Name
Class and Date

Number    Square    Exp                 Power     
0         0         1                   0         
1         1         2.718281746         1         
2         1.41      7.389056206         4         
3         1.73      20.08553696         9         
4         2         54.59814835         16        
5         2.24      148.4131622         25        
6         2.45      403.4288025         36        
7         2.65      1096.633179         49        
8         2.83      2980.958008         64        
9         3         8103.083984         81        
10        3.16      22026.46484         100       
15        3.87      3269017.25          225       
20        4.47      485165184           400       
25        5         7.200490291e+010    625       
30        5.48      1.068647422e+013    900       
35        5.92      1.586013445e+015    1225      
40        6.32      2.353852703e+017    1600      
45        6.71      3.493427058e+019    2025      
50        7.07      5.184705458e+021    2500      

End of Part 1

Number    Output    
0         1         
1         1         
2         2         
3         6         
4         24        
5         120       
6         720       
7         5040      
8         40320     
9         362880    
10        3628800   
11        39916800  
12        479001600 
13        1932053504    // long storage problem starts from here
14        1278945280    // wrong!
15        2004310016    // wrong!
20        -2102132736   // wrong!
25        2076180480    // wrong!
30        1409286144    // wrong!
35        0             // wrong!
40        0             // wrong!
45        0             // wrong!

由于 long 可以包含最大 ~2,1*10^9 的值,但是 13! ~ 6*10^9!

关于c++ - (C++) 需要帮助解决输出文件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44878386/

相关文章:

c++ - 避免输出交错

c++ - std::unique_ptr<type> 和 type&& 有什么区别?

c++ - 在boost几何中正确使用typedef和REGISTER

C++ 进程检查

c++ - 只有默认参数有效

c++ - 我想在程序启动时使用 Qt 打开并读取文本文件

c++ - 常量对象不在 rdata/rodata 部分

c++ - 指向 COM 对象的成员变量指针

c++ - 为什么 std::find_if 和相关函数没有对整个容器进行重载?

c++ - 这是 "*ptr++ = *ptr + a"未定义的行为吗?