Thursday, July 30, 2009

How to create a while loop that adds numbers input by a user until you stop input in C++?

I'm in a C++ class and part of my homework asks me to create a simple while loop to allow a user to keep inputting numbers until typing exit and then to add the sum of those numbers inputed. How do I get the while loop to allow the user to keep adding numbers without having to define a new integer each time?


something like ?


int d;


string str (" ");


cout %26lt;%26lt; "input a number" %26lt;%26lt; endl;


cin %26gt;%26gt; d;





while ( some statement )


{


sum of d inputted


}


cout %26lt;%26lt; sum of d %26lt;%26lt; endl;


if ( str == exit )


{


exit program


}


return 0;


i'm lost on this one help me out.

How to create a while loop that adds numbers input by a user until you stop input in C++?
Several points:





1. The accumulating variable has to te initialized to zero.


2. In general, the input of each value should be inside the while loop.


3. Assuming part of the assignment is "only one variable", you can avoid another variable for the input values by evaluating the input in the same assignment statement you use to increment d.


4. Where is "str" assigned a value? Hint: It's also input, but you need a way to tell the difference between a number and the string "exit".


5. The test on "str" also has to be in the loop, but not where you think.





Hope that helps.
Reply:int sum=0;


char* userValue = "";





do


{


cout %26lt;%26lt; "input a number or type exit" %26lt;%26lt; endl;


cin %26gt;%26gt; userValue;


if(userValue!="exit")


{


sum+=atoi(userValue);


}


while(userValue!="exit");





cout %26lt;%26lt; "Sum: "%26lt;%26lt; sum %26lt;%26lt; endl;
Reply:int sum; // stores sum of numbers entered


int entered; // stores number entered





// loops until user enters -1


do{





cout %26lt;%26lt; "Enter a number: ";


cin %26gt;%26gt; entered;





if(entered != -1){


sum += entered;


} // end if





} while(entered != -1); // end do











I would have stored what they entered as a String to do your exit thing, but I don't know how to do String to integer conversions. i'll leave that to you if it is necessary.
Reply:You should have another integer that will represent your total sum. Your algorithm should look something like:





int sum = 0;


boolean notdone = true;





while(notdone)


{


input your d please!


if(if their input is exit)


{


notdone == false; %26lt;-- because its now done!


}


else


{


sum = sum + input;


}


}





This should add the inputs together until they type exit.


When notdone is turned into false, the while loop will break. Stopping the program from asking any more inputs. (You have to finish it and do whatever with the sum, I only gave you a snippet )





:)
Reply:I guess you've basic skills.


See your question is to get input until exit was typed and then add all the number.For this to implement as such you need an array which will make your program little more complex.What you can do is you can add the number when ever he/she enters an integer and when he type exit just exit from loop after printing the sum:


So you need to put comparison inside the while loop.


So if should be the first like in while loop.





So it should be





int sum=0,num=0;


string str (" ");


while (cin%26gt;%26gt;str) // scan a string from input


{


if (str.compare("exit") ==0) // compare input string with "exit"


{ //0 means both are equal.So exit


cout%26lt;%26lt;sum //after printing sum.


return 0;


}


else if((num=atoi(str))!=0) // atoi() return integer val of string


{ //Return 0 if str is not an integer


sum=sum+num;


}


else


{


continue; // not required though. Go back to start of the


} // loop and get next string.


}





Note:atoi() return some other value other than integer if it is out of range.So be cautious.

covent garden

No comments:

Post a Comment