So far this is what I have:

#include <iomanip>
#include <cmath>
#include <fstream>
#include<string>
#include<iostream>

using namespace std;


int main()
{
    ifstream infile;
    ofstream outfile;

    infile.open ("Wordcalc.txt");
    outfile.open ("WOutput");

    char A = (1*2.0);//value for letter A
    char C = (3 - 20.4);
    char D = ((4/12.0) *3.09);
    char E = ((5/3.0)+5.0);
    char L = ((12+17)*3.7/2.8)+3.17;
    char N = (14 /4.2-6.12)*8.4;
    char R = (18.0/2);
    char S = ((19*3.0) + 7.0)/2.1;
    char T = (20+10.18);

    string names;
    double wordlength = 0;
    double wordeval = 0;
    double value = 0;
    while (infile>>names)
    {



    cout<<"Words"<<setw(15)<<"Value"<<endl;
    cout<<names<<setw(15)<<value<<endl;
    cout<<"The average word length is : "<<wordlength<<endl;
    cout<<"The average word evaluation is : "<<wordeval<<endl;

    }
      infile.close();
      outfile.close();


    return 0;
}

The assignment:
This program, which must use functions and parameter passing, reads in a list of single
words from a sequential file called "a:\wordcalc.txt". It calculates a word value based upon
formulae for individual selected letters in the word (explained below). The program displays
each word along with its calculated word value. lt then displays a count of the total words,
the average word length and the average word evaluation.
The letter evaluations are based upon the ORDINAL position ofthe CAPITAL letter in the
alphabet (examples: A = 1; B = 2; C = 3; Y = 25; Z = 26). The following are the formulae for
(all other letters have a value of zero):
A ==>ordinal value * 2.0
C ==>ordinal value - 20.4
D ==>ordinal value l 12.0 * 3.09
E ==>ordinal value l 3.0 + 5.0
L ==>((ordinal value + 17.0)* 3.7/2.8)+ 3.17
N ==>(ordinaI value /4.2 - 6.12)* 8.4
R ==>ordinal value /2.0
S ==>((ordinaI value * 3.0)+ 7.0)/2.1
T ==>ordinal value +10.18
Example the word COTTON evaluates as follows
C ==> -17.4
O ==> 0.0
Example: The evaluation of the word COTTON:
C=-17.4
O=0.0
T=30.18
T=30.18
O=0.0
N=-23.408
Evaluation is 19.552

* A switch may may not be used to determine the ordinal value of the letter, but is necessary for the individual letter calcualtion.
Suggested functions:
1 for reading
1 for evaluating
1 for displaying the count
1 for calcualting and displaying the word length average
1 for displaying the word evaluation
* There will not be more than 60 words in the infile.
A few questions:
1. Is my declaration of each letter correct?
2. How do i get the program to evaluation each individual letter value and then move on to the other letter in the same phrase and then unto the next phrase in the infile?
3. How do i store the value and the length of each phrase, so as to calculate the average at the end?
4. What can i use to allow the program to continue even if the letter is in lower case?

All assitance is appreciated.

Recommended Answers

All 50 Replies

>>1. Is my declaration of each letter correct?
No. They should be declared as floats, not char, because you will need the decimal places, and make them const because their values will never change.

const float A = (1.0F * 2.0F);//value for letter A

2. use a loop to look at each letter in the string, then a switch statement to calculate the value. (probably also answers your other questions) Example:

std::string sentence = "How Now Brown Cow.";
float total = 0.0F;
int numChars = 0;
for(int i = 0; i < sentence.size(); i++)
{
   switch(sentence[i])
   {
      case 'A': 
              total += A; 
              ++numChars; 
              break;
      // blabla
    }
}

Ok AD, thx for the jump start. So in my specific case would be something like this:

#include <iomanip>
#include <cmath>
#include <fstream>
#include<string>
#include<iostream>

using namespace std;


