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

After spending some more time with this, the code now works. I would like some help in coding the error statement.
1) If the user enters an age, say below 1 or exceeding 120, the user shd get an error message "Warning, age must be between 1 and 120" hence(!(1<=ageValue&&ageValue<=120)), and then the user must be prompted again to enter a valid age.
2) If the user enters a non-int value, the message is "Error: invalid entry" and prgm exits the loop. Where would be best to input these two messages? THANKS!

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

#include
using namespace std;
const int MAX=1000;


int main()
{

int ageValue[MAX];
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==

2006-07-23 14:49:17 · 3 answers · asked by jdegbor 1 in Computers & Internet Programming & Design

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:"< while(count>0)
{
count--;

cout<<" "< }

return 0;

}

2006-07-23 14:50:29 · update #1

3 answers

I had to make some more changes to your code. I placed comments around the changes that I made.

#include
using namespace std;
const int MAX=1000;
/* moved to here so all functions can use them */
int ageValue[MAX];
int count=0;


/* new function */
void inputAge()
{
cout<<"Enter the age of the youngest family member:"< cin>>ageValue[count];

/* Error 2) */
if (!cin)
{
/* not exactly sure what you want done here. */
/* to exit program */
cout<<"Your other message!"
exit(1);

/* to only stop input, and then print the other values out
change the return value to a bool and return false */
/* return false; */
}
/* Error 1) */
else if (ageValue[count]<1||ageValue[count]>120)
{
cout<<"Your error message!"< /* recall this method */
inputAge();
}
}


int main()
{

char choice;

/* call to new function */
inputAge();

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


/* Placed in the below while instad of the for.*/
while (choice=='Y'||choice=='y')
/*for(count=1; choice=='Y'||choice=='y'; count++)*/
{
/* Added the count increment here */
count++;

/* call to new function */
inputAge();

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:"< /* Change the count>0 to count>=0 for it to print out the first entry as well */
while(count>=0)
{
cout<<" "<
/* Place the count decrement here */
count--;
}

return 0;

}

2006-07-24 08:45:18 · answer #1 · answered by Mark aka jack573 7 · 0 0

I have not had a chance to compile this, so I am not sure it is error-free. But I think that it should work fine.


#include
using namespace std;
const int MAX=1000;
int IsInRange(float); //returns 1 if argument is in range.


int main()
{

int ageValue[MAX];
int count=0;
char choice;
float temp;

cout<<"Enter the age of the youngest family member:"< cin>>temp;

while(!(IsInRange(temp))
{
cout<<"Age must be whole number between 1 and 120!"< cin>>temp;
}
ageValue[count] = (int)temp;

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>>temp;

while(!(IsInRange(temp))
{
cout<<"Age must be between 1 and 120!"< cin>>temp;
}
ageValue[count] = (int)temp;

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:"< while(count>0)
{
count--;

cout<<" "< }

return 0;

}



int IsInRange(float);
{

if(1=120) return (0);
for(int i = 1;i<=119;i++)
{
if(i return(0);
else return(1);
}
}

2006-07-23 23:34:14 · answer #2 · answered by viv_1612 1 · 0 0

Looking at your account, it is looks like you don't go back to your questions to award points, so why should we give you the answer?

2006-07-23 23:33:26 · answer #3 · answered by DadOnline 6 · 0 0

fedest.com, questions and answers