int main()
{
    cout << "This Program was written by Nehal Shah\n" << endl;
   
    int num, total;
    cout << "Please enter an integer number:\t\t\t";
    cin >> num;
    while ( num < 0 )
{
    cout << "\t********************************\n";
    cout << "\t**********  ERROR  *************\n";
    cout << "\t********************************\n";
    cout << " --------------------------------------\n";
    cout << "  Only input a positive integer number\n";
    cout << " --------------------------------------\n\n";
    cout << "Please enter the number again:\t\t\t";
      cin >> num;
}
     
      for (int total = num; total <= num; num++)
          cout << "The sum of all number from 1 up to " << num << " is:\t";
          cout << (num+ num) << endl;
    system("PAUSE");
    return 0;
}

i get the error on the total.. any ideas on how i could fix this? thanks..

hi,

my goal for this specific program is that it should ask me to enter an integer number. if i enter negative integer, i should get an error. because only positive integer allowed! once i enter the positive integer such as 32, i want to get sum of all integer numbers from 1 up to 32

any help?

Well, either your math is off or your logic is off. Maybe a complete example will help clear things up:

#include <iostream>
#include <ios>
#include <limits>

int main()
{
  int limit;

  std::cout<<"This Program was written by Julienne Walker\n\n";
  std::cout<<"Please enter a positive upper limit (exclusive): ";

  while ( !( std::cin>> limit ) || limit < 0 ) {
    // Correct truly invalid input as well as negative values
    std::cin.clear();
    std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
    std::cout<<"Error, invalid input. Please enter a positive number: ";
  }

  if ( !std::cin.eof() ) {
    int sum = 0;

    for ( int i = 1; i < limit; i++ )
      sum += i;

    std::cout<<"The sum of [1,"<< limit <<") is "<< sum <<'\n';
  }
}
for ( int i = 1; i < limit; i++ )
      sum += i;

In the above code you might wanna change it to

for ( int i = 1; i <= limit; i++ )
      sum += i;

Though it's not much of problem -_- cause you mentioned it was an example not the actual program. Anyways, that's not my problem here, I tried this in Visual Studio C++ 2008 (express Edition) but std::cin.get(); didn't halt the screen. In other treads you mentioned that it's good to stop the screen and that getch(); shouldn't be used since it's actually a compiler extension. In order to get std::cin.get(); to work, I had to include std::cin.ignore(); before using it. I just want to know if it's alright to use it. Does doing so makes it use more memory? I just recently returned to C++ and found that the standardization has changed -_- I last did C++ like 7 -8 years before. Gwad!

@koolboy
Your program will never give the total because there is nowhere where you are actually adding the numbers. it will just continue to print the same again and again, till the memory is lost (i guess). It's only incrementing the num variable and nothing else.

if you want the same program in a simple format it's here. it's actually the same one Naure wrote, but since I haven't done <limits> and <ios> so, I simplified it to make myself understand it. lame i know..

#include <iostream>
using namespace std;
int main()
{
  int limit;

  cout<<"This Program was written by Julienne Walker\n\n";
  cout<<"Edited by ChaseVoid"<<endl;
  cout<<"Please enter a positive upper limit (exclusive): ";

  while ( !( cin>> limit ) || limit < 0 ) 
  {
    // Correct truly invalid input as well as negative values
    cout<<"Error, invalid input. Please enter a positive number: ";
  }

     int sum = 0;

    for ( int i = 1; i <= limit; i++ )
      sum += i;

    cout<<"The sum of [1 - "<< limit <<"] is "<< sum <<'\n';
  
 
  cin.ignore();
  cin.get();
  
}

Sorry Narue for editing it without asking. Please point out what more errors are there. ( I mean Standard syntax wise).

>Though it's not much of problem -_-
It's not a problem at all, just a different design. My program prints an open range [0,n), not a closed range [0,n] like yours. Notice how the prompt specified that the upper limit is exclusive. ;)

