Correcting the Output of my code.

Hello,

I am really new and need your help with getting this formatted correctly. my output is all messed up.


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 read.

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

example of the out put that is correct.



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




my out put that is not formatting correctly.




BACKWARDS WORDS
rebmeceddecember
rebmeceddecember
7,88,
1022017
siis
ehtthe
TSALLAST
ralugerregular
sslaccalss
yadday
foof
ehtthe
.retSeMeSSeMeSter.
"!!!yarooH""Hooray!!!"
"Hooray!!!"

Words in file: 13

Lowercase vowels in file: 2





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

#include <iostream>
#include <cctype>
#include <string>
#include <iomanip>
using namespace std;

bool islcvowel(char);
void BACKWARDS(string&);
int digitcount(string);




int main() 
{
    string text;
    int digits=0;
    int words=0;
    
    cout <<"BACKWARDS WORDS"<<endl;
    cout <<left;
    cin >>text;
    while (cin)
    {
        
        words++;
        digits = digits + digitcount (text);
        BACKWARDS(text);
        cout << text << endl;
        
        cin >> text;
        
    }
    cout << text << endl;
    cout << endl;
    cout << "Words in file: "<< words << endl;
    cout <<"Lowercase vowels in file: "<< digits << endl;
    return 0;
}


int digitcount (string text)
{
    int total=0;
    char ch;
    int n;
    n=text.size();

    for (int i=0; i<n; i++)
    ch = tolower(text[i]);
    if (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
        total++;
    return total;
}


void BACKWARDS(string& word)
{
    int n;
    n=word.size();
    
    for (int i= n ;i >= 0 ; i--)
       cout<< word[i];
        
}

Last edited on
Too much code. Note: haven't looked into how that split function works so much. Just copied from the internet.

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
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <iterator>

int main()
{
    char text[] = "December 8, 2017 is  the LAST regular class day of the SeMeSter. \"Hooray!!!";
    
    auto number_of_vowels = std::count_if(std::begin(text), std::end(text), [](char c) { return c == 'a' || c == 'e' || c == 'i' || c == 'o'|| c == 'u';  });
    std::cout << "vowels: " << number_of_vowels << std::endl;
    
    std::istringstream iss(text);
    std::vector<std::string> split((std::istream_iterator<std::string>(iss)), std::istream_iterator<std::string>());
    
    std::cout << "count: " << split.size() << std::endl;
    
    for(auto& result : split)
    {
        std::reverse(result.begin(), result.end());
        std::cout << result << std::endl;
    }
}
Last edited on
Hello sr2cute702,

What I found is in the "digitcount" function I put the if statement inside the for loop. Otherwise you are checking the last character of the string with the if statement.

In the function "BACKWARDS" I added a newline after the for loop.

In main line 30 I put a comment on the line because it is not needed ad not doing what you want.

Line 24 does not work well because you need some way to make "cin" fail to end the loop. I changed it to while (text != "end") for now. I tried while (text == "\n"), but that ended the program to soon. It needs worked on.

For me I had to put something before "return 0;" to pause the program to see the final output before the window closed.

Hope that helps,

Andy
Handy Andy,

sorry about the errors you got, were using a redirection function via linux to run the data withing a file.

if you try to pre add the info and run it it will work. i think i got the formating to work but need help with the lower case vowel count not working.


jdoodle.com/a/dme


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

#include <iostream>
#include <cctype>
#include <string>
#include <iomanip>
using namespace std;

bool islcvowel(char);
void BACKWARDS(string&);
int digitcount(string);




int main() 
{
    string text;
    int digits=0;
    int words=0;
    
    cout <<"BACKWARDS WORDS"<<endl;
    cout <<left;
    cin >>text;
    while (cin)
    {
        
        words++;
        digits = digits + digitcount (text);
        BACKWARDS(text);
        
        cin >> text;
        
    }
    cout << endl;
    cout << "Words in file: "<< words << endl;
    cout <<"Lowercase vowels in file: "<< digits << endl;
    return 0;
}


int digitcount (string text)
{
    int total=0;
    char ch;
    int n;
    n=text.size();

    for (int i=0; i<n; i++)
    //if (islower(ch))
        ch = (text[i]);
    if (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
        total++;
    return total;
}


void BACKWARDS(string& word)
{
    int n;
    n=word.size();
    
    for (int i= n ;i >= 0 ; i--)
       cout<< word[i];
    cout << endl;
}




data in file to be read.


December 8, 2017 is 
the LAST regular calss
day of the SeMeSter.
"Hooray!!!"
Then FINALS...




my output now.


BACKWARDS WORDS

rebmeceD
,8
7102
si
eht
TSAL
raluger
sslac
yad
fo
eht
.retSeMeS
"!!!yarooH"
nehT
...SLANIF

1
2
3
4
5
    for (int i=0; i<n; i++)
    //if (islower(ch))
        ch = (text[i]);
    if (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
        total++;

Your for-loop ends after ch = (text[i]);

Secondary note: from my personal point of view, your output is good, but it adds a space at the beginning of every reversed sentence:
BACKWARDS WORDS
 rebmeceD
 ,8
 7102
 si
 eht
 TSAL
 raluger
 sslac
 yad
 fo
 eht
 .retSeMeS
 "!!!yarooH"
 nehT
 ...SLANIF

Maybe that’s irrelevant for your teacher.
enoizat,


your right on the spacing, its not suppose to happen. how do i fix the spacing and the loop stopping at ch = (text[i]); i know moving it messes it up. as far as i know this is looking at each letter withing the text in the file.
how do i fix the spacing

A string is ideally identical to an array of character. In arrays, indexes start from 0.
So, if a string has size 5, it means those five characters ranges from position 0 to position 4.

Now look at your code:
1
2
n=word.size();
for (int i= n ;i >= 0 ; i--)

Inside what range are you moving? Can you spot the problem?

...the loop stopping at ch = (text[i]);

What about braces? :-)
1
2
3
4
5
6
for (int i=0; i<n; i++) { // <-- starting curly bracket    <-------// b
    //if (islower(ch))                                             // l
        ch = (text[i]);                                            // o
    if (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')       // c
        total++;                                                   // k
} // <-- ending curly bracket                              <-------// 

(...and restore indentation)

You said this is an assignment, so if you need the code, I’m afraid you need to explicitely ask for it.
Topic archived. No new replies allowed.