beginner C++

Still in class for intro to C++ and I'm working on one of the labs. In this lab, the program prompts the user to input a number (x), from there the program sums up the numbers from 1 up to (x). For example:
input      sum
1            1
2            3      //(2+1)
3            6      //(3+2+1)
4            10    //(4+3+2+1)
5            15   //(5+4+3+2+1)
etc...      etc...

This is the code for the function called by main:

int find_sum(int value)
{
 int i=0;
 
 i=value;
 
    if (value==1)
  {
   sum=value;
  }
 else
  {
   
   for(i=0; i<=value; ++i)
   {
    sum=value+i;
   }
  }
 return sum;
}

I know something is wrong in the loop, but can't figure out what. I've tried several different loops and nesting just about everything I know of in C++ but still missing something.....(something simple, I'm sure). Just need some fresh eyes to look at this, thanks for your time,
                           Tony



Answer this question

beginner C++

  • nasr0001

    Thank god, I was starting to run out of excedrin and advil, lol. Now that I see the code, I understand how and what the program has going on. As far as the iostream.h, I've tried using it both ways using namespace std and both work the same. I didn't try using the last one (standard flags ) std::cin;std::cout; but aside from them being different ways to use io, what's the difference between them I apologize for all the questions, but again, just learning c++(week 10 now and exams next week with lots of labs due) and going to have C++ II next quarter. Thanks again for your time and patience,
                Tony

  • Potyos

    I appreciate that, I understand a good bit in the short time I've been in C++, I just have problems actually building the programs for the labs. I came from using VB, then quickly found out it's about the same as day and night, lol. I'll probably be back here either sometime tonight or tomorrow night trying to get the rest of these labs done, between working 60 hours a week and going to school full time, I'm doing good to only be this far behind and the bad part is, I'm still ahead of most of the class, lol. Once again, thanks for all your help,
                      Tony

  • tobyxu

    Probably so, the next lab is asking the user to input a number, then from there, is writes out each digit of the number, (user enters 99, output is nine nine|| user enters 986, output is nine eight six). Teacher said something about using division and modulus operators to extract each digit from the input, then call a function to write out each individual digit. Don't sound too bad, but I said that about the last lab as well, lol. Thanks again,
             Tony

  • HarmonicSoftware

    int i=0;
     
     i=value;

    This is redundant.  You can put int i = value, but either way, i is not used her, it's reset to 0 at the start of the loop which does use it.  You shouldn't make variables visible outside the scope where they are needed.

    I don't see anywhere that sum ( which I assume is a member variable ) is reset to 0.  Is it always o

    sum=value+i;

    You probably mean sum = sum + i;  You can also start the loop at 1, because adding 0 won't do anthing :-)

    int find_sum(int value)
    {
     int sum=1;
     
      if (value!=1)
      {
       
       for(int i=1; i<=value; ++i)
       {
        sum=sum+i;
       }
      }
     return sum;
    }

  • Dgates123

    what's the difference between them

    The core difference is that one is standard C++ and one is not.  VC2005 no longer has the non standard headers, so if you get into the habit of using iostream.h, two things will happen

    1. your code will be less portable
    2.  if your code tries to work with code that uses the c++ headers, you will have problems.

    I'm not sure that there are tons of differences, the obvious one is namespace std, but it's just better to use the 'real' one.  And I like being able to explicitly state what's coming into scope from that #include statement.

    I apologize for all the questions

    Not at all, that's what these forums are for.  Good luck with your exams :-)


  • ko712000

    What I was trying to do is use the loop counter to add previous numbers to the value the user entered,


    OK - that wasn't going to work, because you don't keep adding to sum in your code.  You'd end up with value * 2 being the sum, because each time, sum is not incremented, it is changed to i + value, and the last value for i is the same as value.  If you replace your function with the code below, it should work:

    int find_sum(int value)
    {
      sum=0;
     
       for(int i=1; i<=value; ++i)
       {
        sum += i;
       }

     return sum;
    }

    I've used += to increment sum by the value of i, and removed the if statement ( the code I posted before had a bug, it would be off by 1 ).

    Also, iostream.h is not valid C++, it comes from the version of C++ that predates the C++ standard.  You should replace

    #include <iostream.h>

    with

    #include <iostream>

    using namespace std;

    Or, you could do even better and replace

    using namespace std;

    with

    using std::cin;
    using std::cout;



  • Ross Goodell

     between working 60 hours a week and going to school full time

    That's a hell of schedule, I hope it all pays off for you.

    Glad to help, from the sound of it, I'll probably get the chance to help some more before the week is out :-)




  • djee

    This is the entire program:
    #include <iostream.h>

    int find_sum(int);
    int sum=0, value;

    int main()
    {
     char answer='y';
     
     do{

     cout << "\n Please enter value to sum. ";
     cin >> value;

     find_sum(value);

     cout << "\n Your sum is " << sum << endl;

     cout << "\n Want to sum again y=yes, n=no ";
     cin >> answer;
     }while (answer=='y'||answer=='Y');
      return 0;
    }

    /*========================================================================*/

    int find_sum(int value)
    {
     int i;
     
     i=value;
     
        if (value==1)
      {
       sum=value;
      }
     else
      {
       
       for(i=0; i<=value; ++i)
       {
        sum=value+i;
       }
      }
     return sum;
    }

    What I was trying to do is use the loop counter to add previous numbers to the value the user entered, (user enters 5, the loop would run through 4 times adding the counter to the variable "value", if that made any sense, lol) basically if they entered 5, the 5 being "value" is passed to the called function, from there it would add all numbers less than or equal to 5(5+4+3+2+1=15, 15=sum) and sum, would be passed back to main where the "sum" would be displayed through cout.


  • beginner C++