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%26lt;=ageValue%26amp;%26amp;ageValue%26lt;=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%26lt;iostream%26gt;
using namespace std;
const int MAX=1000;
int main()
{
int ageValue[MAX];
int count=0;
char choice;
cout%26lt;%26lt;"Enter the age of the youngest family member:"%26lt;%26lt;endl;
cin%26gt;%26gt;ageValue[count];
cout%26lt;%26lt;"Are there any more family members?(Y for yes and N for no)"%26lt;%26lt;endl;
cin%26gt;%26gt;choice;
for(count=1; choice=='Y'||choice==
C++ follow up...?
I had to make some more changes to your code. I placed comments around the changes that I made.
#include%26lt;iostream%26gt;
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%26lt;%26lt;"Enter the age of the youngest family member:"%26lt;%26lt;endl;
cin%26gt;%26gt;ageValue[count];
/* Error 2) */
if (!cin)
{
/* not exactly sure what you want done here. */
/* to exit program */
cout%26lt;%26lt;"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]%26lt;1||ageValue[count]%26gt;120)
{
cout%26lt;%26lt;"Your error message!"%26lt;%26lt;endl;
/* recall this method */
inputAge();
}
}
int main()
{
char choice;
/* call to new function */
inputAge();
cout%26lt;%26lt;"Are there any more family members?(Y for yes and N for no)"%26lt;%26lt;endl;
cin%26gt;%26gt;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%26lt;%26lt;"Are there any more family members?(Y for yes and N for no)";
cin%26gt;%26gt;choice;
}
cout%26lt;%26lt;"Thank you. The ages of your family in reverse order are:"%26lt;%26lt;endl;
/* Change the count%26gt;0 to count%26gt;=0 for it to print out the first entry as well */
while(count%26gt;=0)
{
cout%26lt;%26lt;" "%26lt;%26lt;ageValue[count];
/* Place the count decrement here */
count--;
}
return 0;
}
Reply: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%26lt;iostream%26gt;
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%26lt;%26lt;"Enter the age of the youngest family member:"%26lt;%26lt;endl;
cin%26gt;%26gt;temp;
while(!(IsInRange(temp))
{
cout%26lt;%26lt;"Age must be whole number between 1 and 120!"%26lt;%26lt;endl%26lt;%26lt;"Try again: ";
cin%26gt;%26gt;temp;
}
ageValue[count] = (int)temp;
cout%26lt;%26lt;"Are there any more family members?(Y for yes and N for no)"%26lt;%26lt;endl;
cin%26gt;%26gt;choice;
for(count=1; choice=='Y'||choice=='y'; count++)
{
cout%26lt;%26lt;"Enter the age of the next youngest person"%26lt;%26lt;endl;
cin%26gt;%26gt;temp;
while(!(IsInRange(temp))
{
cout%26lt;%26lt;"Age must be between 1 and 120!"%26lt;%26lt;endl%26lt;%26lt;"Try again: ";
cin%26gt;%26gt;temp;
}
ageValue[count] = (int)temp;
cout%26lt;%26lt;"Are there any more family members?(Y for yes and N for no)";
cin%26gt;%26gt;choice;
}
cout%26lt;%26lt;"Thank you. The ages of your family in reverse order are:"%26lt;%26lt;endl;
while(count%26gt;0)
{
count--;
cout%26lt;%26lt;" "%26lt;%26lt;ageValue[count];
}
return 0;
}
int IsInRange(float);
{
if(1%26lt;temp||temp%26gt;=120) return (0);
for(int i = 1;i%26lt;=119;i++)
{
if(i%26lt;temp%26amp;%26amp;temp%26lt;(i+1))
return(0);
else return(1);
}
}
Reply: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?
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment