Lucefiel
11-13-2000, 03:16 AM
EDIT:Sorry, but it looks like the code lost all the "spacing" lines. It may be a little hard to read.
OK, I'm working on a program for C++ class (I need to know how to show others what the heck to do). The program is supposed to ask the user for an integer that's greater than one and add the squares of all the numbers between 1 and that number (inclusive) and print out the sum on the screen.
Unfortunately, the teacher wants some pretty advanced error checking. he wants the program to NOT fail is the user enters a float or character value and he wants the program to ask the user for a new number if they enter 0 or 1. The program should run until a negative number is entered.
My problem is that I can't get cin (or inData and outData) to work outside of the main function and I don't know why.
The first program sample is one that does all the errorchecking (it's obviously not finished, so it doesn't actually check for non int numbers or chars) and it's the one that won't compile.
The second program is one that works, except it doesn't check for float numbers and just "fails gracefully" if someone enters a char value (I've included this one so you can see what the program's supposed to do).
Anybody know what the problem is in the first sample?
#include <cmath>
#include <fstream>
#include <iostream>
using namespace std;
int ConvertDataType(string numAsString);
void CalculateTotal(int numAsInt);
void InputNum(bool done, int numAsInt, string numAsString);
ifstream inData;
ofstream outData;
int main()
{
bool done = false;
int numAsInt = 0;
int sum = 0;
string numAsString = " ";
while (numAsInt >= 0 && !done)
{
InputNum(done, numAsInt, numAsString);
if (numAsInt >= 2)
CalculateTotal(numAsInt);
else if (numAsInt == 0 && done)
{
cout << "What the heck??? How did you get to this??? I think you're a cheater!!" << endl
<< "Well, I guess the only thing left to do is end the program..." << endl << endl
<< "Bye!... Cheater!!" << endl;
}
else if (done)
{
cout << endl << "Thanks for playing!" << endl;
}
}
return 0;
}
void CalculateTotal(int numAsInt)
{
int increment = 1;
int sum = 0;
while (increment <= numAsInt)
sum = sum + pow(increment++, 2);
cout << "The sum of all the integers between 1 and " << numAsInt << " is: " << sum << endl << endl;
}
int ConvertDataType(string numAsString)
{
int numAsInt = 0;
outData.open("input.dat");
outData << numAsString;
outData.close();
inData.open("input.dat");
inData >> numAsInt;
inData.close();
return numAsInt;
}
void InputData(bool done, int numAsInt, string numAsString)
{
bool badNum = false;
while (numAsInt == 0 | | numAsInt == 1)
{
if (!badNum)
{
cout << "Please enter an integer greater than 1." << endl
<< "A negative number will end the program: ";
cin >> numAsString;
numAsInt = ConvertDataType(numAsString);
if (numAsInt == 0 | | numAsInt == 1)
{
badNum = true;
}
}
if (badNum)
{
while (numAsInt == 0 | | numAsInt == 1)
{
cout << "\"" << numAsInt << "\" is not a valid number, please re-enter: " << endl;
cin >> numAsString;
numAsInt = ConvertDataType(numAsString);
if (numAsInt > 1 | | numAsInt < 0)
badNum = false;
}
}
}
}
Code segment 2.
#include <iostream>
#include <cmath>
using namespace std;
int ChooseNum();
void DoTheMath(int someNum);
int main()
{
int someNum = 0;
while (someNum >= 0)
{
someNum = ChooseNum();
if (someNum > 1)
DoTheMath(someNum);
}
cout << endl << "Thanks for playing!" << endl;
return 0;
}
int ChooseNum()
{
bool wrongAnswer = false;
int someNum = 0;
while (someNum == 0 | | someNum == 1)
{
if (!cin)
{
cout << "Sorry, but you seem to have entered an invalid data type." << endl
<< "Please restart the program." << endl;
return -1;
}
if (!wrongAnswer)
{
cout << "Please enter an integer > 1." << endl
<< "A negative integer will end the program: ";
cin >> someNum;
if (someNum == 0 | | someNum == 1)
wrongAnswer = true;
}
else if (someNum == 0 | | someNum == 1 && wrongAnswer)
{
cout << "\"" << someNum << "\" is not a valid answer. Please try again: ";
cin >> someNum;
if (someNum < 0 | | someNum > 1)
wrongAnswer = false;
}
}
return someNum;
}
void DoTheMath(int someNum)
{
double sum = 0;
while (someNum >= 1)
sum = sum + pow(someNum--,2);
cout << "The sum is: " << sum << "." << endl << endl;
}
------------------
May the best of your past be the worst of your future.
[This message has been edited by Lucefiel (edited 13 November 2000).]
OK, I'm working on a program for C++ class (I need to know how to show others what the heck to do). The program is supposed to ask the user for an integer that's greater than one and add the squares of all the numbers between 1 and that number (inclusive) and print out the sum on the screen.
Unfortunately, the teacher wants some pretty advanced error checking. he wants the program to NOT fail is the user enters a float or character value and he wants the program to ask the user for a new number if they enter 0 or 1. The program should run until a negative number is entered.
My problem is that I can't get cin (or inData and outData) to work outside of the main function and I don't know why.
The first program sample is one that does all the errorchecking (it's obviously not finished, so it doesn't actually check for non int numbers or chars) and it's the one that won't compile.
The second program is one that works, except it doesn't check for float numbers and just "fails gracefully" if someone enters a char value (I've included this one so you can see what the program's supposed to do).
Anybody know what the problem is in the first sample?
#include <cmath>
#include <fstream>
#include <iostream>
using namespace std;
int ConvertDataType(string numAsString);
void CalculateTotal(int numAsInt);
void InputNum(bool done, int numAsInt, string numAsString);
ifstream inData;
ofstream outData;
int main()
{
bool done = false;
int numAsInt = 0;
int sum = 0;
string numAsString = " ";
while (numAsInt >= 0 && !done)
{
InputNum(done, numAsInt, numAsString);
if (numAsInt >= 2)
CalculateTotal(numAsInt);
else if (numAsInt == 0 && done)
{
cout << "What the heck??? How did you get to this??? I think you're a cheater!!" << endl
<< "Well, I guess the only thing left to do is end the program..." << endl << endl
<< "Bye!... Cheater!!" << endl;
}
else if (done)
{
cout << endl << "Thanks for playing!" << endl;
}
}
return 0;
}
void CalculateTotal(int numAsInt)
{
int increment = 1;
int sum = 0;
while (increment <= numAsInt)
sum = sum + pow(increment++, 2);
cout << "The sum of all the integers between 1 and " << numAsInt << " is: " << sum << endl << endl;
}
int ConvertDataType(string numAsString)
{
int numAsInt = 0;
outData.open("input.dat");
outData << numAsString;
outData.close();
inData.open("input.dat");
inData >> numAsInt;
inData.close();
return numAsInt;
}
void InputData(bool done, int numAsInt, string numAsString)
{
bool badNum = false;
while (numAsInt == 0 | | numAsInt == 1)
{
if (!badNum)
{
cout << "Please enter an integer greater than 1." << endl
<< "A negative number will end the program: ";
cin >> numAsString;
numAsInt = ConvertDataType(numAsString);
if (numAsInt == 0 | | numAsInt == 1)
{
badNum = true;
}
}
if (badNum)
{
while (numAsInt == 0 | | numAsInt == 1)
{
cout << "\"" << numAsInt << "\" is not a valid number, please re-enter: " << endl;
cin >> numAsString;
numAsInt = ConvertDataType(numAsString);
if (numAsInt > 1 | | numAsInt < 0)
badNum = false;
}
}
}
}
Code segment 2.
#include <iostream>
#include <cmath>
using namespace std;
int ChooseNum();
void DoTheMath(int someNum);
int main()
{
int someNum = 0;
while (someNum >= 0)
{
someNum = ChooseNum();
if (someNum > 1)
DoTheMath(someNum);
}
cout << endl << "Thanks for playing!" << endl;
return 0;
}
int ChooseNum()
{
bool wrongAnswer = false;
int someNum = 0;
while (someNum == 0 | | someNum == 1)
{
if (!cin)
{
cout << "Sorry, but you seem to have entered an invalid data type." << endl
<< "Please restart the program." << endl;
return -1;
}
if (!wrongAnswer)
{
cout << "Please enter an integer > 1." << endl
<< "A negative integer will end the program: ";
cin >> someNum;
if (someNum == 0 | | someNum == 1)
wrongAnswer = true;
}
else if (someNum == 0 | | someNum == 1 && wrongAnswer)
{
cout << "\"" << someNum << "\" is not a valid answer. Please try again: ";
cin >> someNum;
if (someNum < 0 | | someNum > 1)
wrongAnswer = false;
}
}
return someNum;
}
void DoTheMath(int someNum)
{
double sum = 0;
while (someNum >= 1)
sum = sum + pow(someNum--,2);
cout << "The sum is: " << sum << "." << endl << endl;
}
------------------
May the best of your past be the worst of your future.
[This message has been edited by Lucefiel (edited 13 November 2000).]