What's wrong with strcmp in this program? - c

When I run the program and answer "yes", it tells me that I am wrong. Does anybody know a way to execute this type of program?
#include <string.h>
#include <stdio.h>
int strcmp(const char *str1,const char *str2 );
// I am trying to make a program that asks the user to input an answer
int main()
{
char answer[20];
printf("Can birds fly?\n");
scanf("%s", &answer[20]);
if(strcmp(answer, "yes") == 0)
{
printf("You are right");
}else
{
printf("You are worng");
}
return 0;
}

Change this line:
scanf("%s", &answer[20]);
to:
scanf("%s", answer);
You have to pass to scanf the address where you want to put the string, with answer[20] you pick the value of the 21th character in the string (undefined because the string is 20 characters only) and then take a pointer to it (garbage, you may even get an access violation).

Related

C Program Crashes in Visual Studio

I am getting this error whenever I run my code in visual Studio:
#include <stdio.h>
#include <ctype.h>
int main() {
char username[10];
printf("Enter Username: ");
scanf_s("%[^\n]", &username);
while (isupper(username)) {
if (username == '-') {
printf("Username cannot contain UpperCase Letters");
}
}
}
Error Image
I don't think you can pass whole array to isupper. Also if you don't want to return anything instead of int main() use void main() or just return 0 in the end or when you want to end after your program executed successfully. As for using scan_s or scanf or getline or whatever I won't say anything because its a different matter and your syntax of scanf_s is certainly wrong.
Also following code will not check for any buffer overflow (not a good practice, you will see even though we gave size 20 char array, this code will work even for larger input which is certainly not a good thing). So you can either limit the size of input or better to read an entire line via fgets() (or getline() if available) and parse the string yourself.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(){
char username[20];
printf("Enter Username: ");
// scanf("%[^\n]", username); <--- Instead of this
scanf_s("%20c", username, 20); // <----Try Using this
int i=0;
while (i<strlen(username)) {
if (isupper(username[i])) {
printf("Username cannot contain UpperCase Letters\n");
return 0;
}
i++;
}
return 0;
}
My first guess would be that your while is an endless loop, try to do it like this:
int i;
for(i=0; i<strlen(username);i++){
if(isupper(username[i])){
printf("Username cannot contain UpperCase Letters");
}
}

How to print the nth term of a string using scanf function?

The console will not do anything after the user has entered a string.
I have got the code to work using char string="enteraword" and taking out the whole printf and scanf function, however I need the code to work with a scanf function.
#include <stdio.h>
#include <string.h>
int main()
{
char* string;
printf("Enter a word: ");
scanf("%s", string);
char c=string[1];
printf("The second letter in %s is %c", string, c);
return 0;
}
This code has undefined behavior, you're passing an uninitialized pointer to scanf(), asking it to store a string there.
Also, remember that %s will stop at whitespace, so it's very unclear what "terms" should mean here.
Try e.g.:
char string[1024];
if(scanf("%1023s", string) == 1 && string[0] != '\0')
{
const char c = string[1];
printf("The second letter of '%s' is '%c'\n", string, c);
}

How can I verify if one string variable is equal to a specific string that is not a variable in a do while in C language?? (not compare two strings)

