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 7 years ago.
Improve this question
while i am running the above title is appearing as error
#include <stdio.h>
int main()
{
int i;
char name[20];
printf("Enter name: ");
scanf("%s",name);
printf("Your name is %s",name);
while(name[i]!="\0")
{
i++;
if(name[i]==" ")
{
strcpy(b[i],name[i]);
printf("copied name: ");
scanf("%s",b[i]);
}
}
}
while i am running this it is showing this error why? warning: comparison between pointer and integer.
"\0" is a string, '\0' is a character. As you comparing a character, you need the latter.
Also, as pointed out by chqrlie, there are many other issues - you need to check your compiler warnings/errors and fix them all. For example,
name[i]==" " is wrong with the same reason.
where is b declared?
where is i initialized??
There are many errors/ warnings in your code.
It should be '\0'. Not "\0";
You have not declared b[];
Initialize i=0;
Instead of strcpy, you can use another method which is shown in below code.
Also, you should not use scanf in last line. You should use printf. scanf is used to take input from user. printf is used to print the results.
Code-
#include <stdio.h>
int main()
{
int i=0,n;
char name[20],b[20];
printf("Enter name: ");
scanf("%s",name);
printf("Your name is %s\n",name);
while(name[i]!='\0')
{
b[i]=name[i]; // to copy from name[] to b[]. Instead of strcpy
i++;
}
printf("copied name: ");
for(n=0;n<=i;n++)
{
printf("%c",b[n]); // to show the copied result.
}
printf("\n");
return 0;
}
Related
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 3 years ago.
Improve this question
I'm making a simple code in C programming. To Be Honest with you guys I have quite a while that I don't program in it. So I wanted to make a simple program to reintroduce myself to this programming language.
This is the code:
#include <stdio.h>
#include <string.h>
int main()
{
char email;
char temppass[64];
char pass[] = "password";
printf("Enter your email: \n");
scanf("%s", &email);
printf("Enter your password: \n");
scanf("%s" , &temppass);
if(strcmp(temppass, pass) == 0){
printf("This is the password");
}
else{
printf("You Failed!");
}
return 0;
}
Though I'm having some problems which I can't solve. The first problem is that it is giving me an warning:
strcmp2.c: In function ‘main’:
strcmp2.c:14:13: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[64]’ [-Wformat=]
scanf("%s" , &temppass);
~^ ~~~~~~~~~
I'm pretty aware that this question might look duplicate as other questions but as far as I have searched and read all questions and answers, none of them could help me since they were different and didn't helped me.
At first, I tried to solve it with fgets() as an input function. When compiling the code, it didn't gave me nor warnings or errors but the problem with it is that fgets puts a \n new line character at the end of input. So the user input and the string I'm comparing are not the same. I tried adding the new line character at char pass[]="password\n" to see if it solves anything, but it failed again.
Though, when I run it and inputs the requested info, the strcmp() function will not successfully compare the two strings, even if the password input is the same.
If anyone would help me, I really appreciate you and your precious time. Many Thanks In Advance :)
The reason that your password test isn't working is related to the warning. When temppass is declared as an array, it is really a pointer to statically allocated memory. So when you take the address of it, you are no longer pointing to the beginning of the array you are pointing to a variable who's value points to the beginning of the array. The situation is similar to this code:
char *c = malloc(42);
scanf("%s", &c);
As you can see, we are giving scanf a char ** not a char *.
As others have noted, the email variable should probably be an array as well, and the call to scanf should not take the address of email at that point either.
Additionally, it is best to explicitly initialize your variables and bound your input. For the former, if you are compiling without optimizations then the compiler is probably zeroing out your memory already, but when you get into production code uninitialized variables are dangerous. Similarly, since the input is being placed into statically allocated arrays scanf should be told to limit the number of characters it copies.
#include <stdio.h>
#include <string.h>
int main()
{
char email[64] = { 0 };
char temppass[64] = { 0 };
char pass[] = "password";
printf("Enter your email: \n");
int scanf_return_value = scanf("%63s", email);
if (scanf_return_value != 1) {
printf("Error parsing input!\n");
if (scanf_return_value == EOF) {
perror("scanf");
} else {
printf("scanf returned unexpected value %d", scanf_return_value);
}
}
printf("Enter your password: \n");
scanf_return_value = scanf("%63s" , temppass);
if (scanf_return_value != 1) {
printf("Error parsing input!\n");
if (scanf_return_value == EOF) {
perror("scanf");
} else {
printf("scanf returned unexpected value %d", scanf_return_value);
}
}
if(strcmp(temppass, pass) == 0) {
printf("This is the password");
}
else {
printf("You Failed!");
}
return 0;
}
email can only store one character, not a string. And you don't need the & in the scanf when you give the address of the array (temppass is &temppass[0]).
try this instead:
char email[64];
char temppass[64];
char *pass = "password";
printf("Enter your email: \n");
scanf("%s", email);
printf("Enter your password: \n");
scanf("%s" , temppass);
Beware that the way you read your string is not safe if the length of the input is greater than the length of the string.
The compiler is complaining because the type of &temppass is, as it says, pointer-to-array-of-char; with scanf, the "%s" specifier expects a pointer-to-char. Easy to fix. Change
scanf("%s" , &temppass);
to
scanf("%s" , temppass);
that is, remove the &.
email variable is a single character, and you are trying to pass a string into it.
Make it email[50]; and remove the & in scanf when you're reading a string from stdin, this is what triggers the warning.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
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.
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.
Improve this question
i am new in c,so i want to input string at 2-d array,but don't print string.what's wrong in this code and how can i fixed this problem.thanks in advanced
#include <stdio.h>
#include<string.h>
int main()
{
char col[100][100];
int i,j;
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
scanf("%s",
&col[i][j]);
}
}
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
printf("%s\t",col[i][j]);
}
printf("\n");
}
return 0;
}
You're printing a char with %s. Change instead to %c.
printf("%c\t", col[i][j]);
With %s printf will print all characters until a \0 is found and in your case there's none which will lead to unexpected behavior.
In a 2D character-array, each element col[i][j] is a character. But , you're taking string and printing string using the printf and scanf statements. You need to change the following:
scanf("%c",&col[i][j]);
and
printf("%c\t",col[i][j]);
Use %c instead of %s because %s is a identifier of String. What I found in your code is that you said you want string as input but you are taking char by char as a input. I will suggest you to use pointer to take String as a input in 2D char Array.
Taking String as a input in 2D char Array
#include <stdio.h>
#include <string.h>
int main() {
char *s[100];
char s1[100];
int i;
for(i=0;i<5;i++){
scanf("%s", s1);
s[i]=strdup(s1);
}
for(i=0;i<5;i++){
printf("%s\n", s[i]);
}
return 0;
}
What I am doing is that I am taking the input from user in s1 and put that in the 2D char array. By using this method you have put each string in a each row.
Hopefully that help.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am trying to develop a small program which is available below:
When I run the program, even after the right input, it gives me the output described in else.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char course;
printf("Enter Your Course Name: \n");
scanf(" %s", &course);
if (course == 'TOEFL') {
printf("Yes, you are eligible \n");
} else {
printf("You Can Not Join Us \n");
}
return 0;
}
You mean
scanf(" %c", &course);
But also,
Strings in c MUST be wrapped with double qoutes, the expression 'TOEFL' must be generating a warning about multi character constant, do not ignore it.
Strings in c, are compared one character at a time, so you need to use a function called strcmp() for that.
To read a string, you need an array to store it in, and yes, the "%s" specifier
char cours[100];
scanf("%99s", course);
if (strcmp(course, "TOEFL") == 0) ...
course is a char variable, so it only can contain a single character.
Try changing its declaration to:
char course[10];
See my code comments for explaination //xxxxxx below
#include <stdio.h>
#include <stdlib.h>
int main()
{
char course; //this decl stores only single character not string
printf("Enter Your Course Name: \n");
scanf(" %s", &course); //passing string argument
if (course == 'TOEFL') {//can't compare strings with == use strcmp
printf("Yes, you are eligible \n");
} else {
printf("You Can Not Join Us \n");
}
return 0;
}
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 7 years ago.
Improve this question
i was writing a password checking program. and i encountered a problem.
when i run the program, everything gets printed out as plain text instead of allowing user input.I have also tried to run other programs that worked before but they are all having the same problem. Here is my code and an image of the problem :
#include <stdio.h>
main()
{
int password, pass2;
char name;
printf("\nInput your name: ");
scanf("%c", &name);
printf("\nEnter your password(must be a number): ");
scanf("%d, &password");
printf("\nRe-enter your password(must be a number): ");
scanf("%d, &pass2");
if("password == pass2")
{
printf("welcome %c", name);
}
else{
printf("sorry your passwords did not match!");
}
getch();
}
You have made some mistakes,
scanf("%d, &password");
The correct way of calling a scanf is the following.
scanf ( const char * format, ... );
The correct way of coding in C this line would be.
scanf("%d", &password);
Also have an error in line 15.
if("password == pass2")
The correct way of comparing numbers would be
if(password == pass2)
The char problem.
You are declaring a char, that in C is a single character.
When reading a string, you should declare a array of char, and to read/write it you should use %s.
There are problems with this approach too, words like "firstname lastname" will not work, but I will put this as something to you work with.
It's normal some mistakes when learning, you can check some help in the
http://www.cplusplus.com/
For a more "guided" approach you can also check this
http://c.learncodethehardway.org/book/
Here is the "correct" final code. (it's still need some improves but will run as expected.)
#include <stdio.h>
int main() {
int password, pass2;
char name[20];
printf("\nInput your name: ");
scanf("%s", name);
printf("\nEnter your password(must be a number): ");
scanf("%d", &password);
printf("\nRe-enter your password(must be a number): ");
scanf("%d", &pass2);
if(password == pass2)
{
printf("welcome %s", name);
}
else{
printf("sorry your passwords did not match!");
}
}
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 8 years ago.
Improve this question
I am reversing a String without using inbuilt function . its reversing every character but missing last character
here is the program
#include<stdio.h>
void main()
{
char str[10],rev[10];
int i,j,k;
clrscr();
printf("enter the string \n");
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
k=i-1;
for(j=0;j<=i-1;j++)
{
rev[j]=str[k];
k--;
}
rev[j]='\0';
printf("reverse=%s",rev);
getch();
}
I am not getting why the last Char is missing
k = i;
You miscounted it.
Your expression k = i-1; doesn't leave space for the whole reversed string.
The code that computes the length of the string is missing a semicolon:
The compiler interprets it as
for(i=0;str[i]!='\0';i++)
k=i-1;
while the intention has probably been to have
for(i=0;str[i]!='\0';i++)
;
k = i - 1;
Demo on ideone.
Now that the code is "working", you should fix an error that could cause undefined behavior: limit the length of the input to 9 characters in scanf, like this:
scanf("%9s", str);
Without 9, the user could cause undefined behavior by entering more than nine characters, and overflow your ten-byte buffer.