counting digits

I need the count_digits function to count the number of digits in a series of strings. The function seems logically sound to me but my program is no compiling. I get errors for invalid initialization and passing arguments. Any assistance would be greatly 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
#include <iostream>
#include <string>
#include <cctype>
#include <fstream>

using namespace std;

bool at_endl(ifstream&);
void skip_blanks(ifstream&);
void reformat(string&);
void getfilename(string,string&);
void count_digit(string&,int);

int main()
{
    string word, filename;
    char ch;
    int sum;
    ifstream input;
    getfilename("input",filename);
    input.open(filename.c_str());
    input >> word;
    while (!input.eof())
        {
            reformat(word);
            count_digit(sum);
            cout << word;
            skip_blanks(input);
            if (at_endl(input))
                {
                    cout << endl;
                }
            else
                cout << " ";
            input >> word;
        }
            cout << sum;
    return 0;
}

void getfilename(string filetype, string& filename)
{
    cout << "Enter the name of " << filetype << " file\n";
    cin >> filename;
}

bool at_endl(ifstream& input)
{
    return (input.peek() == '\n');
}

void skip_blanks(ifstream& input)
{
    char ch;
    while (input.peek() == ' ')
        input.get(ch);
}

void reformat(string& word)
{
    int wlength;
    wlength = word.length();
    word[0] = toupper(word[0]);
    for (int i=1; i<wlength; i++)
        word[i] = tolower(word[i]);
}

void count_digit(string& word, int sum)
{
    int wlength, sum = 0.0;
    char ch;
    wlength = word.length();
    for (int i=1; i<wlength; i++)
    {
        if (isdigit(word[i]))
            sum = sum + 1;
    }
}
What exactly are the errors?
H:\C++ Examples\formatfile.cpp||In function `int main()':|
H:\C++ Examples\formatfile.cpp|26|error: invalid initialization of reference of type 'std::string&' from expression of type 'int'|
H:\C++ Examples\formatfile.cpp|12|error: in passing argument 1 of `void count_digit(std::string&, int)'|
H:\C++ Examples\formatfile.cpp||In function `void count_digit(std::string&, int)':|
H:\C++ Examples\formatfile.cpp|70|error: declaration of 'int sum' shadows a parameter|
H:\C++ Examples\formatfile.cpp|70|warning: converting to `int' from `double'|
||=== Build finished: 3 errors, 1 warnings ===|
On line 26 you are passing an int to count digit as the first parameter instead of a string.

Also in your count_digit function, you aren't returning sum from it or passing it by reference, so you won't actually change the value of it in main. You also have a different int called sum inside the function that isn't the same as the parameter (hence the 'int sum' shadows a parameter error)
Topic archived. No new replies allowed.