Hey i'm trying to write code for a word utility program for a uni project and it is bugging the hell out of me.

#include <stdio.h>
#include <string>
#include <iostream>
#include <fstream>
#include <Windows.h>
#include <string.h>
#include <cctype>
#include <algorithm>
#include <vector>
using namespace std;
const int size=300;
int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,i1,option,Mirrorimage,spacenumber=0,characternumber=0,averagelength=0,Uppercase;
string sentence,save,edit,spell,open,line;
char str [80],mirror,name,forwards[size],backwards[size];
int WordCount(string sentence)//Word count function which uses the string sentance to operate on
        int len = sentence.length();             
        for (i= 0; i<len; i++)//starting from the beginning of the string to the last
                if (sentence[i1] == ' ' && sentence[i1-1] != ' ') 
        return spacenumber;
float Charactercount(string sentence)
        float characternumber = sentence.length()-spacenumber;
        return characternumber;
int Charactercountspace(string sentance)
        int characternumber = sentence.length();
        return characternumber;
float Averagelength(string sentence)
        averagelength= (Charactercount(sentence)-WordCount(sentence)+1)/WordCount(sentence);
        return averagelength;
void MirrorImage(char forwards[], char backwards[])
        int length = strlen(forwards);
        for (int i=length-1, j=0; i>=0; i--, j++)
                backwards[j] = forwards[i];
        for (i=0; i<length; i++)
                cout << backwards[i];
int SaveFile()
        ofstream savefile;
        savefile <<"Your sentence: "<<sentence<<" Was "<<WordCount(sentence);
        savefile<<" words long\nAnd had "<<Charactercount(sentence)<<" characters";
        savefile<<"\nWith an average word length of "<<Averagelength(sentence);
        //savefile<<"Written backwards it reads: "<<MirrorImage(sentence)<<endl;
        savefile.close();       //closes the input to the file
        return 0;
int main()
        cout<<"What would you like to do?\n"<<endl;
        cout<<"1.Word count\n"<<endl;
        cout<<"2.Character count\n"<<endl;
        cout<<"3.Chacter count with space \n"<<endl;
        cout<<"4.Calculate average word length\n"<<endl;
        cout<<"5.Spell check\n"<<endl;
        cout<<"6. Mirror image\n"<<endl;
        cout<<"7.Save the file\n"<<endl;
        cout<<"8.Caps Lock\n"<<endl;
                switch (option)
                case 1:
                        cout<<"1.Word count\n"<<endl;
                        cout<<" Enter your first sentence."<<endl;
                        cout<<"There are "<<WordCount(sentence)<<" words in this sentence\n"<<endl;
                case 2:
                        cout<<"2.Character count\n"<<endl;             
                        cout<<" Enter your first sentence."<<endl;
                        getline(cin, sentence, '\n');
                        cout<< "There are "<<Charactercount(sentence)<<" characters in this sentence\n"<<endl;
                case 3:
                        cout<<"3. Character count with spaces\n"<<endl;
                        cout<<" Enter your first sentence."<<endl;
                        getline(cin, sentence, '\n');
                        cout<<"There are "<<Charactercountspace(sentence)<<" characters in this sentance including spaces"<<endl;
                case 4:
                        cout<<"3.Calculate average word length\n"<<endl;
                        cout<<" Enter your first sentence."<<endl;
                        getline(cin, sentence, '\n');
                        cout<<"The average word length is "<<Averagelength(sentence)<<"\n"<<endl;
                case 5:
                        cout<<"4.Spell check\n"<<endl;
                        cout<<"Enter the word you wish to check"<<endl;
                case 6:
                        cout<<"5. Mirror image\n"<<endl;
                        cout<<"\n Enter the text you wish to be reversed:\n"<<endl;
                        cout<<"Your sentence: "<<sentence<<" when reversed is: "<<endl;
                case 7:
                        cout<<"6.Save the file\n"<<endl;
                        cout << "\nEnter the Filename: "<<endl;//Allows the user to input a filename
                        cin >> save;
                        cout<<"You have saved the file in the Utilities folder"<<endl;
                default: main();
                return 0;

Above is my code and well basically i keep coming up with loads of problems; i know it's something i'm dong wrong and its driving me batty. There are a few things that arent working with it and i've spent sooo many hours trying to sort it.

The wordcount() function i created is meant to count the number of spaces in the string "sentence". I included a for loop to remove the effect of double spacing and the like but it still doesnt seem to work properly. Something is telling me that i've defined something incorrectly.

Oh yea and ignore some of the variables at the top that is for functions i havn't coded for yet.

Thanks very much and i don't expect you to write anything just point me in the right direction would be great.


Recommended Answers

All 2 Replies

line 24. [i1] did you mean i+1
line 37. sentence misspelled , you used sentance

btw try keeping global variables to a minimum. Most of those could go into your main() with no problem.

When you call a function such as float Charactercount(string sentence)

it creates a new string called sentence inside your function.
Also such as you make a new float called characternumber.

I didn't finish looking all you code but i'm pretty sure all those global variables can be put inside your main()

WordCount algorithm:

for current character = beginning-of-text to end-of-text
  while current character is white-space
    go to next character
  end while
  if current character is not end-of-text
    increment word count
    while current character is not white-space and not end-of-text
      go to next character
    end while
  end if
end for

This is a pretty simple algorithm, and once you start thinking algorithmically then this stuff gets pretty easy, since translating this pseudo-code into real program code (in almost any programming language) becomes quite simple.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.