Help on void functions and functions and my code

Hello,

I am really lost in getting my code to work as well as using void functions and non main loop/value returning functions out side of the main. I took a stab at writing the code but it won't work.


this is what the code has to due.

• count the number of words in the file (a word is a consecutive series of non-whitespace
characters)
• display each word in the file backwards, 1 per line (for example, "the" would be displayed
as "eht")
• count the number of lowercase vowels in the file (vowels are a, e, i, o, and u)


the rules of the code build up are.

• the program must make use of functions and pass parameters (no global variables)
• there must be a void function that when passed a string (a word from the file) will display
the characters in the string backwards
• there must be a value-returning function that when passed a string (a word from the file)
will return a count of how many lowercase vowels are in the string
• all header files for library functions used by the program must be included
• additional functions are optional
• the file can only be read ONE time



example file that might be load.

december 8, 2017 is
the LAST regular calss
day of the SeMeSter.
"Hooray!!!"

example of the out put.

rebmeceD
,8
7102
si
eht
TSAL
raluger
ssalc
yad
fo
eht
, retSeMeS
"!!!!yarooH"





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
#include <string>
#include <iostream>
using  namespace std;

int isvowel (char);
int count_words(string);
void reversewords (string);


int main()
{
   
cout << "Word count in file = "<<count_words<< endl;
cout << "vowel count in file = "<< isvowel<< endl;

   return 0;
}

void reversewords (string input)

{
    
      
   // output it in reverse order
   for( int i = input.length(); i >= 0; -- i )
   {
      // output the char one by one according to character's index ( i-th letter in the word )
      cout << input[i];
   }
   
}


int isvowel (char ch)

{

    for(int a = 0; a>= 0; a--)
    {
        if (ch == 'a' || ch == 'e' || ch == 'o' || ch == 'i' || ch == 'u')
        
        isvowel++;
    }

return isvowel;

}



int count_words( string s ) 
{
    int word_count( 0 );
    stringstream ss( s );
    string word;
    while( ss >> word ) ++word_count;
    return word_count;
}


Last edited on
a void function just returns nothing in its name.

consider:
void foo() //no return, no parameters, just prints to the screen.
{
cout <<"stuff" << endl;
}

or

void bar (int &x)
{
x = 5;
}
...
int z = 0;
bar(z); //z is 5 after this executes -- the 5 is returned back into the parameter as pass by reference.

pointers (and the name of an array is a pointer of sorts) are reference by default. Some other things may be as well, but nothing you will see for a while.

Last edited on
Thanks for the info now how do I fix my broken code
start by saying what does not work.

the only thing I see right off is a misunderstanding of string.length.

I think you want this pattern:
for( j = astring.length()-1; j>=0; j--)
cout << astring[j];


arrays, vectors, and strings, and things like that index from 0.
so if its length is 10, it is indexed from 0-9 (inclusive) which is 10 total things (0,1,2,3,4,5,6,7,8,9).
Last edited on
Registered users can post here. Sign in or register to post.