GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-Sep-2008, 10:30
silentmichael silentmichael is offline
New Member
 
Join Date: Sep 2008
Location: San Diego
Posts: 1
silentmichael is on a distinguished road

"while loop" not reading 0's?


Hi.
I'm taking a first year C++ class and need some help.

First, this is what the assignment is asking for:
Quote:
Write a C++ program that gives the new ID for a 4-digit integer entered from the keyboard.
The new ID is formed in the following procedure:
The last four digits of a social security number (SSN) will have a new digit for each.

The new digit for the first digit is figured out by timing it by 4;
if the product is greater than 9, add its digits together.
The new value for the second digit is figured out by timing it by 3.
if the product is greater than 9, add its digits together.
The new value for the third digit is figured out by timing it by 2.
if the product is greater than 9, add its digits together.
The new value for the fourth digit is figured out by timing it by 1.
If any of the new digits is 0, change it to 9.

All the four new digits are then added up to have a total, which is
then used to time to the entered 4-digit SSN to give the new encrypted ID.

For example, with the entered four digits 2620, the new ID will be 78600
because 2*4 = 8, 6*3 = 18 = 9, 2*2 = 4, 0*1 = 0 = 9, and 8+9+4+9 = 30, and then 30*2620 = 78600.


The professor gave us some sample numbers for testing our code. They are:
Quote:
last-4 digits of SSN: 9578, new ID: 268184
last-4 digits of SSN: 0149, new ID: 4321
last-4 digits of SSN: 1039, new ID: 29092
last-4 digits of SSN: 0004, new ID: 124
last-4 digits of SSN: 9999, new ID: 359964
last-4 digits of SSN: 0023, new ID: 575

When I test 9578, 1039, and 9999, I get the correct new ID. However when I test the other 3 (which begin with 0), the new ID is incorrect.

Here is the main part of my code:
CPP / C++ / C Code:
int main()
{
	int ssn;
	int remainder;
	int product;
	int i = 1;
	int sum = 0;
	int newid;
	cout << "Enter the last 4 digits of your SSN: ";
	cin >> ssn;
	int temp = ssn;

	while (ssn > 0)
	{
		remainder = ssn % 10;
		ssn = ssn / 10;
		product = remainder * i;
		if (product == 0)
		{
			product = 9;
		}
		else if (product > 9)
		{
			int t1 = product % 10;
			product = product / 10;
			product = product + t1;
		}
		
		sum = sum + product;
		cout << "The remainder #" << i << ": " << remainder << endl;
		i++;
	}
	cout << "Sum: " << sum << endl;
	newid = sum * temp;
	cout << "Your new ID is: " << newid << endl;
	
	cout << endl << "Press <enter> to exit the program ..." << endl;
	fflush(stdin);
	cin.get();
	return 0;
}

Does anyone have any tips on how to fix it?

Thanks.
  #2  
Old 18-Sep-2008, 13:57
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: "while loop" not reading 0's?


Quote:
Originally Posted by silentmichael
When I test 9578, 1039, and 9999, I get the correct new ID. However when I test the other 3 (which begin with 0), the new ID is incorrect.
Correct.

Look at the basis for your loop:
CPP / C++ / C Code:
while (ssn > 0)
For values such as "0149", the loop is executed only three times. You want it to unconditionally execute four times.
  • The simplest way to correct this problem is to add another variable which will count how many times the loop has been executed. If the leading digit is a zero, the count will be 3. Once out of the loop, check to see if the count is 4. If not, then deal with the leading digit.

    Note that this approach is simply a hack stacked on top of an errant structure.
  • A better approach would be to loop through your logic four times as expected. You may want to look at using a for-loop instead of a while-loop. This may require some reworking of your internal logic.
  • A better approach yet would be to isolate the digit computations to individual functions which would then return their results. These results would be summed & dealt with in main(). This would leave main() simply as the point where input is received, call four functions, gather their results, apply the final logic, & quit. Note that this may result in a longer program, but I will address this momentarily.
The solution posted shows thought & is clever. However, you have had problems debugging it, so the cleverness has bit you. The four function solution posed above makes each individual function very simple, but "simple" is good because "simple" is easier to debug even if it makes for longer source code. More important, it separates the work which will be easier for anyone else to understand. The code originally posted mixes all the requirements together which makes comprehending & debugging more difficult.

Otherwise, you get points for attempting cleverness. I commend you for it. Unfortunately, cleverness is frequently a two-edged sword.
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
contents of .txt file into 2D array anirudhroxrulz C Programming Language 4 10-Apr-2008 23:45
Reading from file to structure until '\n' Simka C Programming Language 1 21-Dec-2006 16:20
Help with reading data...getting garbage output mnkdrt C++ Forum 2 02-Dec-2005 14:46
Need Help reading HEX data from a sensor Moooey C Programming Language 4 09-Mar-2005 14:46
Reading File, Arrays, Editing File Mary MySQL / PHP Forum 4 15-Jul-2003 03:14

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:06.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.