Sign in to follow this  

Struggling with odd problem....

This topic is 3598 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

First off I apologize for posting this as I just read that your not to post homework assignments but this one is already submitted for grading (mistake and all) but I would like to learn how to fix this for the next program I write. I have researched this in my books and online but I have yet to find an answer. As for asking the professor I already did but she probably will not see my question until mid week. I have a section of code that looks like this:
case 'A':
{
	do
	{
	cout<<"\nPlease enter the amount of money you would like to deposit in increments of 5.\n";
	cout<<"Amount: ";
	cin >>depositAmount;

	fiveDenominationTest = depositAmount % 5;

	if(fiveDenominationTest == 0)
	{
		if(depositAmount>100)
		{
		     cout<<"\nNice try with the fake bills, I know your not THAT rich!\n";
		}
		else if (depositAmount <= 100)
		{
		     playerMoney = playerMoney + depositAmount;
		     cout<<"\nYou have deposited $"<<depositAmount<<".";
		     cout<<"\nYour new total is: $"<<playerMoney<<".";
		     loopControl2 = 1;
		}
		else
		{
		     cout<<"\nThat is not a valid selection.\n";
		}
					
	}

	else
	{
	     cout<<"\nThat is not an increment of 5!\n";
	}
	}while(loopControl2 != 1);
			   break;
}


See the section where it tests for the "fiveDenominationTest" to be 0? well if I enter a number, the program is ok. If I enter a letter, the program goes into an infinite loop. Why wouldnt a letter fall under the "else" part of the program just like the non divisible by 5 numbers do? Is there a trick to this? Any help would be greatly appreciated. [Edited by - Chrono1081 on February 10, 2008 3:27:21 PM]

Share this post


Link to post
Share on other sites
As I mentioned in another thread recently, I recommend converting hard tabs to soft tabs before posting code to the forums. Even then, make sure that everything is indented and formatted correctly before submitting the post.

Share this post


Link to post
Share on other sites
Quote:
Original post by Chrono1081
I dont understand what you mean by converting hard tabs to soft tabs. Im assuming instead of pressing the tab button I do a /t or something. Is that what you mean?
Just use spaces (that is, if you use four-space tabs, replace all tabs with four spaces).

Most text editors provide a means to do this in a quick and convenient manner.

Share this post


Link to post
Share on other sites
Here's a program that duplicates the problem and provides a solution.


#include <iostream>
#include <string>
#include <limits>
using namespace std;

int main( int argc, char* argv[] ) {
int number;

bool done = false;
while(!done) {
cout << "Enter a number: ";
cin >> number;

if( cin.fail() ) {
cout << "Invalid input" << endl;

cin.clear();
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
continue;
}

if( !(number % 5) ) {
cout << "Number is a multiple of five." << endl;
} else {
cout << "Number is not a multiple of five." << endl;
}
}

return 0;
}




The problem is, if you read improperly formatted data (such as letters for an integer variable), the data stays in the stream. You have to check for the failbit to see if all the reads went correctly. In this case, check for fail, give an error message, clear the error bits and then "burn" a line with the ignore method.

Edit: It doesn't catch it the "else" clause because if it's not properly formatted, nothing is written to the variable at all. It will remain what it was before you used cin.

Share this post


Link to post
Share on other sites
Quote:
Original post by Chrono1081
I dont understand what you mean by converting hard tabs to soft tabs. Im assuming instead of pressing the tab button I do a /t or something. Is that what you mean?


If you're using visual studio, select all of your code and go to Edit -> Advanced -> Untabify. Also, in Tools -> Options -> Text Editor -> C/C++ -> Tabs, you can configure if you want to use tab characters or spaces, and how many space to use.

Share this post


Link to post
Share on other sites

This topic is 3598 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this