int main()
{
	ifstream infile;
    ofstream outfile;

	infile.open ("Wordcalc.txt");
	outfile.open ("WOutput");

	const float A = (1*2.0);//value for letter A
	const float C = (3 - 20.4);
	const float D = ((4/12.0) *3.09);
	const float E = ((5/3.0)+5.0);
	const float L = ((12+17)*3.7/2.8)+3.17;
	const float N = (14 /4.2-6.12)*8.4;
	const float R = (18.0/2);
	const float S = ((19*3.0) + 7.0)/2.1;
	const float T = (20+10.18);
	
	string sentence = "How Now Brown Cow.";
	float total = 0.0F;
	int numChars = 0;
	for(int i = 0; i < sentence.size(); i++)
	{
    switch(sentence[i])
    {
      case 'A': 
              total += A; 
              ++numChars; 
              break;
			  cout<<sentence<<total<<numChars<<endl;
	}
	}
			  

	cout<<"Words"<<setw(15)<<"Value"<<endl;
	cout<<sentence<<setw(15)<<total<<numChars<<endl;
	cout<<"The average word length is : "<<endl;
	cout<<"The average word evaluation is : "<<endl;

	infile.close();
    outfile.close();

 
	return 0;
}

I had tried above with your example, but with no results. Do i need any additional headers? Thx for the input.

You need to complete that switch statement and add the cases for all the other letters. It didn't work because that sentence doesn't have an 'A'.

Good day. Thanks for your input, I've revised my code. I however, do get the wrong total for the word COTTON, that I have as an example, please be a second pair of eyes for me. I've changed the switch statements up. The answer i get in 78, when i try "Cotton", however, when I use them all caps, (i.e."COTTON"), i get a value of 178. The infile will contain a mixture of upper and lower case letters. How can i address that. Neither of those answers above is correct. The answer should be 19.552. In the example above, using the word cotton, there are no O's in my statements, so i'm assuming that it will assign those letters a zero. Thanks for the input.

#include <iomanip>
#include <cmath>
#include <fstream>
#include<string>
#include<iostream>

using namespace std;


int main()
{
	ifstream infile;
    ofstream outfile;

	infile.open ("Wordcalc.txt");
	outfile.open ("WOutput");

	const float A = (1*2.0);//value for letter A
	const float C = (3 - 20.4);
	const float D = ((4/12.0) *3.09);
	const float E = ((5/3.0)+5.0);
	const float L = ((12+17)*3.7/2.8)+3.17;
	const float N = (14 /4.2-6.12)*8.4;
	const float R = (18.0/2);
	const float S = ((19*3.0) + 7.0)/2.1;
	const float T = (20+10.18);
		
	string sentence = "COTTON";
	float total = 0.0F;
	int numChars = 0;
	for(int i = 0; i < sentence.size(); i++)	
    switch(sentence[i])
    {
      case 'A': 
		  {
              total += A; 
              ++numChars;
	       }
	case 'C':
		{
			total+= C;
			++numChars;
			
		}
	case 'D':
		{
			total+= D;
			++numChars;
			
		}
	case 'E':
		{
			total+= E;
			++numChars;
			
		}
	case 'L':
		{
			total+= L;
			++numChars;
			
		}
	case 'N':
		{
			total+= N;
			++numChars;
			
		}
	case 'R':
		{
			total+= R;
			++numChars;

		}
	case 'S':
		{
			total+= S;
			++numChars;
			
		}
	case 'T':
		{
			total+= T;
			++numChars;
			
		}

	
}

	cout<<"Words"<<setw(15)<<"Value"<<endl;
	cout<<sentence<<setw(15)<<total<<setw(5)<<numChars<<endl;
	cout<<"The average word length is : "<<endl;
	cout<<"The average word evaluation is : "<<endl;

	infile.close();
    outfile.close();

 
	return 0;
}

Hey again zandiago. Watch your syntax.

switch (sentence[ i ]) {
  case 'A': case 'a':
    total += A;
    ++numChars;
    break;
  case 'C': case 'c':
    total += C;
    ++numChars;
    break;
  ...
  }

You are correct in that if a char is not in the switch then nothing is added to your total (nor numChars).

I would also add a case for whitespace and other end of word markers to reset total and work on the average stuff per word.

Hope this helps.

Duoas-thank you very much for your input, and forgive me for not officially welcoming you to the forum earlier. Thx again for your inout, i'll be reposting in a few. Thx again.

Ok, so how do i now calculate the average length of the word and also the average of the evaluation? In this case i only have one word, however, in my infile, i have like 60 words, that i'll need to find the average of.

If you want to calculate the average word length then you will need another counter to sum up the length of all words read. After finished reading just divide total length by total number of words read.

