Click to See Complete Forum and Search --> : Weird error using cin (and fstream variables) in C++


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).]

The_Stack
11-13-2000, 10:56 AM
Is this a compiler error? If so what is the compiler error?

Is this a linker error? If so what is the linker error?

Is this a run-time error? If so what is the error behavior?

http://www.linuxnewbie.org/ubb/smile.gif

Strike
11-13-2000, 11:03 AM
Hmm... honestly, I'm not really willing to look through that much code right now for the error - can you narrow it down a little bit at least? Like to a main subroutine or something that is causing the problem or something.

Lucefiel
11-13-2000, 04:00 PM
Sorry, it's a compiler problem in the "other" functions. It doesn't know what the >> and << binary operators are for cin (in all the functions except main) and outData (an ofstream variable used in the ConvertDataType function). The weird thing about it is that.

1. It doesn't complain about the << operator (for cout in the InputData function), and
2. It doesn't complain about inData (the ifstream variable used in ConvertDataType).

I don't get it... I've never had this problem before.


The problem areas have arrows by them (wouldn't let me bold it) and the actual error messages are at the bottom of this post.


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



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


Error Messages:
:\Programming\C++\Class Help\Projects\Project 2 Rewrite\Project2.cpp(55) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char
> >' (or there is no acceptable conversion)

C:\Programming\C++\Class Help\Projects\Project 2 Rewrite\Project2.cpp(75) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char
> >' (or there is no acceptable conversion)

C:\Programming\C++\Class Help\Projects\Project 2 Rewrite\Project2.cpp(88) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char
> >' (or there is no acceptable conversion)

------------------
May the best of your past be the worst of your future.


[This message has been edited by Lucefiel (edited 13 November 2000).]

BrianDrozd
11-13-2000, 04:27 PM
Originally posted by Lucefiel:
Sorry, it's a compiler problem in the "other" functions. It doesn't know what the >> and << binary operators are for cin (in all the functions except main) and outData (an ofstream variable used in the ConvertDataType function). The weird thing about it is that.

1. It doesn't complain about the << operator (for cout in the InputData function), and
2. It doesn't complain about inData (the ifstream variable used in ConvertDataType).

I don't get it... I've never had this problem before.


The problem areas have arrows by them (wouldn't let me bold it) and the actual error messages are at the bottom of this post.


Okay, this is just a random little shot in the dark, but from there error messages, I'm guessing the problem is that there is no iostream: :operator<<(iostream io, string s) and iostream: :operator>>(iostream io, string s) functions defined. My best guess toward correcting these errors is to use c_str() or data() functions to print out the data, and use a a char * buffer to read in the data.

[This message has been edited by BrianDrozd (edited 13 November 2000).]

Gweedo
11-14-2000, 12:10 AM
Here is code 2 workin:


#include <iostream>
#include <cmath>
using namespace std;

int ChooseNum();
void DoTheMath(int);

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


I used the following command to compile it:
g++ -Wall -g file.cpp -o file

You can compare the two files to see the changes that were made.

Code Segment 1:

#include <cmath>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int ConvertDataType(string);
void CalculateTotal(int);
void InputNum(bool , int , string );

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+=increment;
increment++;
}

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

This is also running, used same compile command. There is a loop issue that you will have to work on though. http://www.linuxnewbie.org/ubb/wink.gif

I did not point out mistakes because it will good for ya to compare your code with this to see where it went wrong.

[This message has been edited by Gweedo (edited 14 November 2000).]

Lucefiel
11-14-2000, 11:10 AM
#include <cmath>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int ConvertDataType(string);
void CalculateTotal(int);
void InputNum(bool , int , string );

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+=increment;
increment++;
}

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

[/b]<HR></BLOCKQUOTE>

I'm a moron!!!
Thanks Gweedo!

P.S.
Sorry, not used to using strings, when I took the class, we used character arrays. It made some things harder, but you didn't have to mess with that darn .h file http://www.linuxnewbie.org/ubb/smile.gif

EDIT: Oh, BTW, I'm not compiling in linux. I'm using Visual Studio http://www.linuxnewbie.org/ubb/redface.gif

------------------
May the best of your past be the worst of your future.

[This message has been edited by Lucefiel (edited 14 November 2000).]