Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I've made this joke program with string compare, but when I type "yes" it also types what I have for the else statement. It doesn't do this when I type "no", and responds predictably if I type something other than "yes" or "no".
#include<stdio.h>
#include<string.h>
int main ()
{
char ansr[50];
printf("Are you a cop?");
scanf("%s", &ansr);
if (strcmp(ansr, "yes") == 0)
{
printf("Then get outta here buddy!");
}
if (strcmp(ansr, "no") == 0)
{
printf("Then you can learn the secret handshake!");
}
else
printf("\nDude! Yes or no question! Are you a cop?!\n");
}
There are a few things to notice. First, you never use scanf to read anything into ansr before using it, which leads to undefined behavior. Second, as others have said, your if/else statements are not congruent. Third, you need a while loop to ask the question again if the answer is invalid. You could rearrange it like so:
int main ()
{
char ansr[50];
printf("Are you a cop?\n");
scanf("%49s", ansr);
while (strcmp(ansr, "yes") && strcmp(ansr, "no"))
{
printf("\nDude! Yes or no question! Are you a cop?!\n");
scanf("%49s", ansr);
}
if (strcmp(ansr, "yes") == 0)
{
printf("Then get outta here buddy!\n");
}
else
{
printf("Then you can learn the secret handshake!\n");
}
return 0;
}
ansrstays uninitialised, before touching it via strcmp(), the latter invokes the infamous Undefined Behavior, anything can happen.
Your code has a if block and an if else block. So it compares ansr twice, change it to if else if block, it will only check once.
#include<stdio.h>
#include<string.h>
int main ()
{
char ansr[50];
printf("Are you a cop?");
//should take input here
if (strcmp(ansr, "yes") == 0)
{
printf("Then get outta here buddy!");
}
else if (strcmp(ansr, "no") == 0)
{
printf("Then you can learn the secret handshake!");
}
else printf("\nDude! Yes or no question! Are you a cop?!\n");
return 0;
}
Edit : Removed some words as suggested. Sorry, included them from op code.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 months ago.
Improve this question
My school teacher teaches if...else if today. She teaches us to write the if...else if like this
if(x == 1) {
//some code here
}
else if(x == 2) {
//some code here
}
else if(x == 3) {
//some code here
}
else if (x == 4) {
//some code here
}
But, from what I learn from Internet is mostly write like this
if(x == 1) {
//some code here
}
else if(x == 2) {
//some code here
}
else if(x == 3) {
//some code here
}
else if (x == 4) {
//some code here
}
So, my question is which should I follow? Internet or my teacher ? I never seen anybody write if...else if like my teacher before.
p.s. I know these two will give same result when running it. Just confusing how should I arrange the if...else if statement.
I normally don't like style debates, but your teacher chose a really strange style indeed.
You are correct, almost everybody writes else if like in your second example, even when the local standard calls for never using single statement blocks with curly braces omitted, as though else if were a keyword with a space in it.
I have seen people ban it, but everybody else who does so always ends up with
else {
if () {
}
}
or a variant of it where one or more opening braces go on their own lines.
We should remember that else if ladders get very long indeed and a style that requires indenting them becomes intolerable after while. Most languages that have more rigid whitespace rules (I'm looking at you Visual Basic) end up with an elseif keyword to avoid this problem.
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 try to make a C console program ask user enter Y (means yes) or N (means no) by using if and while,the user only can enter Y or N,otherwise the program will tell the user input error,no matter they input what the characters, how many strings.The fuction just like terminal ask user “Are you sure?Input Y or N".
But my program will give me more than one error feedback when i test.I relly want it just give me one feedback.Hope you can help me perfect my progrem.
Here are my code:
#include <stdio.h>
int main(void)
{
char yon = '\0';
do
{
if (yon != '\0')
printf("\nSorry,Please try again.\n");
printf("\nEnter Y/N?:");
}
while ((yon = getchar()) != 'y'&&yon != 'Y'&&yon != 'n'&&yon != 'N');
return 0;
}
My English is not good,If you do not know what i mean,Please tell me to edit.
Thanks.
I just now edit my code,it runs very nice without bugs.
Here are my code:
#include <stdio.h>
int main(void)
{
char yon = '\0';
do
{
fflush(stdin);
if (yon != '\0')
printf("\nSorry,Please try again.\n");
printf("\nEnter Y/N?:");
scanf("%1s",&yon);
}
while ( yon!= 'y'&&yon != 'Y'&&yon != 'n'&&yon != 'N');
return 0;
}
Thanks.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
#include<stdio.h>
#include<string.h>
char Sys_Pass[]="3699";
char verify_Pass(char *P,char *Q)
{
char ch;
ch=strcmp(P,Q);
printf("String Cmp %s,%s is %c %d\n",P,Q,ch,ch);
if(ch==0)
return 1;
else
return 0;
}
void main()
{
char Pass[10],New[10];
char fp=0,sp=0;
printf("Enter Password : ");
scanf("%s",Pass);
if(fp=verify_Pass(Pass,Sys_Pass))
{
Change: printf("Enter a New Password : ");
scanf("%s",Pass);
printf("Re-Type New Password : ");
scanf("%s",New);
if(sp=verify_Pass(Pass,New))
{
strcpy(Sys_Pass,New);
printf("Password Successfully changed\n");
printf("New Password : %s\n",Sys_Pass);
}
else
printf("Passwords Mismatch\n");
}
else
{
if(strcmp(Pass,"111999")==0);
goto Change;
printf("Wrong Password !!!\n");
}
printf("Fp : %c %d\nSp : %c %d\n",fp,fp,sp,sp);
}
This code is always executing the if block and provides the password change even though wrong current password is typed I want to know if there is a logical error if any ?
I am working on a 8051 project which i want to establish a secret password change i.e if i forgot a password and i type password as 111999 it should direct me to the password change menu but here it is always directing to change password menu. This was actually embedded C code but I tried to rectify that using C code but produces same output in both the cases.
You have a stray semicolon:
// here ---------------v
if(strcmp(Pass,"111999")==0);
goto Change;
As an aside, this is not a good use of goto. The better thing to do is check for either the current password or 111999 in the if condition.
printf("Enter Password : ");
scanf("%s",Pass);
if ((strcmp(Pass,"111999")==0) || (strcmp(Pass,Sys_Pass)==0))
{
printf("Enter a New Password : ");
...
}
else
{
printf("Wrong Password !!!\n");
}
There are several things in this code that have margin for improvement, but I will start by asking if you have used a debugger to debug the issue. If you haven't, you can check gdb: video here
I think also you should consider changing your main so it returns an int, except if you have a very good reason not to do it. Then you could have return codes. Same applies for the input arguments to main. If it receives none, I would strongly encourage you to specify it by typingint main(void). See here for more info
Personally I always add braces around every if/else block, even when it is one line. It helps avoid mistakes.
I dont have anything against the goto statement if it is used properly, but I believe in this case it is not. The Clean Code book by Robert Martin contains some good explanations on how to use it. I think you can try to rewrite this and avoid goto usage.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
hey im lost on why this loop doesnt work it all seems right but nothing inside the while works please help the rest of the code is in other files if you need them i can post them
#include <stdio.h>
#include "weatherstation.h"
int dunits = METRIC;
void main(void)
{
char test;
InitializeWeatherStation();
while(1);
{
UpdateWeatherStation();
printf("Enter m for Metric units, b for British units, or q to quit");
scanf_s("%c",&test);
if(test == 'm')
{
dunits = METRIC;
}
else if(test == 'b')
{
dunits = BRITISH;
}
else if(test == 'q')
{
return;
}
DisplayWeatherData(dunits);
}
}
while(1);
{
something;
}
is exactly the same as:
while(1)
{
}
{
something;
}
In other words, what you have there is an infinite loop followed by a scoped block of code (which will never be reached).
Get rid of the semicolon and it should fix that particular problem.
You must not end the while(1) with a semi-colon dude. Because that's a null statement you wrote in there.
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
so in this function of mine, it is spitting out this warning: "carboard.c:79:1: warning: control reaches end of non-void function [-Wreturn-type]". Program runs fine, but i just want it to compile cleanly.
here is the code of the function:
int loadMainMenu()
{
char choice[LINE + EXTRA_SPACES];
Boolean menu = TRUE;
printf("\nWelcome to Car Board\n");
printf("--------------------\n");
printf("1. Play Game\n");
printf("2. Show Student's Information\n");
printf("3. Quit\n");
printf("\n");
printf("Please Enter Your Choice:\n");
do
{
int input;
fgets(choice, LINE + EXTRA_SPACES, stdin);
if (choice[strlen(choice) - 1] != '\n')
{
printf("BUFFER OVERFLOW!\n\n");
readRestOfLine();
}
choice[strlen(choice) - 1] = 0;
input = atoi(choice);
switch(input)
{
case 1: playGame();
break;
case 2: showStudentInformation();
loadMainMenu();
break;
case 3:
printf("Bye bye! \n");
return EXIT_SUCCESS;
break;
default: printf("Invalid input\n");
loadMainMenu();
break;
}
}while(menu);
}
You need to have a return statement in your code. Since your function is returning an integer you must have something likek return x; where x is an integer or you can declare function void.
The compiler is not that smart to figure out the do ... while never terminates.
To fix this, first ensure the loop cannot end by making it guaranted infinite using a constant:
do {
} while ( true );
If that doesn't work, try
while ( true ) {
...
}
The compiler might require a specific pattern ( for ( ; ; ) might also be worth a try; it is also a matter of personal preference).
If that still does not work, just return 0; after the loop above. That might be optimised away, so no extra code at best. At worst, you have to live with some dangling code.
An alternative would be to temporarily disable the warning using a pragma. That should really only be the last ressort.