Help please, i'm not sure if this is right

Pages: 12
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;

system("PAUSE");
return 0;
}
Please use code tags in the future.

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
I get everything to come up now but i don't get the sum of the numbers between what ever i put in.

using namespace 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;
}

int sum(int base, int limit)
{
int sum = 0;
for (int i = base; i <= limit; i++){
sum + i;
}
system("PAUSE");
return sum;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cstdlib>

using namespace 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]
Last edited on

#include <cstdlib>
#include <iostream>

using namespace 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; getting error here
}

int sum(int base, int limit)
{
int sum = 0;
for (int i = base; i <= limit; i++){
sum += i;
return sum;
}


found the sum +=i; but you beat me to it :D
i hit the <> button before i posted
never mind, forgot to put the ; after system (PAUSE);

now the error is at the end
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <cstdlib>
#include <iostream>

using namespace 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
Last edited on
You need to highlight your code before you press the button. And where is the error at now that you added the semicolon after system("pause"); ?

There is an extra 0 in your prototype that I just noticed:
int sum(int base, int limit0);
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <cstdlib>
#include <iostream>

using namespace 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.
Sorry, it was due to my bad copy of your program. There is an extra { at the end of you for loop on line 27
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <cstdlib>
#include <cmath>
#include <iostream>

using namespace std;

int main()
{
    int days;
    double pennies = 0.01;
    double total = 0.0;
    
    
    
    cout << " For how many days will the employee work " << endl;
    cin >> days;
    
    cout << " Day           Total Pay \n" << endl;
    cout << "_________________________\n" << endl;
         
         for ( int count =1; count <=days; count++)
         {
             cout << count << "t\t$\t" << (pow(2, pennies)) << endl;
         }
    cout << setprecision(2) << fixed << showpoint << endl;
    cout << "_________________________________ \n" << endl;
    cout << "Total             $\t" << total << endl:
         
     system("PAUSE");
     return 0;
}


I am suppose to have the person start with a penny and be doubled every day worked. Can you tell me if i'm even on the right track here.
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    int days, firstday;
    double numPay = 0.01;
    double total;
    
    cout << "How many days did you work?  " << endl;
    cin >> days;
    cout << "\n" << endl;
    
    while (days <1 || days>31)
    {
   cout << " Day " << "\t\t" << "Amount Earned" << endl;
   cout << "__________________________________" << endl;
   
   while (firstday<=days)
   {
         cout << firstday << "\t\t\t" << numPay << endl;
         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;
}


How does this look, other then the error i'm getting on line 29
You need to include iomanip.

Also, total should be += to numPay and in your while loop.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int days, firstday;
    double numPay = 0.01;
    double total;
    
    cout << "How many days did you work?  " << endl;
    cin >> days;
    cout << "\n" << endl;
    while (days <1 || days>31)
    
   cout << " Day " << "\t\t" << "Amount Earned" << endl;
   cout << "__________________________________" << endl;
   
   while (firstday<=days)
   {
         cout << firstday << "\t\t\t" << numPay << endl;
         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;
}


Fixed what you told me to except the total, not really sure what you meant. I got it to work but it only displays 0.00
First off, line 10:
double total;
total should start at 0

line 15:
while (days <1 || days>31)
This loop doesn't work as you wanted it to

line 26:
total+=numPay-0.01;
This is still not in the while loop like I said. Also, why are you subtracting the 0.01 from total?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace 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.
Because what is the value of first day? you never define it.

Its suppose to be 0.01

Just realized that, just trying to figure out how to fix it.
Pages: 12