>I tried this in Visual Studio C++ 2008 (express Edition)
>but std::cin.get(); didn't halt the screen.
It really doesn't matter what compiler you use. Mixing formatted and unformatted input will consistently give you problems.

>In order to get std::cin.get(); to work, I had to include std::cin.ignore(); before using it.
You're on the right path, but just ignoring a single character doesn't always work. Check out this thread for details on how to robustly clean up the input stream.

>I just want to know if it's alright to use it.
That's a different issue entirely, and it depends on how your program will be used. This whole solution (cleaning up the stream and then pausing) is really only useful if you're running the program interactively in an owned window and the window closes when the program terminates. If you plan to run the program from the command line then the solution becomes an annoyance. Even worse, if you want your program to be used as a filter, it can get kind of messy.

>I last did C++ like 7 -8 years before.
C++ was standardized in 1998 and most of the pieces were known and implemented for years before that. ;) But I know what you meant.

>Sorry Narue for editing it without asking.
While I appreciate it, there's no need to apologize. Any code I post is free for use however you like.

>Please point out what more errors are there.
Well, the big thing is that you remove the error correction code in the loop. Type a letter instead of a numeric digit and watch it run until the heat death of the universe.

>Notice how the prompt specified that the upper limit is exclusive.
Oh! I see so those were on purpose. lol, and silly me, I though it was a type-o..

>only useful if you're running the program interactively
>and the window closes when the program terminates.
>it can get kind of messy.
Exactly, the only reason, I use it is to halt the comand promt after the execution of the code and nothing much. I know what you mean about getting messy. >_< It's just bums out, when you start seaching the whole program for error, only to notic after a long time it's that minute thing which becomes too big of a deal. And C/C++ being case-sensitive we need to really be careful.

>C++ was standardized in 1998
Waa... NOW that's NEW's to me, I read somewhere that it was changed in 2003, but still in my class books they are still usuing #include<iostream.h> . It's so annoying, when you try to compile it and it goes around showing weird errors, and like everyone there gpes around about bitching that the compiler is corrupted. They are ONLY using int main() because GPP doesn't support the void return value. But the actual problem is that, as you stated earlier that, retuning nothing ain't an option.

A question, was namespace std also there in the 1998 Standardization? Ooops, I should do that my self. ><..

>Type a letter instead of a numeric digit and watch it run until the heat death of the universe.
Haahaa, yeah totally, >_< it's so effin' annoying, it won't just halt or show error, just continue to print like rabid rats. (BTW, Eww rats) Sorry I didn't got that part, cause as I stated before I haven't completed my C++ so yeah.. :blush: ^///^ heheh.. Well, that's a nice info I got.
I get it now..

std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );

This will actually exclude everything else after the assigned default numeric limit set by the compiler. But still I don't understand the actual function, that how this code is executing. uh! I soo need to study more, off I'm to the thread to clean. xDD

>I read somewhere that it was changed in 2003
The C++ standard was first ratified in 1998, and updated slightly in 2003. With any luck, a bigger revision will be finished in 2008 sometime.

>A question, was namespace std also there in the 1998 Standardization?
Yes.

>But still I don't understand the actual function, that how this code is executing.
It's pretty simple, actually. If you translated it to C, you'd get something like this:

#include <stdio.h>

// cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
size_t i;

for ( i = 0; i < (size_t)-1; i++ ) {
  int ch = getchar();

  if ( ch == EOF || ch == '\n' )
    break;
}

That's assuming streamsize equates to size_t, which isn't required.

>The C++ standard was first ratified in 1998,
WOW.. I so didn't knew about that. I should sue them for not teaching me the correct syntax >_< but then again, I should also have researched more :((


>a bigger revision will be finished in 2008 sometime.
That;s great, hopefully it'll be also fun to learn all the new changes and the added features.

>It's pretty simple
>translated it to C
Oh! thankx, now I understand it. We are checking, whether it's in integer limit and excluding the characters. Cool. It really helps to, translate the hard to get parts into C. I'll remember this handy trick, for the future. Thank you. ^///^

This article has been dead for over six months. Start a new discussion instead.