Look at this ...
https://www.youtube.com/watch?v=b7PD5969hho
( it's a helium ion :P( strangely enough) I need suggestions about how to incorporate 2 electrons without making the code hyper long)
I want the dots to be removed as well.
http://www.2shared.com/document/RbqXfIdH/Helium_ion_XD.html (this is my c++ code)
The code below also has a problem... I'm having problems with switch statement
#include<iostream>
#include<cmath>
#include<string>
#include<iomanip>
using namespace std;
#include <stdlib.h>
int main ()
{
int i=1,t=0,live=3;
char choice=' ';
char b1='a';
char b2='b';
char b3='c';
char b4='d';
char b5='e';
char b6='f';
char b7='g';
char b8='h';
char b9='i';
char b10='j';
clear:
switch (choice)
case 'a':
b1='*';
live--;
break;
case 'b':
b2='-';
t++;
break;
case 'c':
b3='*';
live--;
break;
case 'd':
b4='-';t++;
break;
case 'e':
b5='-';t++;
break;
case 'f':
b6='-';t++;
break;
case 'g':
b7='-';t++;
break;
case 'h':
b8='*';
live--;
break;
case 'i':
b9='-';t++;
break;
case 'j':
b10='-';t++;
break;
///////////////////////////BOX////////…
cout<<setw(20)<<'|'<<setw(10)<<'|'<<se…
cout<<setw(15)<<b1<<setw(5)<<'|'<<setw(5…
cout<<setw(20)<<'|'<<setw(10)<<'|'<<setw…
cout<<setw(20)<<'|'<<setw(10)<<'|'<<setw…
cout<<setw(60)<<"---------------------…
//////////////////////////////////////…
cout<<setw(20)<<'|'<<setw(10)<<'|'<<se…
cout<<setw(15)<<b6<<setw(5)<<'|'<<setw(5…
cout<<setw(20)<<'|'<<setw(10)<<'|'<<setw…
cout<<setw(20)<<'|'<<setw(10)<<'|'<<setw…
cout<<setw(60)<<"---------------------…
/////////////////////////////////box end/////////////////////////////////////…
//////////////start////////////////
cout<<setw(40)<<"THIS GRID CONTAINS 8 BOMBS...CHOOSE 15 NUMBERS WITHOUT hittng a landmine";
{
cout<<"YOUR "<<live <<"LIVES REMAIN"<<endl;
cout<<"ENTER YOUR"<< i << "NUMBER :";
i++;
cin>>choice;
if(t==15)
{
cout<<"YOU WIN";
goto end;
};
if(live==0)
cout<<"YOU LOSE";
goto clear;};
if(live==0)
cout<<"YOU LOSE";
end:
return 0;
}
My first impressions from the video you supplied is that you need some form of simple graphics library to output what you're trying to do. Outputting "pseudo-graphics" within a console is just going to get increasingly messy and confusing to manage. Look into SDL, for instance.
To address the second section of your question, for one, your switch statement needs curly brackets to encapsulate its body. Indentation and formatting practices will help you identify these issues quickly and independently, so it would be a good idea to get into a habit of using these.
As a side note, I'd suggest you avoid using goto statements in your code, they quickly become unmanageable when a project grows. Maybe it would be a good idea to revise your C/C++.
Related
I am trying to follow a snake tutorial game using C++. However, it seems they were using some windows libraries and in my case I am compiling it on Linux. The problem is in my Input() method, I tried changing it with a recommended code I found on here, but that did not quite worked for me. Is there a way to go around this or any recommendation? Thanks.
#include <iostream>
#include <stdio.h>
#include <ncurses.h>
#include <unistd.h>
.....
//Get the key input inorder to move the snake around
void Input() {
//Tried this from the stackoverlow recommendations, did not work for my situation
if (getch() == '\033') {
getch();
switch(getch()) { // the real value
case 'A':
// code for arrow up
direction = UP;
break;
case 'B':
// code for arrow down
direction = DOWN;
break;
case 'C':
// code for arrow right
direction = RIGHT;
break;
case 'D':
// code for arrow left
direction = LEFT;
break;
case 'Q':
gameOver = true;
break;
default:
break;
}
}
}
.....
The issue I am having with is that linux does not accept the kbhit() which what the snake game tutorial was using, so I tried to modify it with what I have above, however it does not move the snake.
This case 'A': expects capital letter to be typed. Which is not the case :) normally. So:
switch(getch()) { // the real value
case 'A': // either capital
case 'a': // or low case
// code for arrow up
direction = UP;
break;
I have a program written in c++ with opencv which should calculate a disparity map. Now I want to play around with the values wihin a while loop looking like this:
while(true) {
cv::StereoSGBM disparity(minDISP,numDISP,SADWindowSize, disparitySmoothness1, disparitySmoothness2);
cv::Mat disparityMap, disparityNorm;
disparity(undistortedLeft[1], undistortedRight[1], disparityMap);
disparityMap*=(1/16.0);
cv::normalize(disparityMap, disparityNorm, 0,255, cv::NORM_MINMAX, CV_8U);
cv::imshow("disparityMap", disparityNorm);
key = cv::waitKey(0);
switch(char(key)) {
case 'n': numDISP+=16;
case 'm': minDISP+=1;
case 's': SADWindowSize+=2;
case 'q': break;
}
}
key is declared as char key;
the problem I have now is the following.
If I press n everything is working well, if I press m the program increases n and s as well. The same if I press s. If I press q nothing is happening.
I also tried it using if else statements and it was the same result.
Is this because of key saves the buttons pressed before in any way?
Maybe someone of you can help me with this.
Maybe there is a trouble with the syntax of switch-case. Did you leave intentionally break statement in the end of each case?
switch(char(key))
{
case 'n':
numDISP+=16;
break;
case 'm':
minDISP+=1;
break;
case 's':
SADWindowSize+=2;
break;
case 'q':
// do nothing
break;
}
to work on all systems you have not only use break statement but also to do this:
int key;
key = waitKey() & 255;
Note that I am not using waitKey(0) since it gives 0 ms to draw your image. In practice waitKey will take time to draw it still.
This question already has answers here:
Default case in a switch condition
(3 answers)
Closed 8 years ago.
I came up with this program in some other site and thought of trying it, here's the program:
#include <stdio.h>
int main()
{
int a=10;
switch(a)
{
case '1': printf("one");
break;
case '2': printf("two");
break;
defau4t: printf("none");
}
return 0;
}
Suprisingly enough, this compiles without errors or warnings. How is this possible? Isn't there an error on keyword "default"?
Could anyone explain this behaviour?
The token is not considered to be a keyword at all. This is a goto jump mark named "defau4t" pointing at otherwise dead code (after the break; of case '2':)...
Try this for laughs (and an endless loop):
switch(a)
{
case '1': printf("one");
break;
case '2': printf("two");
break;
defau4t: printf("none");
default: goto defau4t;
}
One flaw with the switch statement is that you can wildly jump in and out of them using goto. At any point inside the switch (or outside it for that matter), you can place a label, that you can jump to with goto. Of course, that is very bad practice as it leads to spaghetti code.
So defau4t: is merely a label, and labels can be placed pretty much anywhere inside function bodies.
Apart from that typo of default.
i think you need to also update
case '1': printf("one");
break;
case '2': printf("two");
break;
to
case 1: printf("one");
break;
case 2: printf("two");
break;
I used getchar() to stop the while string. My problem is that it does stop the while string if I type one or two characters, if the user input is more the two characters nothing happens.
here is the code:
printf("enter srting\n");
while ((tmp=getchar()) !='\n') { \\here is my problem
count_letters++;
/* COUNTING WORD THAT START WITH LETTES L,A,C,H */
while (count_letters%3==0) {
switch (tmp) {
case 'A': count_a++;
break;
case 'C': count_c++;
break;
case 'H': count_h++;
break;
case 'L': count_l++;
default:
break;
}
} /* end of count letters while */
n1=n2;
n2=n3;
n3=tmp;
if (n1=='H' && n2=='Y' && n3=='A') {
count_hya++;
}
} /* end of getchar while */
printf("\n");
printf("%d", count_letters);
I believe it's your code :
"while (count_letters%3==0))"
is something wrong!
because when you type more than three letters,your variable "count_letters" will add to 3 due to the code:"count_letters++;".
May be you could change the "while" to "if" and see if it's working correctly~
Here's the problem:
while (count_letters%3==0) {
switch (tmp) {
case 'A': count_a++;
break;
case 'C': count_c++;
break;
case 'H': count_h++;
break;
case 'L': count_l++;
default:
break;
}
}
when your the third character is read, your program enters a infinite loop because of (count_letters%3==0)
I can't understand the purpose of your code.
I suggest using gets() and string manipulation functions because you are processing an entire line. (see string.h)
getchar() is also less efficient compared with gets() + for-loop
I'm creating a console app and using a switch statement to create a simple menu system. User input is in the form of a single character that displays on-screen as a capital letter. However, I do want the program to accept both lower- and upper-case characters.
I understand that switch statements are used to compare against constants, but is it possible to do something like the following?
switch(menuChoice) {
case ('q' || 'Q'):
//Some code
break;
case ('s' || 'S'):
//More code
break;
default:
break;
}
If this isn't possible, is there a workaround? I really don't want to repeat code.
This way:
switch(menuChoice) {
case 'q':
case 'Q':
//Some code
break;
case 's':
case 'S':
//More code
break;
default:
}
More on that topic:
http://en.wikipedia.org/wiki/Switch_statement#C.2C_C.2B.2B.2C_Java.2C_PHP.2C_ActionScript.2C_JavaScript
The generally accepted syntax for this is:
switch(menuChoice) {
case 'q':
case 'Q':
//Some code
break;
case 's':
case 'S':
//More code
break;
default:
break;
}
i.e.: Due the lack of a break, program execution cascades into the next block. This is often referred to as "fall through".
That said, you could of course simply normalise the case of the 'menuChoice' variable in this instance via toupper/tolower.
'q' || 'Q' results in bool type result (true) which is promoted to integral type used in switch condition (char) - giving the value 1. If compiler allowed same value (1) to be used in multiple labels, during execution of switch statement menuChoice would be compared to value of 1 in each case. If menuChoice had value 1 then code under the first case label would have been executed.
Therefore suggested answers here use character constant (which is of type char) as integral value in each case label.
Just use tolower(), here's my man:
SYNOPSIS
#include ctype.h
int toupper(int c);
int tolower(int c);
DESCRIPTION
toupper() converts the letter c to upper case, if possible.
tolower() converts the letter c to lower case, if possible.
If c is not an unsigned char value, or EOF, the behavior of these
functions is undefined.
RETURN VALUE
The value returned is that of the converted letter, or c if the
conversion was not possible.
So in your example you can switch() with:
switch(tolower(menuChoice)) {
case('q'):
// ...
break;
case('s'):
// ...
break;
}
Of course you can use both toupper() and tolower(), with capital and non-capital letters.
You could (and for reasons of redability, should) before entering switch statement use tolower fnc on your var.
switch (toupper(choice))
{
case 'Q':...
}
...or tolower.
if you do
case('s' || 'S'):
// some code
default:
// some code
both s and S will be ignored and the default code will run whenever you input these characters. So you could decide to use
case 's':
case 'S':
// some code
or
switch(toupper(choice){
case 'S':
// some code.
toupper will need you to include ctype.h.