Struggling with odd problem....

Started by
5 comments, last by Chrono1081 16 years, 2 months ago
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]
Advertisement
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.
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?
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.
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.
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.
Wow thank you :) Ill have to explore how all that works in the morning since right now its bed time :D

This topic is closed to new replies.

Advertisement