C++/c++ switch, and functions
Expert: Zlatko - 11/12/2009
QuestionQUESTION: My syntax for the switch statement looks correct but I get case 2, 3, ... label not in switch statement. I also need a function that finds the longest word in a file and returns the word and number of characters in the word to the function call. So far my program looks like this.
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
//prototypes
//all parameters are strings, because
//input is taken from a file
int countW (string);
int countC (string);
int sumInt (string);
double avgInt (string);
int maxInt (string);
int minInt (string);
string wordL (string);
int main()
{
ifstream inFile;
int wordCount, charCount, intSum, choice;
intMax, intMin;
double intAvg;
string fileName, lWord;
do
{
do
{
cout << "1. Count Words In a File" << endl;
cout << "2. Count Characters In a File" << endl;
cout << "3. Sum Integers In a File" << endl;
cout << "4. Average Integers In a File" << endl;
cout << "5. Maximum Integer In a File" << endl;
cout << "6. Minimum Integer In a File" << endl;
cout << "7. Longest Word In a File" << endl;
cout << "8. Quit" << endl << endl;
cout << "Choice : ";
cin >> choice;
cout << "Enter the input file name: ";
cin >> fileName;
inFile.open(fileName);
if (inFile.fail())
{
cout << "Error opening input file --" << fileName
<< "please try again!" << endl;
}
}
while (inFile.fail())
switch(choice) //function calls
{
case 1:
wordCount = countW(fileName);
cout << "Word Count = " <<
wordCount <<endl;
break;
case 2:
charCount = countC(fileName);
cout << "Char Count = " <<
charCount << endl;
break;
case 3:
intSum = sumInt(fileName);
cout <<"Sum of Integers = " <<
intSum <<endl;
break;
case 4:
intAvg = avgInt(fileName);
cout <<"Average of Integers = " <<
intAvg << endl;
break;
case 5:
intMax = maxInt(fileName);
cout << "Maximum of Integers = "<<
intMax << endl;
break;
case 6:
intMin = minInt(fileName);
cout <<"Minimum of Integers = " <<
intMin << endl;
break;
case 7:
lWord = wordL(fileName);
cout << "Longest Word = " <<
lWord << " characters" << endl;
break;
default:
cout << "Invalid Input" << endl;
break;
}
}
while (option != 8)
inFile.close();
return 0;
}
//function definitions
int countW(string fileName)
{
int wordCount = 0;
string word;
ifstream inFile;
inFile.open(fileName.c_str());
inFile >> word;
while (inFile)
{
wordCount = wordCount + 1;
inFile >> word;
}
return wordCount;
}
int countC(string fileName)
{
int charCount = 0;
char character;
ifstream inFile;
inFile.open(fileName.c_str());
inFile >> character;
while (inFile)
{
charCount = charCount + 1;
inFile >> character;
}
return charCount;
}
int sumInt(string fileName)
{
int intSum = 0, integer;
ifstream inFile;
inFile.open(fileName.c_str());
inFile >> integer;
while (inFile)
{
intSum = intSum + integer;
inFile >> integer;
}
return intSum;
}
double avgInt(string fileName)
{
double intAvg;
int integer, intSum = 0, number = 0;
ifstream inFile;
inFile.open(fileName.c_str());
inFile >> integer;
while (inFile)
{
intSum = intSum + integer;
number = number + 1;
inFile >> integer;
}
intAvg = intSum / number;
return intAvg;
}
int maxInt(string fileName)
{
int intMax=0, num;
ifstream inFile;
inFile.open(fileName.c_str());
inFile >> num;
while (inFile)
{
if (num > intMax)
intMax = num;
inFile >> num;
}
return intMax;
}
int minInt(string fileName)
{
int intMin, num;
ifstream inFile;
inFile.open(fileName.c_str());
inFile >> num;
intMin = num;
while (inFile)
{
if (num <= intMin)
intMin = num;
inFile >> num;
}
return intMin;
}
string wordL(string fileName)
{
string lWord, word;
ifstream inFile;
inFile.open(fileName.c_str());
while (inFile)
{
}
return lWord;
}
this last function will be to compute the longest word. It is incomplete.
ANSWER: Chris, I corrected the syntax errors. My comments start with //---
I did not check any logic.
I'm not sure if you want me to do anything else. Can you take it from here ? Post a follow up question if you need more help.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string> //--- needed for cin >> string
using namespace std;
//prototypes
//all parameters are strings, because
//input is taken from a file
int countW (string);
int countC (string);
int sumInt (string);
double avgInt (string);
int maxInt (string);
int minInt (string);
string wordL (string);
int main()
{
ifstream inFile;
int wordCount, charCount, intSum, choice;
int intMax, intMin; //--- always specify the type
double intAvg;
string fileName, lWord;
do
{
do
{
cout << "1. Count Words In a File" << endl;
cout << "2. Count Characters In a File" << endl;
cout << "3. Sum Integers In a File" << endl;
cout << "4. Average Integers In a File" << endl;
cout << "5. Maximum Integer In a File" << endl;
cout << "6. Minimum Integer In a File" << endl;
cout << "7. Longest Word In a File" << endl;
cout << "8. Quit" << endl << endl;
cout << "Choice : ";
cin >> choice;
cout << "Enter the input file name: ";
cin >> fileName;
inFile.open(fileName.c_str()); //--- need to send char* to open.
if (inFile.fail())
{
cout << "Error opening input file --" << fileName
<< "please try again!" << endl;
}
}
while (inFile.fail()); //--- in do/while, always end the while with a semicolon
switch(choice) //function calls
{
case 1:
wordCount = countW(fileName);
cout << "Word Count = " <<
wordCount <<endl;
break;
case 2:
charCount = countC(fileName);
cout << "Char Count = " <<
charCount << endl;
break;
case 3:
intSum = sumInt(fileName);
cout <<"Sum of Integers = " <<
intSum <<endl;
break;
case 4:
intAvg = avgInt(fileName);
cout <<"Average of Integers = " <<
intAvg << endl;
break;
case 5:
intMax = maxInt(fileName);
cout << "Maximum of Integers = "<<
intMax << endl;
break;
case 6:
intMin = minInt(fileName);
cout <<"Minimum of Integers = " <<
intMin << endl;
break;
case 7:
lWord = wordL(fileName);
cout << "Longest Word = " <<
lWord << " characters" << endl;
break;
default:
cout << "Invalid Input" << endl;
break;
}
}
while (choice != 8); //--- changed option to choice
inFile.close();
return 0;
}
//function definitions
int countW(string fileName)
{
int wordCount = 0;
string word;
ifstream inFile;
inFile.open(fileName.c_str());
inFile >> word;
while (inFile)
{
wordCount = wordCount + 1;
inFile >> word;
}
return wordCount;
}
int countC(string fileName)
{
int charCount = 0;
char character;
ifstream inFile;
inFile.open(fileName.c_str());
inFile >> character;
while (inFile)
{
charCount = charCount + 1;
inFile >> character;
}
return charCount;
}
int sumInt(string fileName)
{
int intSum = 0, integer;
ifstream inFile;
inFile.open(fileName.c_str());
inFile >> integer;
while (inFile)
{
intSum = intSum + integer;
inFile >> integer;
}
return intSum;
}
double avgInt(string fileName)
{
double intAvg;
int integer, intSum = 0, number = 0;
ifstream inFile;
inFile.open(fileName.c_str());
inFile >> integer;
while (inFile)
{
intSum = intSum + integer;
number = number + 1;
inFile >> integer;
}
intAvg = intSum / number;
return intAvg;
}
int maxInt(string fileName)
{
int intMax=0, num;
ifstream inFile;
inFile.open(fileName.c_str());
inFile >> num;
while (inFile)
{
if (num > intMax)
intMax = num;
inFile >> num;
}
return intMax;
}
int minInt(string fileName)
{
int intMin, num;
ifstream inFile;
inFile.open(fileName.c_str());
inFile >> num;
intMin = num;
while (inFile)
{
if (num <= intMin)
intMin = num;
inFile >> num;
}
return intMin;
}
string wordL(string fileName)
{
string lWord, word;
ifstream inFile;
inFile.open(fileName.c_str());
while (inFile)
{
}
return lWord;
}
---------- FOLLOW-UP ----------
QUESTION: I need to create a function that finds the longest word in a file and returns that word and the number of characters in the word to the function call. I think there is a predefined function for it, but I can't find it in my text book or the internet.
It should look something like word.longest()
word represents a string variable.
AnswerChris, if you consider a word to be characters delimited by spaces, then this function will do what you want.
You almost had it.
string wordL(string fileName)
{
string lWord, word;
ifstream inFile;
inFile.open(fileName.c_str());
while (inFile)
{
inFile >> word;
if (word.length() > lWord.length())
{
lWord = word;
}
}
return lWord;
}
int main(void)
{
string longestWord = wordL("somefile.txt");
cout << "The longest word is '" << longestWord << "' with length " << longestWord.length() << endl;
}