English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

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<=ageValue&&ageValue<=150)an ERROR message shd be displayed and the user reprompted. Appropriate error messages as well as help with coding would be greatly appreciated.

2006-07-23 08:28:01 · 5 answers · asked by jdegbor 1 in Computers & Internet Programming & Design

g1<=ageValue&&ageValue<=150)

2006-07-23 08:28:10 · update #1

// reading values printing out input in reverse
//**********************************************************************

#include
using namespace std;

int main()
{
int ageValue[10];
int count=0;
char choice;

cout<<"Enter the age of the youngest family member:"< cin>>ageValue[count];

cout<<"Are there any more family members?(Y for yes and N for no)"< cin>>choice;

for(count=1; choice=='Y'||choice=='y'; count++)
{
cout<<"Enter the age of the next youngest person"< cin>>ageValue[count];

cout<<"Are there any more family members?(Y for yes and N for no)";
cin>>choice;
}
cout<<"Thank you. The ages of your family in reverse order are:";

for(count=0; count<10; count--)

cout< return 0;
}

2006-07-23 08:28:33 · update #2

The thing is that I only specified 10 because i thought all arrays should have a size. In this instance I thought it would limit the number of re-prompts the user gets to 10 times. My undersatnding of this topic is limited.. Ideally I would like the user to be reprompted for the age of the next youngest(endless loop) till the user hits N. How do you define an array in that case?(no constant value..just a variable) I would be grateful to see your suggestions and the re-coding you think would work. Thanks!

2006-07-23 08:44:33 · update #3

5 answers

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

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 << "You can enter up to "<
//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 < MEMBERS_MAX; i++)
{
//If this is the first question, ask for youngest first
if(i == 0)
{
cout<<"Enter the age of the youngest family member:"< }
//Otherwise, ask for next youngest.
else
{
cout<<"Enter the age of the next youngest family member:"< }
//Query for age value of the next youngest family member
cin>>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<<"Are there any more family members? (Y/N)"< cin>>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 > 10, but
//i keeps going up every iteration (you have i++), so you had an
//infinite loop.
cout<<"Thank you. The ages of your family in reverse order are:"< for(i--; i >= 0; i--)
{
cout< }

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

2006-07-23 09:41:52 · answer #1 · answered by ComputerTechGeek 1 · 0 1

N2FC...good catch on the line:
for(count=0; count<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...

2006-07-23 09:07:05 · answer #2 · answered by gene_frequency 7 · 0 0

"I was wondering, why isn't it *(array[i]) += 500, and cout << *(array[i]) Since they are pointers don't they have to be dereferenced. Why aren't they? " they are. the [] operator dereferences the array array[i] is the same as *(array+i) [ by definition] which also has the strange side effect that array[i] is the same as i[array] Nicholas: array[i] = *(array+i*sizeof(whateverthedatatypeis))... should be: array[i] parses to *(array+i) the compiler takes care of the multiplying by sizeof the array elements -- if you do it again you will get a wrong index into the array

2016-03-27 04:10:30 · answer #3 · answered by Anonymous · 0 0

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<10; count--)

unless it was a typo in your question - how is that suppossed to work???

2006-07-23 08:38:06 · answer #4 · answered by my_oh_my 2 · 0 0

For starters, your last (output) loop starts at 0 and counts DOWN... It'll NEVER end!

2006-07-23 08:34:23 · answer #5 · answered by N2FC 6 · 0 0

fedest.com, questions and answers