While loop not accepting int changes?

I've read and re-read my manual, and have tried to come up with the proper search terms (But none come to mind), so I'm forced to be the imbecile with a newbie question-

It seems that this while loop does not pertain to loop logic; this is a run-time error I cannot get my head around. Perhaps it's merely some loop logic leftover from my days of BASIC, though.

Here's the snippet:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main()
{
  int numchar = 6;
  cout << "+";
  while (numchar > 0)
     {
       numchar - 1;
       cout << "~";
     }
  cout << "+";
  return 0;
}

Upon having it output the value of "numchar," rather than the "~," it never changes from 6 to 5, to 4, and so forth. According to the book I'm reading (Written in 2010, for Windows; I'm using Linux) it looks like it should work...

The result should be:

 
+~~~~~~+


But it is:

 
+~~~~~~~~~~~~~~~

And it continues indefinitely.

Upon having it output the value of "numchar," rather than the "~," it never changes from 6 to 5, to 4, and so forth. According to the book I'm reading (Written in 2010, for Windows; I'm using Linux) it looks like it should work...

What am I doing wrong? Thanks in advance!
Last edited on
numchar - 1;
This by itself does not do anything. You need to use assignment to actual modify the value of your variable.

Either:
1
2
3
numchar = numchar - 1;
numchar -= 1;
numchar--;
Last edited on
EDIT: Genado ninja'd me.

Line 12 does nothing:

numchar - 1;

It subtracts 1 from numchar and then does nothing with the result of that calculation.

You probably meant to do numchar = numchar - 1; or the more compact --numchar;
Last edited on
Thank you, both! :-)
Topic archived. No new replies allowed.