This is for a school project I'm working on, it's just a small part of the code but for some reason the program doesn't seem to go inside the if() no matter what the input is. I've tried anything and everything I know of (also used the isalpha() function) but it just won't run the commands inside the if().
do
{
flag=1;
gets(input.number);
printf("\n%s\n",input.number);
for(i=0;i<strlen(input.number);i++)
{
printf("yolo1\n"); //debug
if(input.number[i]<48 || input.number[i]>57) //PROBLEM HERE
{
printf("\nyolo2\n"); //debug
flag=-1;
break;
}
}
if(strlen(input.number)<1000 || strlen(input.number)>9999 || flag==-1) printf("\nINVALID INPUT\n\nARI8MOS: ");
}while(strlen(input.number)<1000 || strlen(input.number)>9999 || flag==-1);
Can you guys help me out here??? I've been staring and the code for the better part of 3 days now....
I presume you declared char input.number[].
Your condition in if says that you only want to get into its body if the character is NOT a digit. This is somehow contradictory to the name input.number of the variable (but perhaps you are just checking for incorrect characters here...)
To better see what is happening with the condition, you can choose to print the values of its components, like this:
printf("%c[%d]", input.number[i], input.number[i]);
printf("%d, %d, %d\n", input.number[i]<48 , input.number[i]>57, input.number[i]<48 || input.number[i]>57);
(you will se a 0 for false and 1 for true)
BTW: You could write the same condition in a more readable manner, using char constants like this:
input.number[i]<'0' || input.number[i]>'9')
Related
if((selected[0]=='H')&&(selected[1]=='Y')&&((selected[2]-'0')<=4))
{
integer_v=10+(selected[1]*100)+(selected[2]-'0');
printf("%d", integer_v);
}
else
{
while((selected[0]!='H')||(selected[1]=='Y')||((selected[2]-'0')<=4))
{
printf("Wrong input!(%s) Please try again:\n", selected);
scanf(" %s", re_selected);
strcpy(selected,re_selected);
}
integer_v=10+(selected[1]*100)+(selected[2]-'0');
printf("%d", integer_v);
}
In my project to create this simple board game I cannot get out of this while loop. The purpose of this piece of code is to check if the input I gave(selected), which is a char array, has 'H' in first, 'Y' in second, and a digit from 1 to 4 in third slots of this array. But when I do it wrong once I get stuck in the while loop(The first try is successful). Even if the requirements are fullfilled the "Wrong input" message still appears.
Example:
Choose which pawn you want to move:
HY5
Wrong input!(HY5) Please try again:
HY3
Wrong input!(HY3) Please try again:
HY4
Wrong input!(HY4) Please try again:
In here, HY5 is truly not the required input because 5>4 but HY4 is okay but the code still does not accept it.
The condition of the while statement is wrong.
It is easy to simply add ! to negate the first condition for checking.
while(!((selected[0]=='H')&&(selected[1]=='Y')&&((selected[2]-'0')<=4)))
Another way is negating the condition using De Morgan's laws:
while((selected[0]!='H')||(selected[1]!='Y')||((selected[2]-'0')>4))
I'm having trouble understanding the control flow within a segment of my code. The code is written to take a startword (i.e cat) and an endword (i.e dog) change 1 letter of the original word at a time and reach the end word - checking against a dictionary of real words. If a non-real word is reached, i.e cat --> dat, it should break out of the loop and try changing a different letter.
while (strcmp (startword, endword) != FALSE)
{
change_letter(startword, endword, i++);
if ((check_dictionary(dictionary, startword)) == FALSE)
{
printf("%s --> ", startword);
printf("Bad route\n");
break;
}
if ((check_dictionary(dictionary, startword)) == TRUE)
{
printf("%s --> ", startword);
}
}
change_letter will just do startword[i] = endword[i], where i is initialized at 0 (for the first letter, and moves through the letters as i++ appears.
check dictionary compares the word against the dictionary, if the word is found a 1 is returned. (#define TRUE = 1, #define FALSE = 0).
Right now it will print (if using cat and dog as the words)
cat --> dat --> Bad route and thats all.
To my understanding it should never leave the top while loop whilst the two words are not equal.
while (strcmp (startword, endword))
{
change_letter(startword, endword, i++);
if (!check_dictionary(dictionary, startword))
{
printf("%s --> ", startword);
printf("Bad route\n");
}
else (check_dictionary(dictionary, startword))
{
printf("%s --> ", startword);
}
}
Modified your code a bit. Removed break, as it breaks control flow from loop and control is on the next statement after loop's }. That was your issue. Not sure if it works now, though, as it depends on your check_dictionary implementation.
Additionally, comparison == TRUE is useless. You can endlessly write check_dictionary(dictionary, startword) == TRUE == TRUE == TRUE == TRUE, the meaning will be all the same. Same as in while loop. It makes your code less understandable and is generally a bad practice.
After all, I removed second function call to make the function return value being evaluated only once.
Also, try to control your code readability by keeping same coding style (bracers!). It improves your code's readability.
Sorry for being boring.
Is there a way or function available which would stop me from going over the characters. The maze looks like the following below. I actually dont use printf, but mvprint. I just used printf as an example below.
printf("xxxxxx x");
printf("xxxxxx x");
printf("xxxxxx x");
printf("x x");
printf("x xxxxxx");
printf("x xxxxxx");
I tried the this code below but it doesn't seem to work. The cursor still goes over the x characters. In the third line of the code you can see I've said that if there is a character 'f' there which is created by the bunch of printf statements seen above, the cursor shouldn't move. This doesn't seem to work.
if(m == 's')
{
if((oldy+1,x)=='x') // This is the part of the code where i say that if the next spot is an 'x' dont move.
{
mvprint(win, 10,0,"Sorry, you cant move there.");
refresh(win);
}
else
{
move((y= oldy+1),x);
refresh();
oldy = y;
}
}
After a little bit of research, I think you want your inner condition to be:
if(mvinch(oldy+1,x) == 'x')
The mvinch(y,x) function moves and returns the character at that location.
Also, as other people mentioned, mixing standard I/O and Curses is unreliable at best. When I tried something like this on my machine to test, my program told me that the entire screen was made up of spaces.
I have a work to do in which I have to keep a loop inside the function expecting the following parameters:
-"i" to insert
-"s" to search
-"q" to quit
How do I keep this loop? I've looked up some options and it seems to be possible using a while or a switch, but I am not sure which is the best way to read those chars (with a fscanf perhaps?). I am also not sure how to read the things after the parameter "i" as the input would be "i word 9", so after detecting the i to insert I have to read a string and an int.
Anyone has any idea how to do this? I am sorry is this seems simple, but I am new to programming.
edit: Here is what I have so far
while (loop) {
fscanf(stdin,"%c",&par);
if (strcmp(&par,"i")){
scanf("%s %d",palavra,p);
raiz = insere(raiz,&palavra,p);
}
else if (strcmp(&par,"b")){
scanf("%s",palavra);
busca(raiz,&palavra);
}
else if (strcmp(&par,"q"))
loop = 0;
}
edit 2: This is what I have now, I am having problems reading the string and integer when the parameter is i, somehow it crashes the function
while (1) {
c = getchar();
if (c == 'f')
break;
else if (c == 'i'){
fscanf(stdin,"%s",&palavra);
scanf("%d",&p);
raiz = insere(raiz,palavra,p);
}
else if (c == 'b') {
scanf("%s",palavra);
busca(raiz,palavra);
}
}
Thanks in advance!
The code you have doesn't look too bad compared to what I believe you want. You can replace the "while (loop)" with "while (1)" and then your exist code "loop = 0;" with "break;" which is a bit more standard way of doing things. Also "fscanf(stdin..." is the same as "scanf(..." ... scanf will read from stdin by default. You might want to check the docs for strcmp because it returns 0 for an exact match and I don't think that will do what you want in your 'if' statements. You should be able to use scanf to read in the values you want, is it giving you an error?
You are using 3 separated scans. That means you can't input this "i word 9", but input one command or parameter at the time separated by EOL(pressing enter).. i, enter, word, enter, 9, enter ... Then the function should actually get further in those "if"s. With those scans you also should consider printing information about expected inputs ("Choose action q/i/f")
And I would recommend using something to test those inputs.
if (scanf("%d", &p) == 0) {
printf("Wrong input");
break;
}
Apologize for ambiguous title but I simply did not know how else to put it.
Anyway, I want a piece of code to do the same thing this does in one line (in an if statement)
ret= fee(XYZ);
if((fii(ret) && foh(ret)) !=0)
{
//do something
}
like put all that into something like
if(_______FUM________)
{
//do the same thing
}
Is there anything I can do for this? The answer is probably staring at me in my face. But I am stuck here.
You could use the comma operator:
if(ret = fee(XYZ), (fii(ret) && foh(ret)) !=0)
But why?!
(Also, the !=0 appears to be redundant.)