Sunday, July 12, 2009

C++ array question?

I madethe changes you guys recommended, but I am getting errors. Any ideas? Thanks!!





//Reading ages from user and printing out values in reverse order


//************************************...


#include%26lt;iostream%26gt;





using namespace std;


int main()


{


int ageValue;


int count;


char choice;


int loopCounter=0;





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


cin%26gt;%26gt;ageValue;


cout%26lt;%26lt;"Are there any more family members(Y for yes N for no)";


cin%26gt;%26gt;choice;


if (choice=='Y'||choice=='y')


do


{


count=count+1;





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





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


}


while(choice!='N'||choice!='n');





for(loopCounter=count;loopCounter=0; loopCounter--)


{


cout%26lt;%26lt;ageValue[count];


count--;


}





return 0;


}

C++ array question?
From what I can see, and others have said;


(1) the count is not initialized, the way you want the program to run it should be initialized as -1. When the count = count + 1 is called, it will go to 0, the first index in the array.


(2) ageValue should also be initilized in case of an incorrect entry from user, then the value will remain. Probably be best for -1, as no-one is this age, if you want to use the variable as the input, or you need to init it to an array, and change the first cin %26gt;%26gt; ageValue to cin %26gt;%26gt; ageValue[count] then you can do (1) and init count as 0.


(3) The problem with using an array, you will limit the program to 150, or 255 people. Probably be best using a vector from the standard library. Hope you know about this.


(4) As you are only using loopCounter for the loop at the bottom, you can declare it in the for loop. for (int loopCounter = count; blah, or call it 'i' instead, like for (int i = count; i %26gt;= 0; i++);


(5) The statement count = count + 1 can be shortened two ways


(5.1) count += 1 (use if adding more than 1)


(5.2) count++ (preferred for adding 1)


(6) The loop in (4) has to be changed from loopCounter = 0 to loopCounter %26gt;= 0; The reason is, the statement needs to return true for the loop to continue, and if the count is greater than 1 it will not be equal to 0, and not continue. Therefore, if only one person was input, they will be printed, but any other amount will not. The changed statement will return true until the counter drops below 0 and will print out the desired results.





I think thats about it, I did not test these changes or anything.





[edit]


(7) You could even change the statement while (choice != 'N, blah to while (choice == 'Y, blah so the if statement above is the same, or you could change the if statement to if (choice != 'N', blah.


(8) You have not asked the user if there are any more family members in the do while loop and the while condition will always be true. Just copy what you have from the top, it is bloating the code, but just while you learn, and paste it in the do while loop.
Reply:You have made an error in array .you haven't declared array.


Declare it as


int ageValue[]=new 1nt[50];





initialize count=0 in the beginning itself





then change the first cin statement as


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





then, change the for loop as


for(loopCounter=count;loopCounter%26gt;=1;l...





delete count-- in the loop
Reply:while(choice!='N'||choice!='n'...





yout '=' should be '==' in both places in this line. You also didn't initialize 'count'.





What exact error message do you get? Compile or runtime?
Reply:WTF does that mean?!?!?!?!?
Reply:you should say int ageValue[255];





right now ageValue is not an array but ONE variable.





thats all the errors I can spot and it is a big one so try that and if it doesnt work email the source code to me.





right now, yahoo is putting in the annoying ... things.
Reply:hmmm
Reply:It's been a while since I wrote C++ programs, but it might help if you told us what errors you are getting.





I think I would do it more or less like this, providing my memory of c++ coding is still acurate, at any rate the method should work:





int ageValue[];


int count;


char choice;





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


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


cout %26lt;%26lt; "Are there any more family members";


cin %26gt;%26gt; choice;





for(count = 1; choice == 'Y' || choice == 'y'; count++)


{


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





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





cout %26lt;%26lt; "Are there more family members?";


cin %26gt;%26gt; choice;


}





return 0;


No comments:

Post a Comment