Sunday, August 2, 2009

C++ Array question.... what is wrong with this code?

This program is supposed to prompt the user to enter the age of the youngest family member and ask the user if there are any more family members. after that it tells the user to enter the age of the next youngest family member and then asks again if there are any more family members. If the answer is yes, the user is prompted to enter the next youngest family members age. this continues till the user hits N for no, at which point the message"Thank you...." is printed , followed by the ages(the user entered) in reverse order. What I am doing wrong? Is it the specifying of the size of the array? I thought all arays types must have a size indicated.





Your help would be much appreciated. I will later add an ERROR message for a non-int value and exit the code and if the input is a valid int but outside of say 150 yrs(1%26lt;=ageValue%26amp;%26amp;ageValue%26lt;=150)an ERROR message shd be displayed and the user reprompted. Appropriate error messages as well as help with coding would be greatly appreciated.

C++ Array question.... what is wrong with this code?
You have a huge design problem here, unfortunately. What happens if the user enters more than 10 values? You'll need some way to keep the user from entering in more values, such as display and error message, or use a std::vector class that's part of the Standard Template Library. (Which should be part of C++). (But let's not worry about that now)





Here is a better implementation, and i also commented it so you can see what's going on (and possibly find out what's wrong with your code:





#include %26lt;iostream%26gt;





using namespace std;





//Define how many members can the user enter.


#define MEMBERS_MAX 10





int main()


{


int ageValue[MEMBERS_MAX];


char choice;





//Notify the user they can only enter up to MEMBERS_MAX family members


cout %26lt;%26lt; "You can enter up to "%26lt;%26lt;MEMBERS_MAX%26lt;%26lt;" family members."%26lt;%26lt;endl;





//This i variable is used to determine how many actual values


//have been entered (so we don't go out of bounds when we read


//the age values back.


int i;


//We wouldn't use an endless loop if we can only ask for MEMBERS_MAX members.


for(i = 0; i %26lt; MEMBERS_MAX; i++)


{


//If this is the first question, ask for youngest first


if(i == 0)


{


cout%26lt;%26lt;"Enter the age of the youngest family member:"%26lt;%26lt;endl;


}


//Otherwise, ask for next youngest.


else


{


cout%26lt;%26lt;"Enter the age of the next youngest family member:"%26lt;%26lt;endl;


}


//Query for age value of the next youngest family member


cin%26gt;%26gt;ageValue[i];





//Ask if they have anymore unless this is the last question


//(We can only accept up to MEMBERS_MAX family members)


if(i != MEMBERS_MAX-1)


{


cout%26lt;%26lt;"Are there any more family members? (Y/N)"%26lt;%26lt;endl;


cin%26gt;%26gt;choice;





if(choice == 'n' || choice == 'N')


{


//If there isn't anymore, stop asking more questins


//by breaking this loop.


break;


}


}


}





//Now, start going backwards (using the variable i)


//What was wrong in your previous code was that you have i %26gt; 10, but


//i keeps going up every iteration (you have i++), so you had an


//infinite loop.


cout%26lt;%26lt;"Thank you. The ages of your family in reverse order are:"%26lt;%26lt;endl;


for(i--; i %26gt;= 0; i--)


{


cout%26lt;%26lt;ageValue[i]%26lt;%26lt;endl;


}





return 0;


}





A better design approach is to ask the user how many family members he/she has, then dynamically allocate an array to that size, so this is not limited to only 10 family members. But I leave that up to you.





Hope this helps
Reply:For starters, your last (output) loop starts at 0 and counts DOWN... It'll NEVER end!
Reply:I am guessing you dont get any output at the end of the programm?





take a moment and reread that line of yours


for(count=0; count%26lt;10; count--)





unless it was a typo in your question - how is that suppossed to work???
Reply:N2FC...good catch on the line:


for(count=0; count%26lt;10; count--)


...I didn't see that 'till you pointed it out, so you get the tip of the hat for that.





But actually, the loop will break at at approx (256 ^ sizeof(int))/2 iterations. That's probably why our trooper didn't complain that the code hung at execution time.





Of course, the array contents won't display, because the counter will never get in range of integers 1 - 10. Should display lots of garbage in memory, tho.





jdegbor


in answer to your question about flexible array size, I'd suggest a linked list. Then you can virtually keep adding 'indexes' indefinitely. It's complex, however....tough stuff for a beginner (link for you below)





If the linked list seems too hard, you could always just create a very large array...one that's so big that 99% of users would never fill it....like


int array[5000];





This will work 99% of the time. But you should know some user, someday, may enter 5001 entries, so it's considered bad practice. Industry mistakes due to assumptions like "5000 will always be enough" have been the bane of programmers for decades.





Good luck...


No comments:

Post a Comment