C++ evaluation of a string

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

39 Replies - 1787 Views - Last Post: 23 October 2007 - 07:20 PM Rate Topic: -----

#1 zandiago  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 190
  • Joined: 13-July 07

C++ evaluation of a string

Post icon  Posted 18 October 2007 - 07:17 AM

So fat 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.

Is This A Good Question/Topic? 0
  • +

Replies To: C++ evaluation of a string

#2 zandiago  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 190
  • Joined: 13-July 07

Re: C++ evaluation of a string

Posted 18 October 2007 - 09:06 AM

Any help is appreciated.
Was This Post Helpful? 0
  • +
  • -

#3 zandiago  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 190
  • Joined: 13-July 07

Re: C++ evaluation of a string

Posted 18 October 2007 - 09:43 PM

Good day...why does the following only show the value for the first letter? Are my switch statements ok?
#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 i = 0;
	
	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; 
			  break;

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

	}
	}
	{
	switch(sentence[i])
	{
	case 'D':
			total+= D;
			++numChars;
			break;

	}
	}
	{
	switch(sentence[i])
	{
	case 'E':
			total+= E;
			++numChars;
			break;

	}
	}
	{
	switch(sentence[i])
	{
	case 'L':
			total+= L;
			++numChars;
			break;

	}
	}
	{
	switch(sentence[i])
	{
	case 'N':
			total+= N;
			++numChars;
			break;

	}
	}
	{
	switch(sentence[i])
	{
	case 'R':
			total+= R;
			++numChars;
			break;

	}
	}
	{
	switch(sentence[i])
	{
	case 'S':
			total+= S;
			++numChars;
			break;

	}
	}
	{
	switch(sentence[i])
	{
	case 'T':
			total+= T;
			++numChars;
			break;

	}
	}

	

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

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

 
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#4 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: C++ evaluation of a string

Posted 18 October 2007 - 10:48 PM

Your switch/case expressions are not right, no. You don't need a new switch statement before every case. The entire purpose of the switch-case control statement is to allow for the condition execution of code based on a variable that can have multiple values. In this case, your test variable is the character in question; the individual cases are the the different values that it can take:
switch (sentence[i]) {
	case 'A': {
		//execute code for 'A'
	}
	case 'C': {
		//execute code for 'C'
	}
	case 'D': {
		//execute code for 'D'
	}
}

Notice that I have NOT enclosed the ENTIRE statement in braces - you have done this with each of your switch-case statements (and frequently elsewhere, if I recall other threads correctly). The group of cases associated with the switch is enclosed in braces, as is the code body for each of the cases.

Your way of doing it has resulted in the problem that only your first switch-case is included in the for-loop, possibly as a result of the improper indentation of the code. This is probably the cause of your problem with only the value for the first letter showing up. The code runs through the loop, looking at each character, but the only case actually within the loop is 'A', which doesn't exist in your string. Then, after the loop has completed, it runs through each of the switch-case expressions for each of the other possible values, and uses the index value = 0, since this is the value you declared at the program level after all of your constants. Since sentence[i]=='C', only the first value is accounted for.

I would recommend getting rid of the int i=0 initialization after the declaration of all of your constants. You are redeclaring in your for loop with different scope, which is just asking for trouble. And since the entire switch-case (and with only a single switch statement, remember) will be within the loop once you've made these changes, you don't need the variable i elsewhere.

Just remember - a single switch statement can have many cases.

Hope that helps :)

-jjh
Was This Post Helpful? 0
  • +
  • -

#5 zandiago  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 190
  • Joined: 13-July 07

Re: C++ evaluation of a string

Posted 19 October 2007 - 04:59 AM

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;
}


Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: C++ evaluation of a string

Posted 19 October 2007 - 07:11 AM

The following gives the answer 19.522...note the differences in the case statement (the breaks).A default case has also been implemented. Case insensitivity has also been added.

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

using namespace std;


int main()
{

	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':
	  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;
			
		}
		default:
		{
				total+=0;
				++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;
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#7 zandiago  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 190
  • Joined: 13-July 07

Re: C++ evaluation of a string

Posted 19 October 2007 - 07:37 AM

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.
Was This Post Helpful? 0
  • +
  • -

#8 zandiago  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 190
  • Joined: 13-July 07

Re: C++ evaluation of a string

Posted 19 October 2007 - 11:34 AM

#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;
}[ code=cplusplus]
#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;
}


I'm still having a bit of problems with this. All assistance is apprecaited. The program isn't working.
Was This Post Helpful? 0
  • +
  • -

#9 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: C++ evaluation of a string

Posted 19 October 2007 - 11:46 AM

Please elaborate as to what is meant by not working. Can you post the exact errors?
Was This Post Helpful? 0
  • +
  • -

#10 zandiago  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 190
  • Joined: 13-July 07

Re: C++ evaluation of a string

Posted 19 October 2007 - 11:56 AM

With the following code, it reads to infile ok.
#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;
	cout<<"Words"<<setw(30)<<"Value"<<endl;
	
	while (infile>>sentence)
	{
		
	++numWords;
	cout<<sentence<<endl;
	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<<"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;
}


