Else....

Hello I need help with the if else statement for this cipher I'm working on. I'd like the program to break the cipher when I hit enter. How should I write it?
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// AICipher.cpp : Defines the entry point for the console application.
#include "stdafx.h"

struct Cipher

{
	char num;
	int i;

public :

	Cipher(void);
	~Cipher(void);
	int find(void);
	void systempause(void);		
	void clearscreen(void);	

}cipher;

	Cipher::Cipher(void)
	{
		char letter[] = {'a', 'b', 'c' , 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '\0'};


		for(i = 0; letter[i] != '\0'; i++)
		{
			if (&letter[i] == &letter[0]||&letter[27])
			{
				cout<<letter[i]<<endl;clearscreen();
				
			}

			
		}
		
		
	}

	Cipher::~Cipher(void)
	{
		Cipher();
	}

	int Cipher::find(void)
	{
		
			cout<<"Press 1 for Cipher\nPress 2 for Find:"<<endl;
			cin>>num;
			switch(num)
			{
			case '1' : Cipher();
			case '2' : find();
			
			default : cout<<"nope!";
			}		
		return 1;
		systempause();
	}

	void Cipher::systempause(void)
	{
		system("pause");
	}

	void Cipher::clearscreen(void)
	{
		system("CLS");
	}


int _tmain(int argc, _TCHAR* argv[])
{
	while (cipher.find());
	return 0;
}
jesusislord wrote:
I'd like the program to break the cipher when I hit enter.
What do you mean by 'break' in this case? Your request is ambiguous.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Cipher::Cipher(void)
	{
		char letter[] = {'a', 'b', 'c' , 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '\0'};


		for(i = 0; letter[i] != '\0'; i++)
		{
			if (&letter[i] == &letter[0]||&letter[27])
			{
				cout<<letter[i]<<endl;clearscreen();
				
			}

			
		}
		
		
	}


This section below. How would I include an else statement to make the cipher stop execute when I hit Enter? Like this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
		for(i = 0; letter[i] != '\0'; i++)
		{
			if (&letter[i] == &letter[0]||&letter[27])
			{
				cout<<letter[i]<<endl;clearscreen();
				
			}

			 else (return)
                          {
                         Break; find();
                           }

		}
		
Last edited on
If I understand correctly, you want a quick way to stop executing the method if the user presses enter. I didn't use it much, but the getch() function returns a character corresponding to the key pressed, '\r' for "return" as we used to call the enter key. You'll need to include conio.h

if(getch() == '\r') break;

By the way, you should also ban the usage of system() because of how slow and system-specific it is, especially if you're already making classes
Last edited on
By the way, you should also ban the usage of system() because of how slow and system-specific it is, especially if you're already making classes


I do not know of any other way to make the program compile with clearscreen() and systempause(). I would really appreciate if you could help me fix this as well. Thanks!
You have misconceptions about the console. It is mot meant to be used the way you are using it - it's just meant to be a convenience to see the output and give input. If another program tried to run your program and give input and output what do you think would happen when you wanted to clear the screen or pause and wait for the enter key?
You have misconceptions about the console. It is mot meant to be used the way you are using it - it's just meant to be a convenience to see the output and give input. If another program tried to run your program and give input and output what do you think would happen when you wanted to clear the screen or pause and wait for the enter key?


Okay, but can you still help me better understand how to get the program to execute while also closing and opening the program effectively.
Last edited on
Clearing the screen, although I suggest reading the ENTIRE article this is the part that you are interested in: http://www.cplusplus.com/articles/4z18T05o/#Windows
But why are you bothering to write output to the screen if you're just going to clear it right away?

Your "systempause()" is useless since you call it after returning from the function.

You cancel a running console program with "Ctrl+C". Writing your own method for doing this doesn't make sense.
I smell Troll : IMO, the code is full of stuff designed to attract comment. Calling a constructor from the destructor????
I smell Troll : IMO, the code is full of stuff designed to attract comment. Calling a constructor from the destructor????

I will admit I am a little bit of an amateur, but I am completely serious about my code and I need help. So, please don't make this harder for me. Maybe you can help me TheIdeasMan. Since you mentioned about the constructor in the destructor I just learned something completely new because of you, and thanks! I thought calling the Cipher() function in the destructor helped the heap in some way, but apparently I was wrong. Would you care to help better understand some more coding fundamentals for C++? It would definitely help! Thanks everybody who has contributed to this post.
Last edited on
There is so much wrong with your program, that I seriously suggest you read up on the tutorials and any other beginners resources you can get get hold of. Apart from that, I am not going to comment any further.
Topic archived. No new replies allowed.