What am I missing to complete this program

http://i1209.photobucket.com/albums/cc397/DeathLigerX/Lab8.png
I just want an idea of what I am doing wrong so I can finish this up today
Edited Dec.6 4:28PM EST
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
119
120
121
122
123
#include<iostream>
#include<cctype>
#include<fstream>
#include<stdlib.h>
#include<cstdio>
using namespace std;
struct Count
{
         int countChars; //Counts characters
         int countNW; //counts nonwhite spaces
         int countAlpha; //counts # of alphabetical letters in use
         int countDigits; //counts digits
         int countPunctuation; //counts punctuation
};

int countChars(Count&);
int countNW(Count&);
int countAlpha(Count&);
int countDigits(Count&);
int countPunctuation(Count&);

int main()
{
        string Total_Character;
        string Alphabetic_Character;
        string Punctuation;
        int i; //repeater
        ifstream input;
        ofstream output;
        string fname;

        //Intro
        cout << "Welcome to this program.\n";
        cout << "Please enter text file you use.\n";
        cin >> fname;
        //opening file if correct
        input.open(fname.c_str());
        if(input.fail())
        {
                cout << "Not a valid filename\n";
                exit(0);
                cout << "Not a valid filename\n";
                exit(0);
        }
        char line[256];  //number of lines in the file being read
        char delims[]= ",\t\n"; // delimeters: space, ',', tab, return

        while (input.getline(line, 256))
        {
                printf("lines %d\n", line);
                countChars(Count, line);
                countNW(Count, line);
                countAlpha(Count, line);
                countDigits(Count, line);
                countPunctuation(Count, line);
        }
}
/* Function: countChars
 * Input: characters
 * Output:Counts letters on the line
 * Description: Counts letters in each line
*/
int countChar(Count& Count, char line[])
{
        int c=0; //Repeater
        char letter; //each individual letters in the line
        while(line[c] != '\0')
        {
                c++;
        }
        return c;
}
/* Function: countNW
 * Input:Spaces and returns
 * Output: Count the number of non white spaces
 * Description: Counts number of non white spaces
*/
int countNW(Count& CountNW, int line[])
{
    int d=0; //Repeater
    while(line[d] != '\0')
    {
        d++;
    }
}
/* Function: countAplha
 * Input:Alphabetical charachters
 * Output:Counts the number of alphabetical characters
 * Description:
*/
int countAlpha(Count& countAlpha, int line[])
{
        int e=0; //Repeater
        while(line[e] != '\0')
        {
                e++;
        }
}
/* Function: countDigits
 * Input:numbers
 * Output:Counts the number of numbers usage
*/
int countDigits(Count& countDigits, int line[])
{
    int f=0;
    while (line[f] != '\0')
    {
            f++;
    }
    return 0;
}
/* Function: countPunctuation
 * Input:Punctuations
 * Output:
*/
int countPunctuations(Count& countPunctuation, int line[])
{
        int g=0; //repeater
        while (line[g] != '\0')
        {
            g++;
        }
}
Last edited on
Any suggestions and what I am missing from my coding?
Your functions doesn't returns its values or even uses your struct. The first one is almost correct(countChar), but it's checking for "0" instead of "\0", which the end of a line.


Anyway, check the link below. There you may find some useful stuff.


http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters
I'm getting a "error: expected primary-expression before ',' token"
on line 51-55, what is wrong with it?
Your code has to many incorrect syntax.
For example:

1. Function definitions and declarations are different for every function.

Declaration at line:16 int countChars(Count&);
Definition at line:61 int countChar(Count& Count, char line[])
And passing some other thing at line:49 countChars(Count, line);
Total out of sync!

2. Use of forward slash (/) instead of backward slash (\) for null char.

3. At line:120 input >>
1
2
countChars(Count, line);
// and the function calls that succeeds this 


you are trying to pass the structure itself (Count) to the function.

int countChars(Count&);