>>average of the evaluation
After finished reading the file divide total by numChars

Thanks for your input AD. I'm just now trying to displaying the count of words (from infile), calcualting and dis playing the word length average and displaying the word evaluation average. Here is what i've tried:

#include <iomanip>
#include <cmath>
#include <fstream>
#include<string>
#include<iostream>

using namespace std;


int main()
{
	ifstream infile;
    ofstream outfile;

	infile.open ("Wordcalc.txt");
	outfile.open ("WOutput");

	const float A = (1*2.0);//value for letter A
	const float C = (3 - 20.4);
	const float D = ((4/12.0) *3.09);
	const float E = ((5/3.0)+5.0);
	const float L = ((12+17)*3.7/2.8)+3.17;
	const float N = (14 /4.2-6.12)*8.4;
	const float R = (18.0/2);
	const float S = ((19*3.0) + 7.0)/2.1;
	const float T = (20+10.18);
	int numWords = 0;//# of words
		
	string sentence;
	while (!infile.eof())
	{
	float total = 0.0F;
	int numChars = 0;
	for(int i = 0; i < sentence.size(); i++)	
    switch(sentence[i])
    {
	case 'A':case 'a': 
		  {
              total += A; 
              ++numChars;
			  break;
			  numWords+=numWords;
	       }
	case 'C': case 'c':
		{
			total+= C;
			++numChars;
			break;
			numWords+=numWords;
			
		}
	case 'D': case 'd':
		{
			total+= D;
			++numChars;
			break;
			numWords+=numWords;
			
		}
	case 'E': case 'e':
		{
			total+= E;
			++numChars;
			break;
			numWords+=numWords;
			
		}
	case 'L': case 'l':
		{
			total+= L;
			++numChars;
			break;
			numWords+=numWords;
			
		}
	case 'N':case 'n':
		{
			total+= N;
			++numChars;
			break;
			numWords+=numWords;
			
		}
	case 'R':case 'r':
		{
			total+= R;
			++numChars;
			break;
			numWords+=numWords;

		}
	case 'S':case 's':
		{
			total+= S;
			++numChars;
			break;
			numWords+=numWords;
			
		}
	case 'T':case 't':
		{
			total+= T;
			++numChars;
			break;
			numWords+=numWords;
			
		}
	}
	}

	cout<<"Words"<<setw(15)<<"Value"<<endl;
	cout<<sentence<<setw(15)<<total<<setw(5)<<endl;
	cout<<"The average word length is : "<<sentence.size()/numWords<<endl;
	cout<<"The total # of words from the infile : "<<numWords<<endl;
	cout<<"The average word evaluation is : "<<(total/numChars)<<endl;
	

	infile.close();
    outfile.close();

	

 
	return 0;
}

A few things:
-It's saying that total & numChars are undeclared.
-Is my use of numWords ok-use to calcualte the # of words?
-Are my calcualtions for the averages ok?
-What if if i want to break down this program into the following function:
1 for reading
1 for evaluating
1 for displaying the count
1 for calcualting and displaying the word length average
1 for displaying the word evaluation
Thanks for your assistance.

>>numWords+=numWords;
wrong syntax. should be this: ++numWords; . And move that line outside the switch statement, between lines 33 and 34. Then delete all those other likes where that occurs. You only want to count the number of words, not the number of characters.


>>It's saying that total & numChars are undeclared.
you have them declared in the wrong place. Move lines 32 and 33 outside that loop at line 27.

>>sentence.size()/numWords<<endl;
wrong. you need to keep track of the total length of all sentences read. What you have here is just the length of the last sentence. You will need another int variable to do that.

line 30 is also wrong. It only checks for eof but never actually reads the file. Correct it like this:

while ( getline(infine, sentence) )
{
   // your code here
}

Actually, that loop is also wrong. Your program is supposed to be counting words, not sentences. So you need something like this

string word;

while( infile >> word )
{

}

Thanks, a quick observation though, look at this code:

#include <iomanip>
#include <cmath>
#include <fstream>
#include<string>
#include<iostream>

using namespace std;


