Tuesday, July 14, 2009

C++ programming help!?!?

Anyways I can't get the 7th option for my menu to work.


Since, i don't know any other way to do this, i'm gonna post the program in parts....





#include "stdafx.h"


#include %26lt;iostream.h%26gt;


#include %26lt;iomanip.h%26gt;





int bank_menu(void);





const int SIZE=40;





struct


{


int cust_num;


int ssn;


char cust_name[SIZE];


float balance;


int act_type; //1=Checking; 2=Savings


}bank[10];





int main(int argc, char* argv[])





{


int choice;


int cust_index = -1;


do


{


choice = bank_menu();


if (choice == 1)


{


cout%26lt;%26lt;"You have chosen Load Customer into Structure.\n";


if (cust_index == 9)


cout%26lt;%26lt;"The structure is full.\n";


else


{


cust_index++;


bank[cust_index].cust_num = cust_index + 1;





cout%26lt;%26lt;"Enter Social Security Number: ";


cin%26gt;%26gt;bank[cust_index].ssn;


while (bank[cust_index].ssn %26lt; 100000000 || bank[cust_index].ssn %26gt; 999999999)

C++ programming help!?!?
EDIT: I added more at your SHORTENED VERSION link:


http://answers.yahoo.com/question/index?...





----------


I think the problem might be here in the transfer section:





cin%26gt;%26gt;transfer;


int c_index;


int s_index;


int i;


for (i=0; i%26lt;cust_index+1; i++)


{


if (bank[i].ssn == SSN)


{


if (bank[i].act_type == 1)


{


i=c_index;


}


else


{


i=s_index;


....


...





You have a loop for (i=0; i%26lt;cust_index+1; i++)


but you _might_ have not yet assigned a value to cust_index at the time you call the transfer section. Therefore, it's filled with whatever is in memory -- which may be a very, very large number -- and the loop travels on and on, incrementing 'i', until index i is completely off the array bank[] in:





if (bank[i].ssn == SSN)


...





...and the prog hangs/crashes ugly.





That's our 1st and main suspect. You can verify cust_index when calling the transfer section to be sure:





cout %26lt;%26lt; "cust_index is " %26lt;%26lt; cust_index;





...and thus ensure it's a "safe/small' value (and not negative!) before proceeding. Of course, this is just for debugging. You want to remove such stuff when you go final.








---- ----- ----- ----- -----





Loops are fequently the cause of program hang during run time, and it's hard to know if your code has hit some kind of lengthy or even infinite loop without visual indicators.





Optionally, when I'm debugging a tough problem and I need to inspect a loop variable, I might do something like this:





int iii= 20000;





while(iii--)


{


cout %26lt;%26lt; "\r iii is: " %26lt;%26lt; iii ;


///...other code here


}





This code will allow you to inspect the incrementor/decrementor "iii" as it clicks off without cluttering the screen full of numbers. The little "\r" keeps the cursor pinned to the start of the line.





Not only does this give you a strong visual clue, but the cout action allows you to CTR C keybreak out out of the loop easily if you can see the program is stuck in the loop. Much more amiable than calling taskmanager to kill a wayward execution, for example.





You can inspect all of the variables at run time in methods such as this til you pin down the culprit.


---





Use a standard comment by every piece of debug code so you can easily remove it all once fixed:





while(iii--)


{


cout %26lt;%26lt; "\r iii is: " %26lt;%26lt; iii ; //DEBUG ONLY





Then just do a find on string "DEBUG ONLY" for fast clean up.
Reply:You got to be kidding.


Sorry to be mean, but nobody here is going to debug your entire program - which is what you're asking.





Narrow down the problem and ask more specific question.
Reply:Set a break point at the beginning of the 7th choice and single step throught the code, watch the values of the variables. Since the program is crashing there, you are probably trying access memory that isn't valid (e.g. index problem) or something similar.


No comments:

Post a Comment