Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Hello everyone as the title says as I run through my code everything works fine, it loops correctly but somehow ends up always picking up the default case before restarting the loop.
I'll post my code through pastebin since it's quite long:
https://pastebin.com/TxvH3MhJ
while(myScore != "new" || myScore != "NEW" || myScore != "New"){
switch(s2){
case 1 :
switch(s3){
// code
}
default :
cout<<("\nSomething went wrong, please restart the program.");
break; //this statement works correctly.
}
default : //this statement is always brought up each time i type "new", i also added a cout s2 to check if actually somehow the variable was changing mid code but it prints out correctly.
cout<<("\nSomething went wrong please restart the program.");
cout<<s2;
break;`
p.s. I'm a beginner please bear in mind that probably the code is very rudimental although it is doing the job. at the moment the code just works if you enter 1s because i'm just working out the logic.
break; breaks out of the innermost switch (or loop) only. So you need to place it after inner switches too (unless you would need fall-through), like:
switch(s1) {
case 1:
switch(s2) {
...
}
break; // <== HERE
case 2:
...
break;
default:
....
break; // optional
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
char restart = 'y';
while (restart == 'y')
{
cin >> restart;
cout << "great keep playing\n";
}
cout << "thanks for playing\n";
When I run this code it also displays the code outside of the loop as well, I even tried using a break statement but it didn`t work. How do I fix this?
You could write the loop like this:
while (cin >> restart && restart == 'y')
{
cout << "great keep playing\n";
}
The main issue I see is that you get the value of restart on the 'cin' line, then display 'great keep playing' on the next line, without first checking to see what the value of 'restart' is. You need an if() statement around the 'great keep playing' line, so it will only be displayed if you entered 'y'.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have some knowledge about C++, but I stumbled upon an problem. I want the user to enter some text, but when it takes longer than X seconds, the program will go on. I guess, it is not possible, but maybe you know something about it.
It will be in the command line, without GUI.
I am not certain how the programm will look like, but it will be something like a CMD-RPG. I wanted to use Quick Time Events to make it a little bit more exciting.
I cant comment so I will just leave this here
Input with a timeout in C++
Since I cannot comment, I will simply leave this as an answer.
One possible way to solve this problem is to have 2 threads:
Input capture thread: since receiving input is a thread-blocking action, you should have a thread that simply polls for user input, placing that input into a thread-safe buffer
Quick-time function on main thread: a function that will be responsible for executing the quick-time event. Something like the following could be done (pseudo code):
clear input buffer //the buffer provided by the input capture thread
bool success = false;
while(current time < ending time)
{
if(input buffer isn't empty)
{
get contents of input buffer and send contents to cout
if (user has finished input correctly)
{
success = true;
break;
}
clear buffer
}
}
return success;
For this to work, you would need to turn off echo on the command prompt (see this page for more info)
Although Diogo's reference is excellent (and informative), I believe this answer is more applicable than since the OP has indicated that this program will be ran on Windows.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a very strange error. I am writing a program in Cuda that emulates the Conway Game of Life. I transfered the 2D array to device and there is a if-case that check for the state's thread.
if(iam==-1)
{ //i am on
iam=0;
}
else if(iam==1)
{ //i am dying
iam=-1;
}
else //i am off
{
if(counter_alive==2)//two neighboors alive
{
iam=1; //i will be on
}
// iam = -999;
}
When the last line is in comment nothing works and the var "iam" has the first value. But if i drop the //, it will work. Of course, if the flow's code execute the else, the var "iam" will take the value -999.
Any ideas? Have i missed something?
Thanks in advance!
You do not show the initial value of the variable iam or counter_alive. Let us assume that the compiler has set it to 0. Of course even if the compiler just sets the space to a random value, this analysis would be the same.
if(iam==-1)
{ //i am on
iam=0;
}
Since the initial value is 0, then this fails and drops through
else if(iam==1)
{ //i am dying
iam=-1;
}
Again, since the initial value is 0, then it fails and drops through.
else //i am off
{
if(counter_alive==2)//two neighboors alive
{
iam=1; //i will be on
}
// iam = -999;
}
It enters here with a value of 0. However, since counter_alive has never been set, it is also 0 and the if fails.
Thus, the iam variable is never changed from 0. Note that since neither of the critical values changes, iam will never be reset from 0. If you uncomment the last line, it will always be explicitly set to -999 and will never change either. That is because you never test for 0 or 999. If you had it as -1 it would change to 0 and then never change unless you change counter_alive somewhere else to be 2.
Note that Explanation of CUDA C and C++ explains how the looping is handled as parallel processing. In that case, the reaction of the various items in the array may not be what you would expect in plain C (single stream) processing.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I need to delete some characters from string. When i used erase it doesn`t work, compilation error erase no member named. Please help me. Probaly it is because I writing for Arduino UNO.
Arduino String class is quite different from std::string. For example erase doesn't exists. But there is method remove.
Anyway, you should start with: https://www.arduino.cc/en/Reference/HomePage
Expanding on what KIIV suggested, you could possibly do something like this:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
String words = "This is a sentence."; //reassign same string at the start of loop.
delay(1000);
Serial.println(words);
char c;
char no = ' '; //character I want removed.
for (int i=0; i<words.length()-1;++i){
c = words.charAt(i);
if(c==no){
words.remove(i, 1);
}
}
Serial.println(words);
delay(5000);//5 second delay, for demo purposes.
}
The Libraries in Arduino are customized in order to factor the memory constraints of the target Micro controller. For example , the Uno runs on a mega328P Atmel (now Microchip) device, which has only has 32 KB flash memory.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have a problem with programming movement using C++ and Ncurses.
I'm programming a pacman and the main problem is, that I just want last pressed key in time period.
When I use usleep, it saves every key pressed during sleeping and then it's working with that in the order.
Thanks for ideas.
What did you try?
Something like this should work, if I understood correctly what you try to achieve:
int t = your_delay;
while (t --> 0)
{
sleep(1);
c = getch();
}
If you really need to time it down to the microsecond, this might not be the best approach, but if you can have some tolerance, this should be enough.
Are you in no-delay mode? If so, this might work:
usleep(your_delay);
last_key = ERR;
while ( (key=getch()) != ERR ) {
last_key = key;
}
// "last_key" now holds most recent key, if there was one, else ERR