int main()
{
	ifstream infile;
    ofstream outfile;

	infile.open ("Wordcalc.txt");
	outfile.open ("WOutput");

	const float A = (1*2.0);//value for letter A
	const float C = (3 - 20.4);
	const float D = ((4/12.0) *3.09);
	const float E = ((5/3.0)+5.0);
	const float L = ((12+17)*3.7/2.8)+3.17;
	const float N = (14 /4.2-6.12)*8.4;
	const float R = (18.0/2);
	const float S = ((19*3.0) + 7.0)/2.1;
	const float T = (20+10.18);
	int numWords = 0;//# of words
	float total = 0.0F;
	int numChars = 0;
		
	string sentence = "Cotton";
	{
	++numWords;
	for(int i = 0; i < sentence.size(); i++)	
    switch(sentence[i])
    {
	case 'A':case 'a': 
		  {
              total += A; 
              ++numChars;
			  break;
			  
	       }
	case 'C': case 'c':
		{
			total+= C;
			++numChars;
			break;
			
			
		}
	case 'D': case 'd':
		{
			total+= D;
			++numChars;
			break;
						
		}
	case 'E': case 'e':
		{
			total+= E;
			++numChars;
			break;
						
		}
	case 'L': case 'l':
		{
			total+= L;
			++numChars;
			break;
						
		}
	case 'N':case 'n':
		{
			total+= N;
			++numChars;
			break;
						
		}
	case 'R':case 'r':
		{
			total+= R;
			++numChars;
			break;
			
		}
	case 'S':case 's':
		{
			total+= S;
			++numChars;
			break;
						
		}
	case 'T':case 't':
		{
			total+= T;
			++numChars;
			break;					
		}
	}
	}

	cout<<"Words"<<setw(20)<<"Value"<<endl;
	cout<<sentence<<setw(15)<<total<<endl;
	cout<<"The average word length is : "<<sentence.size()/numWords<<endl;
	cout<<"The total # of words from the infile : "<<numWords<<endl;
	cout<<"The average word evaluation is : "<<(total/numWords)<<endl;
	

	infile.close();
    outfile.close();

	

 
	return 0;
}

The output comes like this:

Words               Value
Cotton         19.552
The average word length is : 6
The total # of words from the infile : 1
The average word evaluation is : 19.552

The above output...let us say is acceptable. although i forced the sum of total words to be 1. However when i change my program to:

#include <iomanip>
#include <cmath>
#include <fstream>
#include<string>
#include<iostream>

using namespace std;


int main()
{
	ifstream infile;
    ofstream outfile;

	infile.open ("Wordcalc.txt");
	outfile.open ("WOutput");

	const float A = (1*2.0);//value for letter A
	const float C = (3 - 20.4);
	const float D = ((4/12.0) *3.09);
	const float E = ((5/3.0)+5.0);
	const float L = ((12+17)*3.7/2.8)+3.17;
	const float N = (14 /4.2-6.12)*8.4;
	const float R = (18.0/2);
	const float S = ((19*3.0) + 7.0)/2.1;
	const float T = (20+10.18);
	int numWords = 0;//# of words
	float total = 0.0F;
	int numChars = 0;
		
	string sentence;
	while (infile>>setence)
	{
	++numWords;
	for(int i = 0; i < sentence.size(); i++)	
    switch(sentence[i])
    {
	case 'A':case 'a': 
		  {
              total += A; 
              ++numChars;
			  break;
			  
	       }
	case 'C': case 'c':
		{
			total+= C;
			++numChars;
			break;
			
			
		}
	case 'D': case 'd':
		{
			total+= D;
			++numChars;
			break;
						
		}
	case 'E': case 'e':
		{
			total+= E;
			++numChars;
			break;
						
		}
	case 'L': case 'l':
		{
			total+= L;
			++numChars;
			break;
						
		}
	case 'N':case 'n':
		{
			total+= N;
			++numChars;
			break;
						
		}
	case 'R':case 'r':
		{
			total+= R;
			++numChars;
			break;
			
		}
	case 'S':case 's':
		{
			total+= S;
			++numChars;
			break;
						
		}
	case 'T':case 't':
		{
			total+= T;
			++numChars;
			break;					
		}
	}
	}

	cout<<"Words"<<setw(20)<<"Value"<<endl;
	cout<<sentence<<setw(15)<<total<<endl;
	cout<<"The average word length is : "<<sentence.size()/numWords<<endl;
	cout<<"The total # of words from the infile : "<<numWords<<endl;
	cout<<"The average word evaluation is : "<<(total/numWords)<<endl;
	

	infile.close();
    outfile.close();

	

 
	return 0;
}

