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))
Related
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')
I have to write an ATM program for a class, and i cant figure out how to make a function that will ask the user for a pin and if the pin is entered incorrectly three times the program will display an exit message then terminate.... this is what i have some far. I think my issue is i don't know the correct syntax to handle my issue.
I know i will need a for loop but not sure how exactly to construct it.
void validate_acc(){
int user_acc_try;
printf("Please enter your account number: ");
scanf("%d", &user_acc_try);
if(user_acc_try != account_number){
printf("You entered the wrong account number");
}
else{
printf("");
}
}
void validate_pin(){
int user_pin_try;
printf("Please enter your pin number: ");
scanf("%d", &user_pin_try);
if(user_pin_try != pin){
printf("You entered the wrong pin number.");
}
else{
printf("");
}
}
void validate(){
validate_acc();
validate_pin();
}
Secondly, since i can only post every 90 minutes might as well ask another question, I do not know how to make a function go back to the beginning of my program like for example say after an deposit, what is the logic i would need to use to have a function go back to the beginning of my main function. I know of goto labels, that didnt seem to work when i put it in front of my main function like so...
MAIN:
int main()
i would put goto main; in another function and i would get a.... Main is not defined error. I have read a few different questions on here about labels but cant find anything that helps, if someone could guide me in the right direction, you would be giving me a great deal of relief.
thank you in advance.
It's a good idea to write out a flow chart for things like this if you can't figure out how to do it in code.
Please do not use labels/goto in C. It's a nasty habit and it's not needed.
You know how to use if statements to make a decision; think about how you would use a while loop to try to make the same decision over and over again until something changes. For instance, in pseudo-code (because I don't want to do your work for you)
user_has_not_entered_correct_pin = true
retries_left = 3
while retries_left > 0 and user_has_not_entered_correct_pin:
get pin
if pin_is_not_correct(pin) retries = retries - 1
else user_has_not_entered_correct_pin = false
end while
I am limited on time right now, so I will just post a quick help. I would suggest start researching loops in C. Since this is for a class, the book you are using should have information in it about for loops and while loops, but if not, a simple Google search can help a lot.
With a quick search on Google, this site seemed like a decent site for basic information on loops:
Loops in C
It has links and examples of using a for loop, a while loop, a do...while loop and nested loops which should help you solve your problem.
Edited to add:
In your post you mentioned that you think the problem is that you don't know the syntax that you need. It is for that reason that I pointed you to a location that can help you with the syntax that you need to solve your problem rather than show you directly how to solve the problem. I hope that this helps you not only with this question, but going forward in your class as well.
Keep a count variable like I have did below and check the number of attempts:
I don't see a need for goto here. The same logic can be used for checking pin also.
int i=0;
while(1)
{
if(i>2)
{
printf("Maximum attempts reached\n");
break;
}
printf("Enter the acc_num\n");
scanf("%d", &user_acc_try);
if(acc_num == saved_acc_num)
{
// Do your stuff
}
i++;
}
Return value from validate_pin() int validate_pin(){... return 0; .... return 1;} and test it in the main() or your validate().
int i=0;
int result=0;
while ( (result==0)&&(i<3) ){
result=validate_pin();
i++;
}
Dont use goto, learn to use loops.
My project is to create a Calculator, which includes some rounding functions like:
Normal rounding, Floor, and ceiling.
They appear in the program like this:
round(x)
ceil(x)
floor(x)
Here's the code of that menu:
else if (user_input == 3){ /*Rounding Operations*/
instructions_Rounding();
scanf("%d",user_input);
while(user_input!=4){
if(user_input==1){ /*For round(x)*/
printf("\nEnter a number to round it: ");
scanf("%lf",&num1);
double rnd;
rnd = num1+0.5;
result = floor(rnd);
printf("\nResult is %f", result);
user_input=4;
else if(user_input==2){ /*For ceil(x)*/
printf("\nEnter a number: ");
scanf("%lf",&num1);
result = ceil(num1);
printf("\nResult is %f", result);
user_input=4;
}
else if(user_input==3){ /*For floor(x)*/
printf("\nEnter a number: ");
scanf("%lf",&num1);
result = floor(num1);
printf("\nResult is %f",result);
user_input=4;
}
}
}
Note: This loop can be ended by setting user_input to 4, but this loop is inside a bigger loop that can be ended by setting it to 5. I don't know how this might help but I thought it might be the cause of something and it might be worth mentioning.
Anyway, the prorgam runs flawlessly except when I choose anything from this menu, it gives me an error like this:
http://oi44.tinypic.com/2gwdssy.jpg
I really don't see anything wrong with my code. It used to work good before but only when I had a problem with the menus, needed a while to figure how to return to the main menu after each operation instead of getting stuck in an infinite loop. But only when I fixed it did this happen. Not sure why.
Can someone tell me if anything is wrong with this code? Is it normal for it to act this way?
Oh, and I tested it on 3 different computers so it's not a problem from my computer.
This line:
scanf("%d",user_input);
Should be:
scanf("%d",&user_input);
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;
}
i was practicing my C Prog Language
and i decided to create a salon with cashier features
it looks messy,
though i'm still learning
posted here: http://pastebin.com/B2XaaCYV
it say runtime error with variable "menu", but i tried to recheck it around 5x and i don't see any error with it.
the code is really simple
like xy[0][1] = default 0 = meaning not yet purchased. its value will be 0/1 only. it will be 1 when you actually purchase it after picking the hairstyle.
then of course
xy[1][i] means price of xy[0][i]
i tried using other techniques like removing of breaks and changing variable name, but still it says runtime error with variable menu
no idea what makes the error. so i wish someone can help me with this
scanf("%1s",&menu);
No! A char isn't a string at all. You want to get a single character, so use either getchar() or scanf("%c",&menu);.
A related error is in your core_return, where you try to read 3 characters into a single character. Also, don't call your main in a sub-routine. Instead return from the sub-routine and put a loop in your main. By the way, 'yes' and 'no' aren't valid. If you want to compare strings, you have to use strcmp:
// returns 1 if the user wants to go again
int another_menu(void)
{
char tmp[20];
printf("Do you want another service?");
for(;;){
scanf("%3s",tmp);
if(strcmp("y",tmp) || strcmp("yes",tmp))
return 0;
else if(strcmp("n",tmp) || strcmp("no",tmp))
return 1;
printf("Please specify either 'no' or 'yes': ");
}
}
Use compiler warnings in order to find your errors quicker (GCC: -Wall -Wextra).