The errors/problems:
1. The program does print out each word from the infile, but the value of each of the individual words.
2. Wit regards to the average word length, it only does it for the last word of the infile. i want it to average the length of all the words in the infile.
3. it does read the correct number of sentences from the infile and the correct lenght of that word of the infile.
4. The average word evaluation is wrong.
Thanks for your input.
Was This Post Helpful? 0
  • +
  • -

#11 zandiago  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 190
  • Joined: 13-July 07

Re: C++ evaluation of a string

Posted 19 October 2007 - 01:31 PM

Here is what i've downgraded to, just to get the program to work:
#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 sentlength = 0;//number of individual characters
			
	string sentence="COTTON";
	cout<<"Words"<<setw(30)<<"Value"<<endl<<endl;
	
	
	{
		
	++numWords;	
	cout<<sentence<<setw(30)<<total<<endl;
	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 : "<<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 problems:
1. it doesn't show the total for the word cotton, but it does show it at the bottom(average).
2.If i change it to the infile, it doesn't work.
I tried using:
total+=total;


That doesn't work. Thx for your input.
Was This Post Helpful? 0
  • +
  • -

#12 zandiago  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 190
  • Joined: 13-July 07

Re: C++ evaluation of a string

Posted 19 October 2007 - 03:51 PM

#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.
Was This Post Helpful? 0
  • +
  • -

#13 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: C++ evaluation of a string

Posted 19 October 2007 - 04:06 PM

In your input from the file, you aren't getting a word, you are getting a line. THe default behavior for getline with no specified termination character is that it will read in everything up to the first newline character '\n'. So it runs through everything in the file, totaling everything up.

Then, because your display statements are after the loop ends (this should sound familiar), only the last "word" (i.e. line) is outputted. However, because the total was tallied over the entire file, the total value for the file is displayed.

If you want to display each word and it's total, you need to move your display statement for that step into your while loop, and reset the total score for the word to zero at the start of each loop. If you also want to keep track of the total score for the entire file, you need to have another variable, initialized to zero before the loop starts, that you increment with the value of the current word each time the while loop completes a round.

You can use getline(infile,sentence," ") with a space as a delimiter to extract words from the file one at a time; otherwise you may want to try using strtok() with a series of delimiting characters (all punctuation, spaces, newlines, etc.) to extract a single word from your sentence for processing. A summary of the usage of strtok can be found here. Note that this function works only with C-style strings, not the std::string. To use it you'll have to pass sentence.c_str(), which returns a non-modifiable C-style string based on your std::string.

Hope that helps,

-jjh
Was This Post Helpful? 0
  • +
  • -

#14 zandiago  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 190
  • Joined: 13-July 07

Re: C++ evaluation of a string

Posted 19 October 2007 - 06:12 PM

"reset the total score for the word to zero at the start of each loop"...how do i do that? I kinna get whay your saying because cout doesn't destroy the value of the string. Below is my 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;
	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;
}


Ok, i fixed the infile part of it, so it reads all the sentences in the infile. However, in addition to resetting the value, 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.
Was This Post Helpful? 0
  • +
  • -

#15 zandiago  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 190
  • Joined: 13-July 07

Re: C++ evaluation of a string

Posted 21 October 2007 - 06:08 AM

#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;
						
	++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;
}


Good day.
1. We're almost there, but how do i reset the value each time it completes the loop so that it doesn't carry over into the new selection? In other words, How do you reset the total score for the word to zero?
2. I guess the above question would also resolve the problem that even words which have none of the specified letters(which should give zero), somehow shows a value. Also, the first word from my infile doesn't show a value. Below is a sample of my output:
Words						 Value

Singer					  0.000000
Democrat					   22.7349
Republican					   54.2115
Mississippi					   72.5616
Constitution					   194.466
Blip					   251.267
Computer					   292.758
Obsyphylactic					   321.205
Promulgation					   390.552
Connection					   449.816
Pascal					   381.638
Crate					   440.206
Intractible					   470.653
Accommodation					   549.363
Catering					   526.365
Tyrannosaurus					   533.403
Circumlocution					   599.720
Mesopotamia					   604.783
Rhabdomancy					   676.106
Homozygous					   649.328
Nanny					   679.804
Concentric					   611.580
Schistosomiasis					   558.411
Occupy					   725.572
Cactus					   690.772
Lillipution					   718.628
Mugwump					   849.874
Candelabra					   849.874
Enlargement					   873.254
Rust					   929.110
Academy					   998.766
Uncanny					   993.063
Delaware					   907.438
Contradiction					   976.293
Cplusplus					   967.067
Cannery					   1093.60
Omnidirectional					   1047.05
Overcompensate					   1073.21
Clips					   1124.05
Sarsparilla					   1178.62
Lithostratigraphy					   1346.56
Bimbo					   1531.06
Fib					   1531.06
Xbox					   1531.06
Mummy					   1531.06
Pigbox					   1531.06
Gumbo					   1531.06
Quibbomb					   1531.06
Fogbow					   1531.06
Highjump					   1531.06
Oomph					   1531.06
Hobby					   1531.06
Bumpy					   1531.06
High					   1531.06
Fuzzy					   1531.06
Hopi					   1531.06
Gummy					   1531.06
Retatteration					   1531.06
The average word length is :
The total # of words from the infile : 58
The average word evaluation is : 28.6847


Thanks for all the assistance.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3