I get the following as output:(the only alteration to the code is the use of infile):
output:

Words               Value
Retatteration        1663.71
The average word length is : 0
The total # of words from the infile : 58
The average word evaluation is : 28.6847

Now in this case, all of the above is wrong except for the # of words in the infile.

I'm also going to see how the program can be broken down into the following functions:
1 for reading
1 for evaluating
1 for displaying the count
1 for calcualting and displaying the word length average
1 for displaying the word evaluation

you have to also correct line 106 as I mentioned previously. sentence is only the value of the last word read from the file so its length is not appropriate for that calculation.

If you want reading and evaulation to be two different functions then you need to read the file into an array of strings (probably a vector) so that the evaluation function can see them.

maybe just a silly question...but if sentence is only the value of the last word read from the file, why in my previos post did it print '0' as the average length and the value as being 1663.71?

because the length of the last word read == Retatteration -- has 12 letters and the program read 58 words, so 12/58 = 0 (integer arithmetic drops the fractional part).

#include <iomanip>
#include <cmath>
#include <fstream>
#include<string>
#include<iostream>

using namespace std;


int main()
{
	ifstream infile;
    ofstream outfile;

	infile.open ("Wordcalc.txt");
	outfile.open ("WOutput");

	const float A = (1*2.0);//value for letter A
	const float C = (3 - 20.4);
	const float D = ((4/12.0) *3.09);
	const float E = ((5/3.0)+5.0);
	const float L = ((12+17)*3.7/2.8)+3.17;
	const float N = (14 /4.2-6.12)*8.4;
	const float R = (18.0/2);
	const float S = ((19*3.0) + 7.0)/2.1;
	const float T = (20+10.18);
	int numWords = 0;//# of words
	float total = 0.0F;
	int numChars = 0;
			
	string sentence;
	while (infile>>sentence)
	{
	++numWords;
	for(int i = 0; i < sentence.size(); i++)	
    switch(sentence[i])
    {
	case 'A':case 'a': 
		  {
              total += A; 
              ++numChars;
			  break;
			  
	       }
	case 'C': case 'c':
		{
			total+= C;
			++numChars;
			break;
			
			
		}
	case 'D': case 'd':
		{
			total+= D;
			++numChars;
			break;
						
		}
	case 'E': case 'e':
		{
			total+= E;
			++numChars;
			break;
						
		}
	case 'L': case 'l':
		{
			total+= L;
			++numChars;
			break;
						
		}
	case 'N':case 'n':
		{
			total+= N;
			++numChars;
			break;
						
		}
	case 'R':case 'r':
		{
			total+= R;
			++numChars;
			break;
			
		}
	case 'S':case 's':
		{
			total+= S;
			++numChars;
			break;
						
		}
	case 'T':case 't':
		{
			total+= T;
			++numChars;
			break;					
		}
	}
	}
	int sentlength = sentence.length();//number of individual characters

	cout<<"Words"<<setw(20)<<"Value"<<endl;
	cout<<sentence<<setw(15)<<total<<endl;
	cout<<"The average word length is : "<<sentlength<<endl;
	cout<<"The total # of words from the infile : "<<numWords<<endl;
	cout<<"The average word evaluation is : "<<(total/numWords)<<endl;
	

	infile.close();
    outfile.close();

	

 
	return 0;
}

