follow up: console closing down, code example

Pages: 12
Jul 22, 2018 at 9:54pm
I wanted to follow up about the console closing, and one of the solutions. Is there any way I could get some help debugging my code, and turning this into a personal library to access in more complicated situations?

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
// keep console open.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

class KeepRunning {
public:
~KeepRunning() {
	        string plans;
		      if (cin >> plans);//when user input is entered, we want it to print response.
		          std::cout << "Press ENTER to continue...";
		           std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		
/* I intended to use this as a method for keeping the console open, but I had some issues. This is a: a trivial program, and I want some help using this with something more complex.B: This method had worked initially, but printed two "press enter"s, and did not give me the result I wanted after attempting to patch the initial result. In other words, without the condition it worked fine, but broke as soon as I added in the condition.  *
	}
};

int main()

{
	KeepRunning kr; // this is my first instance of the the "keep running" solution that seemed to work initially. 
	string response;
	
	bool isgood = false;
	bool isbad = false;
	string good_bad; // I define my good or bad response here.
	string plans;// I wanted to ask the user what their plans were. 
	cout << "hello\n";// first say hello.
	cout << "what is your name";//ask them for their name. 
	string name;
	cin >>name;
	cout << "nice to meet you!\n:" << name << endl;
	cout << "how are you today";
	cin >> good_bad;
	/*cin.ignore(); // was intended as the initial solution, but was not able to cut it. 
	cin.get();*/ 
	if (good_bad=="bad")
	{
		cout << "i'm sorry to hear that";
		
	
	} 
	else cout << " glad to hear it!, any plans?";
	cin >> plans;
	if (cin >> plans);
      cout << "great to hear!";
	  KeepRunning kr2;
		 
		 
		 
	 }






Last edited on Jul 23, 2018 at 4:49pm
Jul 22, 2018 at 9:59pm
Please, use correct code formatting.
Edit your post and add [code] and [/code] around your C++ code.

I don't know know exactly what your question is, but if you want us to tell you what's wrong with your program, the most obvious thing is that you have semi-colons after your if statements.

If-statement rundown:

Curly braces used can execute multiple statements:
1
2
3
4
5
if (something)
{
     this_statement_will_conditionally_execute;
     this_will_too;
}


If no curly braces used, can only execute one statement:
1
2
3
if (something)
    this_statement_will_conditionally_execute;
this_statement_will_always_execute;


Semi-colon used: (bad)
1
2
3
if (something); // semi-colon! Wrong!
this_statement_will_always_execute;
this_statement_will_always_execute_too;
Last edited on Jul 22, 2018 at 10:24pm
Jul 22, 2018 at 10:05pm
So you are saying that semicolons imply it will always execute? Could this have something to do with why the debugging solution is not working? In my comments, I mention a solution I found in the "console closing down" thread that I expected to work but did not. I was hoping that if I a: optimized my i/o program, and b: was able to modify the OOP style solution I would be able to chug along. I would like to be able to fashion a console output solution that will work in all situations, and therefore could be used in a library.
Jul 22, 2018 at 10:10pm
Here we go:)!

Removed the semicolons! The next step would be to solve why my solution isn't working.

[code]

// keep console open.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

class KeepRunning {
public:
~KeepRunning() {
string plans;
if (cin >> plans)//when user input is entered, we want it to print a response.
std::cout << "Press ENTER to continue...";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
/* I intended to use this as a method for keeping the console open, but I had some issues.
This is a: a trivial program, and I want some help using this with something more complex.
B: This method resulted initially, but printed two "press enter"s, and did not give me the result I wanted
after attempting to patch the initial result.
In other words, without the condition it worked fine, but broke as soon as I added in the condition. */
}
};

int main()

{
KeepRunning kr;// this is my first instance of the the "keep running" solution that seemed to work initially.
string response;

bool isgood = false;
bool isbad = false;
string good_bad; // I define my good or bad response here.
string plans;// I wanted to ask the user what their plans were.
cout << "hello\n";// first say hello.
cout << "what is your name";//ask them for their name.
string name;
cin >>name;
cout << "nice to meet you!\n:" << name << endl;
cout << "how are you today";
cin >> good_bad;
/*cin.ignore(); // was intended as the initial solution, but was not able to cut it.
cin.get();*/
if (good_bad=="bad")
cout << "i'm sorry to hear that";
else cout << " glad to hear it!, any plans?";
cin >> plans;
if (cin >> plans)
cout << "great to hear!";
KeepRunning kr2;



}

