I am totally stuck on this problem and would love it if anyone knew how to help me fix it! Thanks for any input!!!

Here are directions:
Write a program that asks the user for a positive integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loops will find the sum of 1,2,3,4,....50.
Input Validation: Do not accept a negative starting number.

And this is what I have so far:

#include <cmath>
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
    int number, maxnumber, total;
    {  
cout << "Enter an integer number: ";
    cin >> number;
}
    while (number < 0)
    {
       cout << "ERROR: PLEASE ENTER A POSITIVE INTEGER: " << endl;
       cin >> number;

       }
    for (number = 1; number <= maxnumber; number++)
    {
    total += number;
}

    cout << "The sum of all integer numbers from 1 to ";
    cout <<  << " is: " << total << endl;


      if (number >= number);

    system("PAUSE");
    return 0;
}

Recommended Answers

All 7 Replies

I made red what I changed or commented

#include <cmath>
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
    int number, maxnumber, total;
    {//Why us bracket's here?
        cout << "Enter an integer number: ";
        cin >> number;
    }
    while (number < 0)
    {
        cout << "ERROR: PLEASE ENTER A POSITIVE INTEGER: " << endl;
        cin >> number;
    }
    for (maxnumber = 1; maxnumber <= number; maxnumber++)
    {
        total += maxnumber;
    }

    cout << "The sum of all integer numbers from 1 to ";
    cout <<  << " is: " << total << endl;
      
    //don't use system dependable functions, when you can replace them.
    cin.ignore();
    cin.get();
    return 0;
}

Haven't tested it but I think it should work. You should to always indent your work.

I am totally stuck on this problem and would love it if anyone knew how to help me fix it! Thanks for any input!!!

When asking for help, you really need to tell us what your problem is. We can't always guess.

Here are directions:
Write a program that asks the user for a positive integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loops will find the sum of 1,2,3,4,....50.
Input Validation: Do not accept a negative starting number.

So in your code:

for (number = 1; number <= maxnumber; number++)
    {
    total += number;
    // What is the starting value of total?  It isn't 0. You never initialized it.
}

    cout << "The sum of all integer numbers from 1 to ";
    cout <<  << " is: " << total << endl;
//          ^   what goes here?
    
      if (number >= number);
// This IF does nothing...
      
    system("PAUSE");
// Don't call SYSTEM, use [I]cin[/I] or [I]cin.get()
[/I]

Ok so I changed this program around a little bit with the help of those who replied, thank you!!! But now I just can't seem to get the math right. My output has to be the total of all of the integers up to the number inputted. What did I do wrong?

This is what I have so far:

#include <cmath>
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
    int number, maxnumber, total;
    
    cout << "This program written by kp for cs102 Online." << endl;
    cout << endl; 
    cout << endl;
    cout << "Enter an integer number: ";
    cin >> number;

    while (number < 0)
    {
       cout << "ERROR: PLEASE ENTER A POSITIVE INTEGER: " << endl;
       cin >> number;
       
       }
    for (maxnumber = 1; maxnumber <= number; maxnumber++)
    {
        total += maxnumber;
    }

cout << "The sum of all integer numbers from 1 to ";
    cout << number << " is: " << total << endl;

    cin.ignore();
    cin.get();
    system("PAUSE"); // dont use this if using cin.get()
    return 0;

Anwer to your question has alread been anwered by Mr. WaltP.

// What is the starting value of total? It isn't 0. You never initialized it

Incorporate that change and post your code in code tags so it is in readable form. See the forum announcements for it.

I know I'm supposed to have a starting value for total but I don't really understand what it is or where I should put it, I'm sorry if I sound really stupid but I'm very new to this so most stuff takes me awhile to figure it out. So where and what is total then?

IT is better to initialize the values while declaring them to avoid these type of small bugs. Do somtthng like this at the startt of your program and it should work out fine.: int number = 0, maxnumber = 0, total = 0;

I know I'm supposed to have a starting value for total but I don't really understand what it is or where I should put it, I'm sorry if I sound really stupid but I'm very new to this so most stuff takes me awhile to figure it out. So where and what is total then?

If you are going to add numbers to a running total, what value should the total start with before you start adding? -10? 20? 0? Pick one. Key point, before you start adding...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.