Program Which: Takes numbers from a user, and sum's them.
-
Hello all, I am working on my homework for my C++ programming class and I am hitting a brick wall. Everywhere I look online does not seem to have any information in which I can apply to my program. I am supposed to use a "while" loop which I am, and thats working. The part which is not working is the users input should add it to the previous entry, and after the user types "0" it will display the sum on the screen. I have looked at the following references to try and make sense of things: http://www.cplusplus.com/reference/std/numeric/partial_sum/[^] http://www.cplusplus.com/reference/std/numeric/accumulate/[^] http://frank.mtsu.edu/~csci117/manual/lab7/lab7.html[^] I am trying very hard to understand this, but I can not find a good online reference of the libraries. www.cplusplus.com is very good but dosent have the info I need, or more than likely I can not find it. But here is the code to which I am trying to correct:
#include <iostream>
#include <string>using namespace std;
int main()
{// Global Variables
int userinput;
int sum = 0;
sum = sum + userinput;// Start While loop
while (userinput != 0)
{
// Prompt User for input
cout << "Please enter a number. When you are finished, type 0 and press Enter." ;
cin >> ++userinput ;}
cout << "The sum is:" << sum << endl;
return 0;
}Thank's to all in advance for pointing me in the correct direction so I can figure this out. I should have put this at the top: I DO NOT WANT THE ANSWER, I WOULD PREFER THE REFERENCE TO FIND MY OWN.
-
Hello all, I am working on my homework for my C++ programming class and I am hitting a brick wall. Everywhere I look online does not seem to have any information in which I can apply to my program. I am supposed to use a "while" loop which I am, and thats working. The part which is not working is the users input should add it to the previous entry, and after the user types "0" it will display the sum on the screen. I have looked at the following references to try and make sense of things: http://www.cplusplus.com/reference/std/numeric/partial_sum/[^] http://www.cplusplus.com/reference/std/numeric/accumulate/[^] http://frank.mtsu.edu/~csci117/manual/lab7/lab7.html[^] I am trying very hard to understand this, but I can not find a good online reference of the libraries. www.cplusplus.com is very good but dosent have the info I need, or more than likely I can not find it. But here is the code to which I am trying to correct:
#include <iostream>
#include <string>using namespace std;
int main()
{// Global Variables
int userinput;
int sum = 0;
sum = sum + userinput;// Start While loop
while (userinput != 0)
{
// Prompt User for input
cout << "Please enter a number. When you are finished, type 0 and press Enter." ;
cin >> ++userinput ;}
cout << "The sum is:" << sum << endl;
return 0;
}Thank's to all in advance for pointing me in the correct direction so I can figure this out. I should have put this at the top: I DO NOT WANT THE ANSWER, I WOULD PREFER THE REFERENCE TO FIND MY OWN.
Outside the loop you should initialize the variable "userinput". The statement
sum = sum + userinput
before the loop should be removed. This statement should appear somewhere inside the loop. Also, for this you can use the += operator instead. The
++userinput
inside the loop should just beuserinput
without the ++ operator. The variables "userinput" and "sum" are not "Global Variables". They are local to the function "main". You need to work out the details. -
Outside the loop you should initialize the variable "userinput". The statement
sum = sum + userinput
before the loop should be removed. This statement should appear somewhere inside the loop. Also, for this you can use the += operator instead. The
++userinput
inside the loop should just beuserinput
without the ++ operator. The variables "userinput" and "sum" are not "Global Variables". They are local to the function "main". You need to work out the details.Thank you very much. I got it working now. I read over this while I was at dinner with the wife and figured it out at the dinner table! here is the working code:
#include
using namespace std;
int main()
{// Local Variables
int userinput;
int sum = 0;// Start While loop
while (userinput != 0)
{
// Prompt User for input
cout << "Please enter a number. When you are finished, type 0 and press Enter." ;
cin >> userinput ;
sum = sum + userinput;}
cout << "The sum is:" << sum << endl;
return 0;
} -
Hello all, I am working on my homework for my C++ programming class and I am hitting a brick wall. Everywhere I look online does not seem to have any information in which I can apply to my program. I am supposed to use a "while" loop which I am, and thats working. The part which is not working is the users input should add it to the previous entry, and after the user types "0" it will display the sum on the screen. I have looked at the following references to try and make sense of things: http://www.cplusplus.com/reference/std/numeric/partial_sum/[^] http://www.cplusplus.com/reference/std/numeric/accumulate/[^] http://frank.mtsu.edu/~csci117/manual/lab7/lab7.html[^] I am trying very hard to understand this, but I can not find a good online reference of the libraries. www.cplusplus.com is very good but dosent have the info I need, or more than likely I can not find it. But here is the code to which I am trying to correct:
#include <iostream>
#include <string>using namespace std;
int main()
{// Global Variables
int userinput;
int sum = 0;
sum = sum + userinput;// Start While loop
while (userinput != 0)
{
// Prompt User for input
cout << "Please enter a number. When you are finished, type 0 and press Enter." ;
cin >> ++userinput ;}
cout << "The sum is:" << sum << endl;
return 0;
}Thank's to all in advance for pointing me in the correct direction so I can figure this out. I should have put this at the top: I DO NOT WANT THE ANSWER, I WOULD PREFER THE REFERENCE TO FIND MY OWN.
Now that the program adds the numbers and provides the sum at the end, I need to provide a means of displaying how many times a user input something. The example output would be: Sum = 134 Total numbers of inputs = 8 So I have been looking to find something on counters. I have looked at multiple web sites yet again and can not find anything. I have fixed my previous code and it is listed below. Once again, I DO NOT WANT THE ANSWER, I WANT THE REFERENCE SO I CAN LOOK IT UP MYSELF. Being pointed in the right direction helps me most. Thank you all again.
#include <iostream>
using namespace std;
int main()
{// Local Variables
int userinput;
int sum = 0;
int count = 0 ;// Welcome Message
cout << "Roberts Number program v1" << endl;
cout << endl;
cout << endl;
cout << "When you are finished, enter '0'" << endl;
cout << endl;// Start While loop
while (userinput != 0)
{
// Prompt User for input
cout << "Input a number, then press enter. " ;
cin >> userinput ;
sum += userinput;}
cout << "The sum is:" << sum << endl;
return 0;
} -
Now that the program adds the numbers and provides the sum at the end, I need to provide a means of displaying how many times a user input something. The example output would be: Sum = 134 Total numbers of inputs = 8 So I have been looking to find something on counters. I have looked at multiple web sites yet again and can not find anything. I have fixed my previous code and it is listed below. Once again, I DO NOT WANT THE ANSWER, I WANT THE REFERENCE SO I CAN LOOK IT UP MYSELF. Being pointed in the right direction helps me most. Thank you all again.
#include <iostream>
using namespace std;
int main()
{// Local Variables
int userinput;
int sum = 0;
int count = 0 ;// Welcome Message
cout << "Roberts Number program v1" << endl;
cout << endl;
cout << endl;
cout << "When you are finished, enter '0'" << endl;
cout << endl;// Start While loop
while (userinput != 0)
{
// Prompt User for input
cout << "Input a number, then press enter. " ;
cin >> userinput ;
sum += userinput;}
cout << "The sum is:" << sum << endl;
return 0;
}rbwest86 wrote:
counters
A counter is just a variable that holds a number. So per your requirement I will reword it to provide the hint that you need... Each time that the user enters a number, increase the counter, then when the user is done entering numbers, output the number of times that the user entered a value.
Why is common sense not common? Never argue with an idiot. They will drag you down to their level where they are an expert. Sometimes it takes a lot of work to be lazy Individuality is fine, as long as we do it together - F. Burns Help humanity, join the CodeProject grid computing team here
-
Thank you very much. I got it working now. I read over this while I was at dinner with the wife and figured it out at the dinner table! here is the working code:
#include
using namespace std;
int main()
{// Local Variables
int userinput;
int sum = 0;// Start While loop
while (userinput != 0)
{
// Prompt User for input
cout << "Please enter a number. When you are finished, type 0 and press Enter." ;
cin >> userinput ;
sum = sum + userinput;}
cout << "The sum is:" << sum << endl;
return 0;
}Even though this program works, you are relying on the variable
userinput
having an initial non-zero value. The compiler actually does not always guarantee that. To make the program robust, I suggest you take control on the values of variables and not rely on some random factors. Good luck and happy programming!