[/code]




Last edited on Jul 23, 2018 at 4:48pm
Jul 22, 2018 at 10:25pm
Read the two first sentences of my last post, please.
Jul 22, 2018 at 11:40pm
Yes, you had mentioned that my formatting was off, and I had thought I had corrected it. Did I not?
Jul 23, 2018 at 2:56am
Yes, you had mentioned that my formatting was off, and I had thought I had corrected it. Did I not?
You did not.

Place your source code within [code][/code] tags. For example, the literal input
[code]
# include <iostream>
int main() { std::cout << "Hello, World!\n"; }
[/code]

Produces
1
2
# include <iostream>
int main() { std::cout << "Hello, World!\n"; }
Jul 23, 2018 at 1:12pm
Revised:):

I had modified some basic i/o code to include a solution to the console closing, which I found in the other thread. It has not worked for me just yet, and I was hoping for some guidance on how to get it to work. The solution uses a class destructor as a way of triggering some sort of 'get' function, in order to keep the program running post-output every-time. I thought I had solved my problem, but ran into another bump in the road with it only seeming to work with two instances of object Kr. If I can slay the i/o issues, I can move forward.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

class KeepRunning {
public:
~KeepRunning() {
string plans;
if (cin >> plans);//when user input is entered, we want it to print response.
      std::cout << "Press ENTER to continue...";
      std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');


}
};


1
2
3
4
5
6
7
/* I intended to use this as a method for keeping the console open, but I had some issues. 

This is a: a trivial program, and I want some help using this with something more complex.
B: This method had worked initially, but printed two "press enter"s, and did not give me the result I wanted after attempting to patch the initial result. 

In other words, without the condition it worked fine, but broke as soon as I added in the condition. */


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

int main()

{
KeepRunning kr; // this is my first instance of the the "keep running" solution that seemed to work initially. 
string response;

bool isgood = false;
bool isbad = false;
string good_bad; // I define my good or bad response here.
string plans;// I wanted to ask the user what their plans were. 
    cout << "hello\n";// first say hello.
    cout << "what is your name";//ask them for their name. 
string name;
    cin >>name;
    cout << "nice to meet you!\n:" << name << endl;
    cout << "how are you today";
    cin >> good_bad;
/*cin.ignore(); // was intended as the initial solution, but was not able to cut it. 
cin.get();*/ 
if (good_bad=="bad")
    {
       cout << "i'm sorry to hear that";


} 
else cout << " glad to hear it!, any plans?";
cin >> plans;
if (cin >> plans);
    cout << "great to hear!";
    KeepRunning kr2;



}


Last edited on Jul 23, 2018 at 4:54pm
Jul 23, 2018 at 2:07pm
Could you please re-read what people have told you about code tags, and fix your posts accordingly, to make them readable?
Jul 23, 2018 at 4:12pm
I smell a troll.
Jul 23, 2018 at 4:41pm
I opoligize. I had placed the slash in the wrong place. Now, is it readable?
Last edited on Jul 23, 2018 at 4:44pm
Jul 23, 2018 at 4:46pm
Thankyou. Unfortunately, you seem to have managed to wipe out all the indentation from your code, so, although it's a little easier to read and understand than it was, it's still not as easy as it could be. It's definitely an improvement, though!

EDIT: I do like your RAII-esque solution to keeping the console window open :)

One question: Why do you need to use two KeepRunning objects, in your most recent code sample?
Last edited on Jul 23, 2018 at 4:49pm
Jul 23, 2018 at 4:50pm
do you need line 13 above the cin statment?
Jul 23, 2018 at 4:50pm
So it's easier to read. Are the comments helpful?
Jul 23, 2018 at 4:52pm
I have mainly been focusing on functionality over optics at this point, and c++ doesn't require formatting, but I can add indentation if needed.
Jul 23, 2018 at 5:08pm
Having the two objects was the only way it seemed to work. As far as the amount of cout lines, I'm sure there could be some concision. However, I did notice that the solution does work, just not as smoothly as I would like. I also need it to always work with my more complex code I took from a book I'm studying. This also crosses c and c++ syntax which is another area of concern.

