Thx for your input.How do you reset the total score for the word to zero?
zandiago 115 Nearly a Posting Maven Featured Poster
Lerner 582 Nearly a Posting Maven
int wordValue = 0;
while(inputAWord)
{
//calculate word value here
//reset wordValue to zero before starting loop again
wordValue = 0;
}
zandiago 115 Nearly a Posting Maven Featured Poster
#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 : "<<(numWords/value)<<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;
}
Would the reset occur for every case statement? Additionally, if it were reset, then to calculate the average word length, which i used the variable, value, it i run it as is, it shows '4', but thats wrong, so would resetting the value display the incorrect word length. Thx for your response.
zandiago 115 Nearly a Posting Maven Featured Poster
I've tried placed the reset in around in the code, and it reverts back to the above, or it shows zero for everything. Thx for your input
zandiago 115 Nearly a Posting Maven Featured Poster
#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();
++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)<<showpoint<<total<<endl;
}
total = 0;
cout<<"The average word length is : "<<(value/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;
}
My outfile:
Words Value
Singer 22.7349
Democrat 54.2115
Republican 72.5616
Mississippi 194.466
Constitution 251.267
Blip 292.758
Computer 321.205
Obsyphylactic 390.552
Promulgation 449.816
Connection 381.638
Pascal 440.206
Crate 470.653
Intractible 549.363
Accommodation 526.365
Catering 533.403
Tyrannosaurus 599.720
Circumlocution 604.783
Mesopotamia 676.106
Rhabdomancy 649.328
Homozygous 679.804
Nanny 611.580
Concentric 558.411
Schistosomiasis 725.572
Occupy 690.772
Cactus 718.628
Lillipution 849.874
Mugwump 849.874
Candelabra 873.254
Enlargement 929.110
Rust 998.766
Academy 993.063
Uncanny 907.438
Delaware 976.293
Contradiction 967.067
Cplusplus 1093.60
Cannery 1047.05
Omnidirectional 1073.21
Overcompensate 1124.05
Clips 1178.62
Sarsparilla 1346.56
Lithostratigraphy 1531.06
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 1663.71
The average word length is : 0
The total # of words from the infile : 58
The average word evaluation is : 0.000000
Thanx for your input AD, however, i still have a few problems:
1. I'm having problems with the averages of the word length & average word evaluation at the bottom of the code.
2. There are several word that show a value of 1531.06 (at the end), but shouldn't have a value, because none of the specified criteria letters are there.
3.How could i use functions to break it down to do the following:
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
Thx much for the assistance.
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
[edit]The line numbers below refer to your post #33
line 30 is showing the value of total before that value has been calculated. Why don't you move that line down to line 110. And it will show the sum of all values for not only the current sentence (or word actually) but all the words read before it. If you want to show the total for just the word that is being displayed then you will need to use a different variable. Since total is already accumulating the value in all those case statements then maybe create another int and name it something like grandTotal then sum it on line 111 and reset total on line 112. Insert the lines below at line 110.
line 111: cout<<"Words"<<setw(30)<<"Value"<<endl<<endl;
line 112: grandTotal += total;
line 113: total = 0;
You will also have to change line 115 to use grandTotal instead of total
zandiago 115 Nearly a Posting Maven Featured Poster
#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<<endl;
while (getline(infile,sentence,'\n'))
{
++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;
}
default:
{
total+=0;
++numChars;
break;
}
}
int value = 0;
int total = 0;
value = sentence.size();
cout<<"Words"<<setw(30)<<"Value"<<endl;
int grandTotal=0;
grandTotal += total;
}
cout<<"The average word length is : "<<(value/numWords)<<endl;
cout<<"The total # of words from the infile : "<<numWords<<endl;
cout<<"The average word evaluation is : "<<(grandTotal/numWords)<<endl;
infile.close();
outfile.close();
return 0;
}
Thanks for your reply, i get the following error messages though:
error C2065: 'value' : undeclared identifier
error C2065: 'grandTotal' : undeclared identifier
Also, is there really a need to put a default case statement to satisfy if there isn't any of the specified letters indicated?
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
haven't you learned anything yet? you have to declare variables before you can use them!
zandiago 115 Nearly a Posting Maven Featured Poster
#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;
int grandTotal=0;
string sentence;
while (getline(infile,sentence,'\n'))
{
++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;
}
default:
{
total+=0;
++numChars;
break;
}
int total = 0;
value = sentence.size();
grandTotal += total;
}
}
cout<<"Words"<<setw(30)<<"Value"<<endl<<endl;
cout<<"The average word length is : "<<(value/numWords)<<endl;
cout<<"The total # of words from the infile : "<<numWords<<endl;
cout<<"The average word evaluation is : "<<(grandTotal/numWords)<<endl;
infile.close();
outfile.close();
return 0;
}
I get the following as my output. Thx for the point abt the declaration of variable.
Words Value
The average word length is : 0
The total # of words from the infile : 58
The average word evaluation is : 0
It doesn't show each individual word their individual value as i want it to. Also, it doesn't show my averages as i wanted to. Thx for your assistance.
zandiago 115 Nearly a Posting Maven Featured Poster
#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;
int grandTotal=0;
string sentence;
cout<<"Words"<<setw(30)<<"Value"<<endl<<endl;
while (getline(infile,sentence,'\n'))
{
value = sentence.size();
total=0;
++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;
}
default:
{
total+=0;
++numChars;
break;
}
int total = 0;
value = sentence.size();
grandTotal += total;
}
cout<<sentence<<setw(30)<<total<<endl;
}
cout<<"The average word length is : "<<(value/numWords)<<endl;
cout<<"The total # of words from the infile : "<<numWords<<endl;
cout<<"The average word evaluation is : "<<(grandTotal/numWords)<<endl;
infile.close();
outfile.close();
return 0;
}
Ok, so i pretty much got it. I think...i'll be doing some manually to ensure that the calculation is correct. The problem now is because i reset some of the values...my averages show zero for the averages, that i have to calculate at the bottom. Thx for your input.
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
Look at lines 128 and 130 and see if you can't figure out why the averages are zero.
zandiago 115 Nearly a Posting Maven Featured Poster
int total = 0;......i deleted that one, but i still have problems with the averages.
tonykjose 0 Newbie Poster
setw(15) what does this command do ?? and also where have u included the command to input characters from the file ???
zandiago commented: are you blind????????? +0
zandiago 115 Nearly a Posting Maven Featured Poster
tonykjose-welcome aboard....however, if you don't know what the comands are for then you may want to read up about them.... But just for simplicity....setw(15) is one way of manipulating the output for formatting, to allow an even width(distance) between your output so it looks clean, such as a column. Your second question...the command to input characters from the file ???.... if you look in my file you'll see the comand (getline(infile,sentence,'\n'))...This website provides great tutorials about the two following questions above. If you have specific questions, please open your own thread and also take a look at our FAQ and forum rules. Happy coding..
zandiago 115 Nearly a Posting Maven Featured Poster
#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;
int grandTotal=0;
string sentence;
cout<<"Words"<<setw(30)<<"Value"<<endl<<endl;
while (getline(infile,sentence,'\n'))
{
value = sentence.size();
total=0;
++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;
}
default:
{
total+=0;
++numChars;
break;
}
grandTotal += total;
}
cout<<sentence<<setw(30)<<total<<endl;
}
cout<<"The average word length is : "<<(numChars/numWords)<<endl;
cout<<"The total # of words from the infile : "<<numWords<<endl;
cout<<"The average word evaluation is : "<<(total)<<endl;
infile.close();
outfile.close();
return 0;
}
Thanks so much guys for the help, however, I'm still having a problem with the average word evaluation. It uses the value from the last word and then divide it by 58(num of words) and outputs that as the average word evaluation. If i use the variable grandTotal and divide it by the num of words....i get zero.
zandiago 115 Nearly a Posting Maven Featured Poster
Ok guys...problem solved. Thx for all the input. i'll be posting my code soon, to see how i can break down this program into different programs and then repost it. I have to provide functions for the following:
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
Thx much for the assistance.
zandiago 115 Nearly a Posting Maven Featured Poster
Actually, if i shifted the printing lines into the while loop..it still shows zero as the answer and the line "The average word evaluation is" prints 50 times because it's inside the help. I'm having a problem with the average evaluation. Thx for your input.
Lerner 582 Nearly a Posting Maven
Try this out. It's your code with the various revisions suggested, plus a few comments and one slight simplification. I've not compiled it, though.
#include <iomanip>
#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 in file
float total = 0.0F;//evaluation of given word
int numChars = 0; //#of chars in file
int value = 0; //#of chars per word
int grandTotal=0; //evaluation of all words
string sentence; //a given word in the file
//assumes input file is one word per line
while (getline(infile,sentence,'\n'))
{
value = sentence.size();
++numWords;
for(int i = 0; i < value; i++)
{
switch(sentence[i])
{
case 'A': case 'a':
total += A;
break;
case 'C': case 'c':
total+= C;
break;
case 'D': case 'd':
total+= D;
break;
case 'E': case 'e':
total+= E;
break;
case 'L': case 'l':
total+= L;
break;
case 'N': case 'n':
total+= N;
break;
case 'R': case 'r':
total+= R;
break;
case 'S': case 's':
total+= S;
break;
case 'T': case 't':
total+= T;
break;
default:
total+=0;
break;
}
++numChars;
}
grandTotal += total;
cout << "current word was " << sentence << " with total = " << total << endl;
cout << "current word had " << value << "number of characters" << endl;
cout << "the average per char for this word was" << total/value << endl;
total=0;
}
cout << endl << endl;
cout<<"The average word length is : "<<(numChars/numWords)<<endl;
cout<<"The total # of words from the infile : "<<numWords<<endl;
cout<<"The average word evaluation is : "<< grandTotal/numWords <<endl;
infile.close();
outfile.close();
return 0;
}
zandiago 115 Nearly a Posting Maven Featured Poster
Thx Lerner, i've completed the assignment and it works perfectly, i'm now just trying to break it 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
zandiago 115 Nearly a Posting Maven Featured Poster
In keeping with the requirements of the program. I need to use the preceeding problem with the use of 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
Please take a look at the following and see why i'm having problems:
#include <iomanip>
#include <cmath>
#include <fstream>
#include<string>
#include<iostream>
using namespace std;
void run();
void show(int grandTotal, int numWords);
int eval (int numChars, int total);
int display (int total, string sentence);
int counter (int numWords);
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;
int grandTotal=0;
string sentence;
cout<<"Words"<<setw(30)<<"Value"<<endl<<endl;
voidrun()
{
while (getline(infile,sentence,'\n'))
{
value = sentence.size();
total=0;
++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;
}
default:
{
total+=0;
++numChars;
break;
}
}
}
grandTotal += total;
int display (int total, string sentence)
{
cout<<sentence<<setw(30)<<total<<endl;
}
}
void show(int grandTotal, int numWords)
{
cout<<"The average word evaluation is : "<<(grandTotal/numWords)<<endl;
cout<<"The average word length is : "<<(numChars/numWords)<<endl;
}
int counter (int numWords)
{
cout<<"The total # of words from the infile : "<<numWords<<endl;
}
infile.close();
outfile.close();
return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
I'm not doing your debugging for you. Look at the error messages and the line numbers and see if you can't see the problem. The first one is on line 42. Why ? I'll let you figure it out yourself. With 359 posts you should be able to do that.
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.