2017-07-03 173 views
2

优先级:(C++)需要帮助解决输出文件错误

我对此很新颖。我曾尝试阅读其他人的错误,因为我找不到解决办法。当我取出流的位并切换fout为cout然后程序工作正常,但我似乎无法得到它输出到文件。我确实提前制作了这个文件。

次要的:

我想也以某种方式使用1个回路,用于x的范围应在5个步骤0至10在步长为1,10〜50(在SquareMachine功能)。主循环中1位循环的规则相同,0到15为1度递增,15到45为5度递增。我相信有一种技术,我根本没有看到结合我的循环或一个循环......洞..哈哈得到它?无论如何,主要需要输出文件的帮助。

谢谢你的任何意见/援助

错误(S):

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; 


} 
+0

'fOut'是一个自动变量,由'main'函数作用域。它只要'main'一样长,但只能在'main'中看到。第二个错误是一个错字。你在错误的地方丢了一个'.'。 – user4581301

+0

谢谢,我不知道...... dangit!我会尽力现在解决这些错误。 –

+0

嗯,不确定如何解决这个问题。 –

回答

1

你在你的代码的许多错误。大多数优化错误,也有一些错别字。但是总是,你应该首先听你的编译器,因为它可以帮助你找到问题。它旨在这样做!

有时它会在错误的情况下说出你应该做什么(或不该做什么)。

例如你的编译器说:

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

这意味着,在该函数的范围fOut都看不出来。这是因为它是在main()函数中声明的,所以它是一个局部变量(只能在范围内使用)而不是全局的(从任何地方都可用)。如果你也想在其他函数中使用这个变量,那么使用引用或指针是一个好习惯。 (我会建议你使用全局变量只有当你真的需要的话,在特殊情况下)

包括头:(不包括不必要的头)

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

函数原型:

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

从这里开始的函数实现!

(我真的不知道你打算用这个函数来完成):

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

+0

为什么要传递指针而不是引用函数? – UnholySheep

+0

是的,我确实过度使用了这部分。编辑,谢谢! – Adam

+0

OP的“事实”功能不是因子。例如。对于输入16,它计算'16 * 11!',而不是'16!'。我只是假设他想要的是......但是,它仍然存在问题(例如,如果(n <= 15 || n <= 45) –