Example:

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262

#include "stdafx.h"
#include <iostream>
#include <string>
#define _CRT_SECURE_NO_WARNINGS
using namespace std; 
//int main()
//{
//	
//	char note1, note2, dummy; /* note names,dummy char*/
//	int pc1, pc2, interval; /* pitch class,interval*/
//	cout << "please enter two natural notes" << endl; 
//	cout << "first natural note:";
//	cin >> (note1);
//	cout << "second natural note:";
//	cin >> (note2);
//	
//	
//	
//	switch(note1) 
//	{ case 'C':case 'c':
//		pc1 = 0;
//	case 'D':case 'd':
//		pc1 = 2;
//		break;
//	case 'E':case 'e':
//		pc1 =4;
//		break;
//	case 'F':case 'f':
//		pc1 = 5;
//	case 'G':case 'g':
//		pc1 = 7;
//		break;
//	case 'A':case 'a':
//		pc1 = 9;
//		break;
//	case 'B':case 'b':
//		pc1 = 11;
//		break;
//	default:
//		cout << "error:", (note1), "is not a natural note ";
//		return 1;
//	}
//	switch (note2)
//	{
//	case 'C':case 'c':
//		pc2 = 0;
//	case 'D':case 'd':
//		pc2 = 2;
//		break;
//	case 'E':case 'e':
//		pc2 = 4;
//		break;
//	case 'F':case 'f':
//		pc2 = 5;
//	case 'G':case 'g':
//		pc2 = 7;
//		break;
//	case 'A':case 'a':
//		pc2 = 9;
//		break;
//	case 'B':case 'b':
//		pc2 = 11;
//		break;
//	default:
//		cout << "error:", (note2), "is not a natural note ";
//		return 1;
//	}
//
//
//	/* calculate the interval and keep it mod 12. */
//	interval = pc2 - pc1; 
//	if (interval < 0) interval+= 12; 
//	else if (interval > 11) interval -= 12;
//	/* print the number of semitones. The special case of unison(0) 
//	 has to be handled correctly, so we use the conditional
//	 operator for this*/
//	//cout << "%d semitones up or %d semitones down\n", (interval), (interval ? 12 - interval : 0);
//	/* now we print out the interval name*/
//
//	switch(interval)
//	{ case 1:
//		cout <<"minor second up, or major 7th down" ; 
//		break;
//	case 2:
//		cout <<"major 2nd up or minor 7th down" ;
//		break;
//	case 3:
//		cout <<"minor 3rd up or major 6th down" ;
//		break;
//	case 4:
//		cout <<"major 3rd up or minor 6th down" ;
//		break;
//	case 5:
//		cout <<"perfect 4th up or perfect 5th down" ;
//		break;
//	case 6:
//		cout <<"augmented 4th" ;
//		break;
//	case 7:
//		cout << "perfect 5th up or perfect 4th down";
//		break;
//	case 8:
//		cout << "minor 6th up or major 3rd down";
//		break;
//	case 9:
//		cout << "major 6th up or minor 3rd down";
//		break;
//	case 10:
//		cout <<"minor 7th up or major 2nd down" ;
//		break;
//	case 11:
//		cout << "major 7th up or minor 2nd down";
//		break;
//		
//	}
//	
//	system("pause"); 
//	
//	return 0; 
//}
//

//int main{
//	int note, i;
//    
//cout<< "Please enter the key(in pitch class number, 0-11):";
///* make sure start of note is not negative*/
//while (note < 0;) note += 12;
///* build scale */
//for (i = 0; i < 7; i++;)
//{
//	/* translate pitch-class to note name*/
//	if (note % 12 == 0) cout << "C";
//	else if (note % 12 == 0)
//	else if (note % 12 == 1) cout << "Db";
//	else if (note % 12 == 2) cout << "D";
//	else if (note % 12 == 3) cout << "Eb";
//	else if (note % 12 == 4) cout << "E";
//	else if (note % 12 == 5) cout << "F";
//	else if (note % 12 == 6) cout << "Gb";
//	else if (note % 12 == 7)cout << "G";
//	else if (note % 12 == 8)cout << "Ab";
//	else if (note % 12 == 9)cout << "A";
//	else if (note % 12 == 10)cout << "Bb";
//	else cout << "B";
//	/* find the next pitch, class*/
//	if (i != 2) note += 2; 
//}

