I don't get how to use "the do-while loop".

I'm just learning about the do-while loop, and about the if and else thing. By reading the tutorial on this website, I thought the "do" is supposed to repeat the code again and again until the "while" condition is fulfilled. When I use this code and type a number in, the program closes!


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 "stdafx.h"
#include <string>
#include <iostream>
#include <sstream>

using namespace std;


int main ()

{
	int kincaid42;
	string toend = "press 0 to end.\n";
	
	do { 
		
		cin >> kincaid42;
		if (kincaid42 > 0)
			cout << "postive. " << toend;
		else if (kincaid42 < 0)
			cout << "negtive. " << toend;
	 }
	
        while (kincaid42 == 0);
		char t;
		cin >> t;
	

}


As you can tell I'm using Visual. I use the char t; and cin >> t; to keep the program from closing when i type one number in.


**side question** Is it even possible for a 10th grader (going to 11th after summer) to learn c++? Or should I wait after high school and take this in college?
The 'while' tells you when to loop. Its just like English: "Do this while kincaid42 is equal to 0." That mean that the loop will only continue WHILE kincaid42 is 0. That doesn't sound to me what you want here.

You probably want to "Do this while kincaid42 is not equal to 0". Because when kincaid42 is equal to 0 you want to stop.
The loop continues as long as the while condition is fulfilled (note: while x is true), not until.

kincaid42 wrote:
**side question** Is it even possible for a 10th grader (going to 11th after summer) to learn c++? Or should I wait after high school and take this in college?

If that reassures you: I started programming in 4th grade and started with C++ in 5th grade and I had no problems.
I would say that learning is easier when you're young, not harder.

By the way, learning programming had a positive (and in retrospect very important) side-effect for me:
before that, I wasn't really any good at maths, but after starting programming, solving problems methodically and logical thinking became easy to me, so my math grades quickly went to the top.
This ended with me studying maths at the university today.

I don't know how good you're at maths, but learning programming sure will help you with maths (and vice versa).
Topic archived. No new replies allowed.