This function accepts an object of Count, so you should actually declare an object of count then pass it to the function.

while( input.getline( line, 256 ) )

The array line has a length of 80 characters, you set the streamsize argument in getline() as 256 which can cause buffer overflow. to remedy this problem, you should :
a. either decrease the streamsize of getline to 80 or change line size to 256

or much better

b. use the std::string type from the header <string> as the type of line, use the other version of getline()
which is getline( cin, line ) to parse the text


printf("lines %d\n", line);
use cout instead of this, it will make your life easier

My current code right now is this
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
119
120
121
122
123
124
#include<iostream>
#include<cctype>
#include<fstream>
#include<stdlib.h>
#include<cstdio>
using namespace std;
struct Count
{
         int countChars; //Counts characters
         int countNW; //counts nonwhite spaces
         int countAlpha; //counts # of alphabetical letters in use
         int countDigits; //counts digits
         int countPunctuation; //counts punctuation
}Counts;
int readChars(ifstream&, Count[]);
int countChars(Count&, char line[]);
int countNW(Count&, char line[]);
int countAlpha(Count&, char line[]);
int countDigits(Count&, char line[]);
int countPunctuation(Count&, char line[]);

int main()
{
        string Total_Character;
        string Alphabetic_Character;
        string Punctuation;
        int i; //repeater
        ifstream input;
        ofstream output;
        string fname;

        //Intro
        cout << "Welcome to this program.\n";
        cout << "Please enter text file you use.\n";
        cin >> fname;
        //opening file if correct
        input.open(fname.c_str());
        if(input.fail())
        {
                cout << "Not a valid filename\n";
                exit(0);
                cout << "Not a valid filename\n";
                exit(0);
        }
        char line[256];  //number of lines in the file being read
        char delims[]= ",\t\n"; // delimeters: space, ',', tab, return

        while (input.getline(line, 256))
        {
                cout << "lines, line";
                countChars(Counts, line);
                countNW(Counts, line);
                countAlpha(Counts, line);
                countDigits(Counts, line);
                countPunctuation(Counts, line);
        }
}
/* Function: countChars
 * Input: characters
 * Output:Counts letters on the line
 * Description: Counts letters in each line
*/
int countChar(Count& countChar, int line[])
{
        int c=0; //Repeater
        char letter; //each individual letters in the line
        while(line[c] != '\0')
        {
                c++;
        }
        return c;
}
/* Function: countNW
 * Input:Spaces and returns
 * Output: Count the number of non white spaces
 * Description: Counts number of non white spaces
*/
int countNW(Count& CountNW, int line[])
{
    int d=0; //Repeater
    while(line[d] != '\0')
    {
        d++;
    }
}
/* Function: countAplha
 * Input:Alphabetical characters
 * Output:Counts the number of alphabetical characters
 * Description:
*/
int countAlpha(Count& countAlpha, int line[])
{
        int e=0; //Repeater
        while(line[e] != '\0')
        {
                e++;
        }
}
/* Function: countDigits
 * Input:numbers
 * Output:Counts the number of numbers usage
*/
int countDigits(Count& countDigits, int line[])
{
    int f=0;
    while (line[f] != '\0')
    {
            f++;
    }
    return 0;
}
/* Function: countPunctuation
 * Input:Punctuations
 * Output:
*/
int countPunctuations(Count& countPunctuation, int line[])
{
        int g=0; //repeater
        while (line[g] != '\0')
        {
            g++;
        }
}

I am having a hard time trying to get it to use the stuff in the structure
I'm having a hard time with the bold part too.
Last edited on
Anyone?
Hey for all functions in declaration line[] is of char type whereas in definition its in int type. It should be char line[].

Line63:
int countChar(Count& countChar, int line[])

which should be
int countChar(Count& countChar, char line[])

as declered and called with char line[]

Similarly for every count function, as it is unable to link with those functions.
Topic archived. No new replies allowed.