//}

///more compact version of the interval program:

//#include <stdio.h>
//#define _CRT_SECURE_NO_WARNINGS
//int nameToPc(char name) {
//	switch (name)
//	{
//	case 'C':case 'c':
//		return 0;
//
//	case 'D':case 'd':
//		return 2;
//
//	case 'E':case 'e':
//		return 4;
//	case 'F':case 'f':
//		return 5;
//	case 'G':case 'g':
//		return 7;
//	case 'A':case 'a':
//		return 9;
//	case 'B':case 'b':
//		return 11;
//	default: /* error code*/
//		return 100; 
//					}
//
//}
//
//int main()
//
//{
//	char note1, note2, dummy; 
//	int interval; 
//	printf("please enter two natural notes.\nfirst note:");
//	scanf("%c%c", &note1, &dummy);
//	printf("second note:  ");
//	scanf("%c", &note2);
//	/* to calculate the interval, we call nameToPc() to translate*/
//	interval = nameToPc(note2) - nameToPc(note1);
//	if(interval>20||interval<-11)
//	{
//		printf("either %c or %c are invalid notes\n", note1, note2);
//		return 1; 
//	    
//	}
//	if (interval > 11) interval -= 12; 
//	printf("%d semitones up or %d semitones down\n", interval, interval ? 12 - interval : 0);
//	
//	return 0;
//}

//#include <stdio.h>
//#include <string.h>
//int main()
//{
//	int note, i; 
//	char key[3];
//	std::string scale[12] = { "C","Db", "D","Eb","E","F","Gb","G",
//	 "Ab","A","Bb"};
//	std::cout << "Please enter capitals only";
//	std::cin >> scale[12]; 
//	for(i=0; i<12; i++)
//	{
//		if ((scale[i], key) == 0) { note = i; 
//		
//		std::cout << ("major scale", key);
//
//		}
//		if( note>=0) 
//		{
//			for (i = 0; i<7; i++)
//			{
//				std::cout << scale[note % 12];
//				if (i != 2) note += 2;
//				else note++;
//			}
//			std::cout << ("\n");
//		}
//		else { std::cout << ("invalid key", key); 
//		return 1;
//		}
//	}
//
//}

//example of pointer arithmetic to step through and array

#include<iostream>

int main()

{

	int i;
	for (i = 0; i<6; i++)
	{
		int array[6]{ 1,2,3,4,5,6 };
		int *pa;
		pa = &array[6];

		*pa = array[6];
		//pa++; 
		std::cout << array[i];


	}
	return 0;

}
Jul 23, 2018 at 5:14pm
If you just want the console to stop closing, then you need to prevent the program from ending. You do this by putting your code into a loop. It is how games are made.

1
2
3
4
5
6
while(true) {
//lots of code
   if(ready_to_quit) {
      break;
   }
}
Last edited on Jul 23, 2018 at 5:15pm
Jul 23, 2018 at 5:19pm
Okay:). So instead of fumbling with fancy workarounds, like the other thread I should just wrap my entire code in a while loop, and attach a key stroke to the break statement? Then I could just predict the program being done doing its thing, and have a cout "press enter to end" ready? Or... I could not, and treat this as a program that would run on its own, and simply adding a kill switch as opposed to forcing it to stay open?

If this was all that needed to happen, why didn't anyone just say so in the other thread?

It was 7 pages long for a simple solution like this:)?
Last edited on Jul 23, 2018 at 5:20pm
Jul 23, 2018 at 5:20pm
1
2
3
4
5
		int array[6]{ 1,2,3,4,5,6 };
		int *pa;
		pa = &array[6];

		*pa = array[6];

You're dereferencing past valid indices of your array. This is undefined behavior.
Jul 23, 2018 at 5:22pm
Okay @Ganado, I'm trying to do "list" manipulations like I learned in my Python 3 course this past uni semester in c++. How do I accomplish this?
Pages: 12