Having problems getting information to file

This program is designed to anylize any character put into it from an input file as a letter,(capital, lowercase,consonate, vowel), digit (odd or even), and punctuation symbols. All the output should go to a file, unfortunatly the header function is not going to the output file and I do not no why. All it shows is the the character analysis. Any help would be really appreciated





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

int ispunc(char);
int isnumber(char);
int isletter(char);
void ShowProgramHeader(ofstream & outfile);


int main()
{
        ofstream outfile;
        outfile.open("Textout.txt");
        char c;
        ShowProgramHeader(outfile);
        outfile<<c<<endl;

        ifstream infile;
        infile.open ("file.txt");
        while(!infile.eof())
        {
                infile>>c;
        if( isletter(c) == 1)
        {

                return 1;
        }


        isnumber(c);

        ispunc(c);
        }
        infile.close();
        outfile.close();
}

int isletter(char c)
{

        int result = 0;
        ofstream outfile;
        if((c>=65 && c<=90) || (c>=97 && c<=122))
        {
            outfile.open("Textout.txt");
            outfile<<"Character is a letter,"<<endl;
            result = 1;

            if(c>=65 && c<=90)
            {
                outfile<<" upper case,"<<endl;

            }
            else
            {
                outfile<<"lower case,"<<endl;
            }
            if(c==65||c==69||c==73||c==79||c==35)
            {
                outfile<<"and a vowel!"<<endl;
            }
            else
            {
                outfile<<"and a consonant!"<<endl;
            }
        }
        return result;
}


int isnumber(char c)
{
        int result = 0;
        ofstream outfile;
        if (c>=48 & c<=57)
        {
                outfile.open("Textout.txt");
                outfile<<"Character is a digit and"<<endl;
                result = 1;
        }
        if (c%2==0)
        {
                outfile<<"is even"<<endl;
        }
        else
        {
               outfile<<"is odd"<<endl;

        }
        return result;
        outfile.close();
}

int ispunc(char c)
{
        int result = 0;
        ofstream outfile;

        if ((c>=33 && c<=47) || (c>=58 && c<=64) || (c>=91 && c<=96) || (c>=123 && c<=126))
        {
                outfile.open("Textout.txt");
                outfile<<"You Have A Punctuation Character"<<endl;
                result = 1;
        }

        return result;
        outfile.close();
}

void ShowProgramHeader(ofstream & outfile)
{

        outfile<<"Name: Tim Stoekl"<<endl;
        outfile<<"Date: 13 Nov, 2009"<<endl;
        outfile<<"Class: CS 112"<<endl;
}

Last edited on
There are some problems here:

in main you are opening outfile and then in all the functions you are again opening the same file and not closing it. just open it once in main and use the stream object.you can pass the object to all these functions. Also, whenever you return from your program, close all the open file streams.
Also, in line 18 you cannot use 'c' without initializing it.

if (c>=48 & c<=57)
arey you trying to use && here? check for the precedence of the operators.
Last edited on
You shouldn't be defining outfile everywhere.

Here I have modified your code/ I define outfile only once at the beginning as a global.

However it might be better to define it in the main() function and pass it as a parameter to each function you call.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

int ispunc(char);
int isnumber(char);
int isletter(char);
void ShowProgramHeader(ofstream & outfile);

// Global outfile
ofstream outfile;

int main()
{
        char c;
        outfile.open("Textout.txt");
        ShowProgramHeader(outfile);
        outfile<<c<<endl;

        ifstream infile;
        infile.open ("file.txt");
        while(!infile.eof())
        {
                infile>>c;
        if( isletter(c) == 1)
        {

                return 1;
        }


        isnumber(c);

        ispunc(c);
        }
        infile.close();
        outfile.close();
}

int isletter(char c)
{

        int result = 0;
        if((c>=65 && c<=90) || (c>=97 && c<=122))
        {
            outfile<<"Character is a letter,"<<endl;
            result = 1;

            if(c>=65 && c<=90)
            {
                outfile<<" upper case,"<<endl;

            }
            else
            {
                outfile<<"lower case,"<<endl;
            }
            if(c==65||c==69||c==73||c==79||c==35)
            {
                outfile<<"and a vowel!"<<endl;
            }
            else
            {
                outfile<<"and a consonant!"<<endl;
            }
        }
        return result;
}


int isnumber(char c)
{
        int result = 0;
        if (c>=48 & c<=57)
        {
                outfile<<"Character is a digit and"<<endl;
                result = 1;
        }
        if (c%2==0)
        {
                outfile<<"is even"<<endl;
        }
        else
        {
               outfile<<"is odd"<<endl;

        }
        return result;
}

int ispunc(char c)
{
        int result = 0;

        if ((c>=33 && c<=47) || (c>=58 && c<=64) || (c>=91 && c<=96) || (c>=123 && c<=126))
        {
                outfile<<"You Have A Punctuation Character"<<endl;
                result = 1;
        }

        return result;
}

void ShowProgramHeader(ofstream & outfile)
{

        outfile<<"Name: Tim Stoekl"<<endl;
        outfile<<"Date: 13 Nov, 2009"<<endl;
        outfile<<"Class: CS 112"<<endl;
}
It's not necessary to explicitly close a file. When the file object goes out of scope and its destructor gets called, the file is closed automatically.
globals is not a good idea, rather pass them as parameter to functions. how many global's are you going to take.
Alright thanks. No we have not covered globals yet in class, so I think I'll pass them as parameters instead. I'll see if it works.
Topic archived. No new replies allowed.