Thanks for the assitance. I've got a bit of issue with the infile. A few things:
1. My average and value are way off.
2. The program only shows the last word from the file(why doesn't it show all the individual words and their respective individual letter)?

I also tried putting in my inline code, and still shows without the line #'s like how AD had edit it.

This is how to get the line numbers
[ code=cplusplus]

>>. The program only shows the last word from the file(why doesn't it show all the individual words and their respective individual letter)?

because you didn't tell it to do that. Recall that the >> operator deletes the previous value stored in the string them reads the next word from the file and saves it there. If you want to display all the lines then you have to call cout just after the word is read.

Oh goodness, forgot all about that 'cout' statement. But then i couldn't 'cout' the value, because it has to go through the switch statements to carry out the calculations?

cout doesn't destroy the value of the string -- your program can do lots of things with it. You could put a cout statement right after line 34 if all you want to do is display the value of the string.

Thanks for your input AD, here goes my revised code, howver, as of now the calcualtions are wrong and the output shows the value(which is wrong) and then the name:

#include <iomanip>
#include <cmath>
#include <fstream>
#include<string>
#include<iostream>

using namespace std;


int main()
{
	ifstream infile;
    ofstream outfile;

	infile.open ("Wordcalc.txt");
	outfile.open ("WOutput");

	const float A = (1*2.0);//value for letter A
	const float C = (3 - 20.4);
	const float D = ((4/12.0) *3.09);
	const float E = ((5/3.0)+5.0);
	const float L = ((12+17)*3.7/2.8)+3.17;
	const float N = (14 /4.2-6.12)*8.4;
	const float R = (18.0/2);
	const float S = ((19*3.0) + 7.0)/2.1;
	const float T = (20+10.18);
	int numWords = 0;//# of words
	float total = 0.0F;
	int numChars = 0;
			
	string sentence;
	while (infile>>sentence)
	{
		cout<<sentence<<endl;
		cout<<total;
		
	++numWords;
	
	for(int i = 0; i < sentence.size(); i++)	
    switch(sentence[i])
    {
	case 'A':case 'a': 
		  {
              total += A; 
              ++numChars;
			  break;
			  
	       }
	case 'C': case 'c':
		{
			total+= C;
			++numChars;
			break;
			
			
		}
	case 'D': case 'd':
		{
			total+= D;
			++numChars;
			break;
						
		}
	case 'E': case 'e':
		{
			total+= E;
			++numChars;
			break;
						
		}
	case 'L': case 'l':
		{
			total+= L;
			++numChars;
			break;
						
		}
	case 'N':case 'n':
		{
			total+= N;
			++numChars;
			break;
						
		}
	case 'R':case 'r':
		{
			total+= R;
			++numChars;
			break;
			
		}
	case 'S':case 's':
		{
			total+= S;
			++numChars;
			break;
						
		}
	case 'T':case 't':
		{
			total+= T;
			++numChars;
			break;					
		}
	}
	}
	int sentlength = sentence.length();//number of individual characters

	cout<<"Words"<<setw(20)<<"Value"<<endl;
	cout<<"The average word length is : "<<sentlength<<endl;
	cout<<"The total # of words from the infile : "<<numWords<<endl;
	cout<<"The average word evaluation is : "<<(total/numWords)<<endl;
	

	infile.close();
    outfile.close();

	

 
	return 0;
}

My calculations are wrong. I wanted the output to be formatted like this (as an example):

[U][I][B]WORDS[/B][/I][/U]           [U][B][I]VALUE[/I][/B][/U]

Narue                                              45
acientdragon                                  100
zandiago                                          32
ect...

Then at the end:

cout<<"The average word length is : "<<sentlength<<endl;
	cout<<"The total # of words from the infile : "<<numWords<<endl;
	cout<<"The average word evaluation is : "<<(total/numWords)<<endl;

Thanks for your input.

delete line 35, it has no useful purpose there.

variable sentlength on line 107 is declared in the wrong place -- declare it after line 29 and initialize it to 0 so that it can be used inside the loop. Then on line 35 sum up the number of characters in all sentences with the += operator.

#include <iomanip>
#include <cmath>
#include <fstream>
#include<string>
#include<iostream>

using namespace std;


int main()
{
	ifstream infile;
    ofstream outfile;

	infile.open ("Wordcalc.txt");
	outfile.open ("WOutput");

	const float A = (1*2.0);//value for letter A
	const float C = (3 - 20.4);
	const float D = ((4/12.0) *3.09);
	const float E = ((5/3.0)+5.0);
	const float L = ((12+17)*3.7/2.8)+3.17;
	const float N = (14 /4.2-6.12)*8.4;
	const float R = (18.0/2);
	const float S = ((19*3.0) + 7.0)/2.1;
	const float T = (20+10.18);
	int numWords = 0;//# of words
	float total = 0.0F;
	int numChars = 0;
	int value = 0;//# character in individual word
			
	string sentence;

	cout<<"Words"<<setw(30)<<"Value"<<endl<<endl;	

	while (getline(infile,sentence))
	{
		value = sentence.size();
				
	++numWords;
		
	for(int i = 0; i < sentence.size(); i++)	
    switch(sentence[i])
    {
	case 'A':case 'a': 
		  {
              total += A; 
              ++numChars;
			   break;
			  
	       }
	case 'C': case 'c':
		{
			total+= C;
			++numChars;
			break;
			
			
		}
	case 'D': case 'd':
		{
			total+= D;
			++numChars;
			break;
						
		}
	case 'E': case 'e':
		{
			total+= E;
			++numChars;
			break;
						
		}
	case 'L': case 'l':
		{
			total+= L;
			++numChars;
			break;
						
		}
	case 'N':case 'n':
		{
			total+= N;
			++numChars;
			break;
						
		}
	case 'R':case 'r':
		{
			total+= R;
			++numChars;
			break;
			
		}
	case 'S':case 's':
		{
			total+= S;
			++numChars;
			break;
						
		}
	case 'T':case 't':
		{
			total+= T;
			++numChars;
			break;					
		}
	}
	}
	

	cout<<sentence<<setw(30)<<total<<endl;
	cout<<"The average word length is : "<<value<<endl;
	cout<<"The total # of words from the infile : "<<numWords<<endl;
	cout<<"The average word evaluation is : "<<(total/numWords)<<endl;
	cout<<value<<endl;
	

	infile.close();
    outfile.close();

	

 
	return 0;
}

The thing is it shows the value for all 58 words in the infile. It doesn't show the actual word. As of right now, it shows the last word in the infile and then beside it how it's value to be the total for the entire infile.

If you want to output each word as it is read in you need an output statement doing that as each word is read in.

You are using total to be both the total value of each word and the total value of all words, and you can't have it both ways. You could use the variable value to accumulate the worth of each word and total to accumulate the worht of all words combined. Be sure value is set to zero each time through the loop.

#include <iomanip>
#include <cmath>
#include <fstream>
#include<string>
#include<iostream>

using namespace std;


int main()
{
	ifstream infile;
    ofstream outfile;

	infile.open ("Wordcalc.txt");
	outfile.open ("WOutput");

	const float A = (1*2.0);//value for letter A
	const float C = (3 - 20.4);
	const float D = ((4/12.0) *3.09);
	const float E = ((5/3.0)+5.0);
	const float L = ((12+17)*3.7/2.8)+3.17;
	const float N = (14 /4.2-6.12)*8.4;
	const float R = (18.0/2);
	const float S = ((19*3.0) + 7.0)/2.1;
	const float T = (20+10.18);
	int numWords = 0;//# of words
	float total = 0.0F;
	int numChars = 0;
	int value = 0;//# character in individual word
			
	string sentence;

	cout<<"Words"<<setw(30)<<"Value"<<endl<<endl;	

	while (getline(infile,sentence,'\n'))
	{
		value = sentence.size();
		cout<<sentence<<setw(30)<<showpoint<<total<<endl;
		cout<<value<<endl;
				
	++numWords;
		
	for(int i = 0; i < sentence.size(); i++)	
    switch(sentence[i])
    {
	case 'A':case 'a': 
		  {
              total += A; 
              ++numChars;
			   break;
			  
	       }
	case 'C': case 'c':
		{
			total+= C;
			++numChars;
			break;
			
			
		}
	case 'D': case 'd':
		{
			total+= D;
			++numChars;
			break;
						
		}
	case 'E': case 'e':
		{
			total+= E;
			++numChars;
			break;
						
		}
	case 'L': case 'l':
		{
			total+= L;
			++numChars;
			break;
						
		}
	case 'N':case 'n':
		{
			total+= N;
			++numChars;
			break;
						
		}
	case 'R':case 'r':
		{
			total+= R;
			++numChars;
			break;
			
		}
	case 'S':case 's':
		{
			total+= S;
			++numChars;
			break;
						
		}
	case 'T':case 't':
		{
			total+= T;
			++numChars;
			break;					
		}
	}
	}
	cout<<"The average word length is : "<<endl;
	cout<<"The total # of words from the infile : "<<numWords<<endl;
	cout<<"The average word evaluation is : "<<(total/numWords)<<endl;
	
	infile.close();
    outfile.close();
 
	return 0;
}

Thx for your input. reset the total score for the word to zero. i noticed that even for sentences which have none of the specified letters, it shows for it'sm value, the value for the last sentence in the infile. Thx for your assistance.

I edited my recent post with additional information and slightly altered recommendation.

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.