Really simple code, help

So, I've never programmed anything before and I kinda started playing with it, and Im trying to make a very simple thing, it counts to 20 after I press 0 and its supposed to stop when it hits 20 seconds, but it won't stop.

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
// program test "Loop"

#include <iostream>
#include <windows.h>
using namespace std;

int main ()
{
    int n;
    int *np;
    np = &n;
    int c = 1;
    cout << "Push 0 and enter: ";
    cin >> n;
    
    while (n==0) {
          cout << c << ", ";
          ++c;
          Sleep(1000);
}
    if (c>20) {
    *np = 1;
    cout << "Finished.\n";
}

system("PAUSE");
return 0;
}


I think Im in an entirely wrong path, please tell me what I'm doing wrong
Your condition inside the while loop is incorrect. You should loop while the count hasn't reached 20. If you don't want to run the while loop unless zero is entered, then you contain your loop inside an if statement.
That did it, but now when it hits 20, it skips the part where its supposed to say "Finished" and finishes the program.

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
// program test "Loop"

#include <iostream>
#include <windows.h>
using namespace std;

int main ()
{
    int n;
    int *np;
    np = &n;
    int c = 1;
    cout << "Push 0 and enter: ";
    cin >> n;
    
    if (c<21) {
              while (c<21) {
                    cout << c << ", ";
                    ++c;
                    }
    } else {
                    cout << ".\nFinished.\n";
                    }
                    
system("PAUSE");
return 0;
}
closed account (10oTURfi)
hmmm...
This should do if I understood what you wanted.
1
2
3
4
5
6
7
8
9
int main()
{
    int n;
    cout << "Push 0 and enter: ";
    cin >> n;
    if(n==0)
        for(int c=0; c<=20; c++){ cout << c << ", "; if(c==20) cout << "Finished!\n"; }
    else cout << "you must push 0!" << endl;
}
Plz explain is briefly , but for now I think this is what you want

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

int main()
{
    int n=1;
   
    while (n!=0)
    {
    cout << "Push 0 and enter: ";
    cin >> n;
     
    if(n!=0)
    cout<<"You must press 0";

    }
 
for(int n=1;n<21;n++)
cout<<n<<", ";

cout<<"Finished ! ";

getch();

}
Last edited on
I'm just as fresh as you are, and as a result having started I have found it very beneficial to do my brackets a particular way not to save lines but for absolute clarity of reading. There's no right answer from what I can tell but I do it like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
				if(player_pick > 3 || player_pick < 1){
					system("cls");
					cout << "Invalid selection\n";
					cout << "Please select 1, 2, or 3\n";
				}
				else if (pile == 0){
					system("cls");
					cout << "You took the last toopick\n";
					cout << "You lose\n";
					break;
				}
				else{
				        system("cls");
					cout << "You take " << player_pick << " toothpicks\n";
					cout << "There are " << pile << " toothpicks left\n";
				}


Sorry for a blurb of unrelated but equally basic code, personally I have found viewing as many simple arguments as possible the best way to learn so far anyhow.

EDIT: LOL @ tabulation... Time for more functions and less nesting.


Last edited on
@Krofna
You did forget that Tryed wanted a Sleep(1000), so that each number took a second before being printed. So, here is my version. This program will keep looping til a '0' is pressed.
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
30
31
32
33
34
// N Loop.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <windows.h>

int main()
{
	int n=-1;


	do
	{
		std::cout << "Push '0' and 'ENTER' to begin: ";
		std::cin >> n;
		if(n==0)
			for(int c=0; c<=20; c++)
			{ 
				if ( c < 10)
					std::cout << "0";
				std::cout << c << ", ";
				Sleep(1000);
				if ( c==9)
					std::cout << std::endl;
				if(c==20) 
					std::cout << "Finished!\n";
			}
		else 
			std::cout << std::endl << "You must push 0 to start!" << std::endl;
	} while (n!=0);
	system("pause");
	return 0;
}

If you press anything BUT numbers, it goes into an endless loop.
closed account (10oTURfi)
@whitenite1
Ah, didnt notice sleep, but np, easy to add
Also I being lazy to add a do while loop mainly beacuse i didnt write that code in compiler, but in a post counting 4 spaces as a replacement for tab lol.
closed account (zb0S216C)
Tryed wrote:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main ()
{
    int n;
    int *np;
    np = &n;
    int c = 1;
    cout << "Push 0 and enter: ";
    cin >> n;
    
    while (n==0) {
          cout << c << ", ";
          ++c;
          Sleep(1000);
}
    if (c>20) {
    *np = 1;
    cout << "Finished.\n";
}
(sic)

First, if n contains a value other than zero when the while loop is encountered, the loop never executes. If n contains the value of zero when the while loop is encountered, the loop never stops since you never told it when to stop. As a result of this, you have an infinite loop. To remedy this, incorporate the break statement or rethink your condition.

Second, your if statement is outside the while loop, therefore, it has no effect.

[i]Wazzak[/tt]
Last edited on
Topic archived. No new replies allowed.