I have already read the C string function man pages. The best thing I found was "strcmp" and "strncmp". But that's not what I want because it compares two strings. I just want to compare one like this...
char input[3];
do {
//important code
printf ("You want to continue??);
fflush(stdin);
gets(input);
} while (input == "yes" || "Yes);
It doesn't work. I would need to not use string and just a char like: "y" for yes. Is there a way I can do what I want? How can I compare just this one string with these values?
If you want to make string comparison, use strcmp. You will compare just one variable, as you want it, but with two different values. Change line :
while (input == "yes" || "Yes");
to :
while ( strcmp(input,"yes") == 0 || strcmp(input, "Yes") == 0);
Also, change :
char input[3];
to :
char input[4];
as you need to take into account the terminating \0 character.
Here I wrote a simple solution for what you're trying to achieve.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char input[4];
do {
printf ("You want to continue??");
fflush(stdin);
gets(input);
} while (strcmp(input, "yes") == 0 || strcmp(input, "Yes") == 0);
return 0;
}
Most likely you are going run into newline character that gets carried over
to next input. Here is a solution for that.
#include <stdio.h>
#include <stdlib.h>
int
main(void)
{
char input[4], garbage[50];
do
{
//important code
printf ("You want to continue??");
scanf("%s", input);
fgets(garbage, sizeof(garbage), stdin); //Newline char is now stored into garbage.
} while (strcmp(input,"yes") == 0 || strcmp(input,"Yes") == 0 );
return 0;
}

Prompt for input and print a response with only one printf()?

C-code only: Ask user if they are married or not. User must input 0 for false. User must input any other character for true. Do it using only one printf.
Ok, so I always turn to stackoverflow as a last resort, because I am trying to figure it out. This, is what I came up with but I get errors and I have done other things like take out scanf("%f", &t), because that is essentially unnecessary. I also made char married[3]; char married[] ="; instead but that doesn't work.
Here is my code:
#include <stdio.h>
#include <string.h>
int main()
{
char married[3];
unsigned long t;
int f;
scanf("%f", &t);
scanf("%d", &f);
printf(" For the following question: Enter 0 if false. Enter anything but 0 if true. Are you married? %s", married);
if (f == 0)
{
married == "no";
}
else
married == "yes";
return 0;
}
Thanks the help is appreciated. Please go easy on me just learning...
I'm not sure you are interpreting the question correctly. It says to print whether the person is married or not. So that's the expected output. It suggests you can do that with one printf. It does not mean the whole program only has one printf so you are allowed to have another printf for the user prompt. It just means avoid using two printfs for the output (one for YES and another for NO). One way to do this is to use the ? operator.
For example:
#include <stdio.h>
#include <string.h>
int main(void)
{
int married = 1;
printf(" For the following question: Enter 0 if false. Enter anything but 0 if true. Are you married?");
scanf("%d", &married);
printf("You %s married\n", married ? "ARE" : "ARE NOT");
return 0;
}
#include <stdio.h>
#include <string.h>
int main() {
char married[4]; //Space for 'yes' + the NUL-terminator
//unsigned long t; Why do you have this?
int f = 1; //Initialize variables
//scanf("%f", &t); ??
//scanf("%d", &f); Wrong place
printf(" For the following question: Enter 0 if false. Enter anything but 0 if true. Are you married?"); //Remove %s and the argument. You are trying to print an uninitialized array
scanf("%d", &f); //scan input after printing
if (f == 0)
strcpy(married, "no");
else
strcpy(married, "yes"); //Copy strings using strcpy function
return 0;
}

CodeBlocks exe stops working

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
char string;
printf("Hello\n");
printf("What would you like to do\n");
printf("Here are the options\n");
printf("s : How are you\n");
printf("c : What would you like to search\n");
scanf("%s",&string);
if(string == 'h')
printf("iam fine\n");
else if (string == 's')
printf("What would you like to search\n");
scanf("%s",&string);
system(string);
return 0;
}
When I run this after it says what would you like to search and I type run notepad it stops working.
There are two problems with this scanf:
printf("What would you like to search\n");
scanf("%s",&string);
system(string);
string is a single char - the scanf will result in a buffer overrun.
The %s format specifier only reads until the next whitespace.
To fix the problem you should allocate a larger buffer and read an entire line:
char buffer[1024];
printf("What would you like to search\n");
fgets(buffer, sizeof buffer, stdin);
system(buffer);
Problem 1. defining string as char won't work for you. you need an array.
define char string[100] = {0};
Problem 2. scanf("%s",&string); not required, can be used as scanf("%s",string);
problem 3. if(string == 'h'), wrong. array contents cannot be compared using == operator. you've to use strcmp() function.

Resources