I am to write a program that will outpout the sum of all of the numbers from 1 to 100. This is what i have but when i excute it the console comes up and disappears immedieatly so i'm not sure what i did wrong. Any help would be appreaciated. Thanks.
using namespace std;
int sum(int base, int limit0);
int main()
{
std::cout << sum(1, 100);
return 0;
}
int sum(int base, int limit)
{
int sum = 0;
for (int i = base; i <= limit; i++){
sum + i;
}
cout << " The sum of all numbers from 1 to 100 is " << endl;
return sum;
The problem is that after you return sum from sum(), no other code after it is executed, including the pause command. So you go back to main() and exit immediately. You'll want to move the pause to before you return from your program in order to make it work.
Also, not that the logic in your for loop is incorrect and will not sum properly. In fact, the sum variable will not be modified at all.
Thanks Zhuge, I a very new beginner to this and do appreaciate the help. I changed the return to after the pause but you where right i'm not getting any output. Guess on to the next step to figure out what in the middle is wrong. Sorry about the code tags but i'm not entirely sure what you mean by that. Again i am a beginner go easy on me. :D
#include <iostream>
#include <cstdlib>
usingnamespace std;
int sum(int base, int limit0);
int main()
{
int a, b;
cout << " Enter smaller number: " << endl;
cin >> a;
cout << " Enter bigger number: " << endl;
cin >> b;
cout << "Sum of all numbers between " << a << " and " << b << " is " << sum (a, b) << endl;
system("pause");return 0;
}
int sum(int base, int limit)
{
int sum = 0;
for (int i = base; i <= limit; i++)
sum += i;
return sum;
}
Edit: To add code tags, which means to make it look like mine, you press the <> button to the right of the new post window. Or you can manually type them in: [code][/code]
#include <cstdlib>
#include <iostream>
usingnamespace std;
int sum(int base, int limit0);
int main()
{
int a, b;
cout << " Enter smaller number: " << endl;
cin >> a;
cout << " Enter bigger number: " << endl;
cin >> b;
cout << "Sum of all numbers between " << a << " and " << b << " is " << sum (a, b) << endl;
system("PAUSE");
return 0;
}
int sum(int base, int limit)
{
int sum = 0;
for (int i = base; i <= limit; i++){
sum += i;
return sum;
} <
I've doubled checked everything and it all looks right now, so not sure except where i notated the < at the end. Which is what comes up as an error before excuting
#include <cstdlib>
#include <iostream>
usingnamespace std;
int sum(int base, int limit);
int main()
{
int a, b;
cout << " Enter smaller number: " << endl;
cin >> a;
cout << " Enter bigger number: " << endl;
cin >> b;
cout << "Sum of all numbers between " << a << " and " << b << " is " << sum (a, b) << endl;
system("PAUSE");
return 0;
}
int sum(int base, int limit)
{
int sum = 0;
for (int i = base; i <= limit; i++){
sum += i;
return sum;
}
The error is on line 29, i underlined it. Everything else seems to check out. Thinks for noticing the 0.
Thank you so much for your help. I truly appreaciate it. Now on to the next one which he always makes the harderst. Everyone in are class is lost on it. :D
Your for loop is not even using count in the equation to calculate their pay, so it will always be the same every iteration. Also, you aren't summing into total so that will also always be unchanged.
#include <cstdlib>
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
int days, firstday;
double numPay = 0.01;
double total = 0;
cout << "How many days did you work? " << endl;
cin >> days;
cout << "\n" << endl;
cout << " Day " << "\t\t" << "Amount Earned" << endl;
cout << "__________________________________" << endl;
while (firstday<=days)
{
cout << firstday << "\t\t\t" << numPay << endl;
total+=numPay-0.01;
numPay = numPay *2;
firstday++;
}
total+=numPay-0.01;
cout << " " << endl;
cout << fixed << showpoint << setprecision(2) << endl;
cout << "Total Amount To Be Paid is $ " << total << endl;
cout << " " << endl;
system("PAUSE");
return 0;
}
kk, got rid of the loop while with the days, added the total = 0. I'm subtracting the .01 because i'm suppose to start out with a penny and double it each day i work. I'm very new to this and apparently i don't have the right formula because i keep getting o when i input days.