2

Priority:

I am quite new at this obviously. I have tried reading other peoples errors for what I have and can't find a fix. When I take out the ofstream bit and switch fOut for cout then program works fine but I cant seem to get it to output to a file. I did make the file in advance.

Secondary:

I am suppose to also somehow use 1 loop for the range of x should be 0 to 10 in steps of 1, 10 to 50 in steps of 5( In the SquareMachine function). Same rule of 1 loop for the bit in main with 0 to 15 in 1 degree increments and 15 to 45 in 5 degree increments. I am sure there is a technique I am simply not seeing to combine my loops or perhaps a loop... hole.. hah get it? Anyway, primarily need assistance with the output file.

Thank you for any advice/assistance

Error(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

Code:

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


}
4
  • fOut is an Automatic variable scoped by the main function. It lives as long as main does, but is only visible in main. The second error is a typo. You dropped a . in the wrong spot. Commented Jul 3, 2017 at 5:36
  • Thank you, I didn't know that... dangit! I will try to fix these errors now. Commented Jul 3, 2017 at 6:07
  • Hmm, Unsure how to remedy this. Commented Jul 3, 2017 at 6:11
  • Welcome to Stackoverflow! Some hints: Only post 1 question per question. If you have two issues, post it in a second question. And don't prefix the title with the language, use tags for that Commented Jul 3, 2017 at 8:47

1 Answer 1

1

You have numerous errors in your code. Mostly optimization errors, also some typos. But always, you should listen to your compiler first, because it helps you find the problem. It is designed to do so!

Sometimes it literally says what you should do (or what not) in a case of error.

For example your compiler says:

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

It means that in that function's scope fOut cannot be seen. It is because it was declared in the main() function, so it is a local variable (only avaiable in a scope) and not global (avaiable from everywhere). If you want to use this variable in other functions too, it is a good practice to use references or pointers. (I would recommend you using global variables only if you really need to do so, in special cases)

Included headers: (don't include unnecessary headers)

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

Function prototypes:

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

Client code:

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

Function implementations from here!

Header (I'm not really sure what you are planning to do with this function):

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

Square machine:

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

And finally the recursive factorial function! (You overcomplicated your solution, if you meant to use the factorial method). Also note that when your factorial's value becomes so big, you have to handle it. You should find a type that can store larger numbers than long!

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

Output (I used sTable.txt instead of 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!

Since long can contain a value up to ~2,1*10^9, however 13! ~ 6*10^9!

Sign up to request clarification or add additional context in comments.

3 Comments

Why are you passing pointers instead of references to the functions?
Yes I indeed overcomlicated that part. Edited, thanks!
The OP's fact function is not the factorial. E.g. for input 16 it calculates 16*11!, not 16!. I just assume that is what he wants... Yet, there are still problems in it (e.g. if (n <= 15 || n <= 45)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.