I'm learning C but I don't understand why the error appears on line number 9
---> scanf("%[^\n]s",&cadena); // i tried with "%s" but still doesnt work.
#include <stdio.h>
int main(void)
{
char cadena[40];
printf("Ingrese cadena: ");
fflush(stdin);
scanf("%[^\n]s",&cadena);
printf("La cadena es %s \n", cadena);
return (0);
}
Remove the ampersand on cadena in scanf
scanf("%[^\n]", cadena);
An array decays to a pointer so you were actually passing a pointer to a pointer in that case.
Also you can just write it like this
scanf("%s",cadena);
Depends on your end goal though.
Related
My issue is coming from the %c name input, I am getting an error that it is expecting type char * but has type char * [15] for the scanf function. I am also getting an error in the printf where the %c expects int but has type char *. I am still quite new at this so if it could be explained as simply as possible that would be amazing.
#include <stdio.h>
struct Student {
int StudentID;
char Name[15];
float Mark1;
float Mark2;
float Mark3;
} a;
int main() {
float ave;
printf("Please input Student's ID \n");
scanf("%d", &a.StudentID);
printf("Please input Student's name. \n");
scanf(" %c", &a.Name);
printf("Input Mark 1. \n");
scanf("%f", &a.Mark1);
printf("Input Mark 2. \n");
scanf("%f", &a.Mark2);
printf("Input Mark 3. \n");
scanf("%f", &a.Mark3);
ave = (a.Mark1 + a.Mark2 + a.Mark3) / 3;
printf("Student Detail\nStudent ID: %d\nName: %c\nMark 1: %.2f\n Mark 2: %.2f\n Mark 3: %.2f\nAverage: %.2f\n",
a.StudentID, a.Name, a.Mark1, a.Mark2, a.Mark3, ave);
return 0;
}
Your problem is related to the difference between an array of chars and a single char.
The %c format only reads in one character at a time.
If you wish to read a string of characters use %s and it will read until a whitespace. (Please make sure you don't try to read a name more than 14 characters long into your 15 character buffer)
In more depth, your char Name[15] is actually a pointer to a series of chars in memory. You are accidentally trying to change to pointer itself, instead of the chars that it points to. This is why the compiler expects a char * .
Instead if you truly meant to only read one char you could use
scanf(" %c", &a.Name[0]);
to place the character in the first block of the Name array.
If this is too complicated don't worry, it will all come eventually :)
For now I think %s will suffice.
You can use %14s to be extra safe.
Also don't forget to use %s in the final printf as well
In your scanf() call, %c tells scanf() to accept a single character. But your argument is a character array. C is a low-level language; it's not smart enough to realize you wanted a string (char array) as input. You have to tell it by using %s instead of %c.
The user has to enter some items and then the matrix has to print them on 3x4
When i use the "%c" instead of "%s" works but it only shows a character but when I want to print the whole word with "%s" wont work.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char objetos[10][10];
int main(){
int i,e;
for (i=1;i<=3;i++){
for (e=1;e<=4;e++){
system("cls");
printf("Ingrese El Objeto Personal %i-%i: ",i,e);
scanf("%s",&objetos[i][e]);
}
}
system("cls");
for (i=1;i<=3;i++){
for (e=1;e<=4;e++){
printf("%s",objetos[i][e]);
}
printf("\n");
}
return 0;
}
but when I want to print the whole word with "%s" wont work ?
The format specifier %s expects argument of const char* and objetos[i][e] is not of char* type, its of char type.
Change this
for (i=1;i<=3;i++){
for (e=1;e<=4;e++){
printf("%s",objetos[i][e]);
}
to
for (i=1;i<=3;i++){
printf("%s",objetos[i]);
}
Also while scanning, this
scanf("%s",&objetos[i][e]);
is not correct, use %c instead of %s here.
For e.g
for (i=1;i<=3;i++){
for (e=1;e<=4;e++){
system("cls");
printf("Ingrese El Objeto Personal %i-%i: ",i,e);
scanf(" %c",&objetos[i][e]);
}
}
Side note, array index starts from zero(0) not one(1) in C. You may seems to put data into objetos[1] & read from objetos[1] but mistakenly if your program ever try read from objetos[0] then it create the issue.
Better start rotating loop from 0th index. For e.g
for (i=0;i<=3;i++){
for (e=0;e<=4;e++){
scanf(" %c",&objetos[i][e]); /* give the whitespace before %c to avoid buffer overrun */
}
}
I'm running a while loop so the user can constantly enter expressions, until they indicate they want to quit the program. I'm using strcmp() to compare two strings so as soon as they enter quit the program will stop. But the program keeps going, any Ideas?
#include <stdio.h>
#include <string.h>
int main()
{
int min12=0;
char opper;
int x=0;
int min13;
char *Repeatprog="cont";
char *Repeatprog1="quit";
while (strcmp(Repeatprog,Repeatprog1))
{
printf("enter the integer number \n");
scanf( "%d %c %d", &min12, &opper, &min13);
printf("%d %c %d\n", min12, opper, min13);
printf("Type the word quit to end program\n");
scanf("%s", Repeatprog);
}
printf("Good Bye");
return 0;
}
Remember always that an Array is a Pointer to the first object of the array.
And secondly, in your call to scanf() you only read a character. Not a whole string (represented by %s in C)
So in conclusion, your call to scanf() shouldn't have a pointer and should have a string instead of a character.
scanf("%s", Repeatprog);
or simply
gets (Repeatprog);
EDIT :
As the commenter #EOF said, gets() is not a good idea since it can lead to Undefined Behaviour. That's because the program can read more characters than it should have and lead to overflow, thus it isn't secure.
So I recommend using char *fgets(char *str, int n, FILE *stream)
Note:
Also, your code is using string literals. So if you make any attempt to change the content of the char pointer then it will lead to Undefined Behaviour.
For this note, please thank the guys below me [comments]. I made a huge mistake and I'm sorry.
hello I am searching where I did the wrong step?
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int account_on_the_bank=25;
printf("how much money do you have in the banque? \n");
scanf("%f",account_on_the_bank);
printf("Vous avez %d euros sur votre compte",account_on_the_bank);
return 0;
}
where's my problem???? it shows windows has stop working
I have tried everything the same problem shows up always?
This is the problematic line:
scanf("%f",account_on_the_bank);
It has wrong specifier and also not passing the address of the variable to scanf.
It should be:
scanf("%d", &account_on_the_bank);
Please read the basics of C
You have error in your scanf()
scanf("%f",account_on_the_bank);
^ ^
It should be
scanf("%d",&account_on_the_bank);
^ ^
because int needs %d as specifier, and scanf() needs address of the variable in which you want to store the read value(address is obtained by using &)
This question already has answers here:
C Language, scanf issue with char's [duplicate]
(3 answers)
Closed 8 years ago.
I have learnt a few languages in the past and I thought it would be a good I idea to learn C.
I am having a little trouble with scanf...
here is the code:
#include <stdio.h>
#include <string.h>
int main(){
char name[20];
char yn;
printf("Welcome to Benjamin's first C programme! \n\n");
printf("What is your name? \t"); scanf("%s", name); printf("\n");
printf("Is your name %s? [y/n]", name); scanf("%s", yn);
}
I am having trouble with: scanf("%s", yn)
do the following:
printf("Is your name %s? [y/n]", name); scanf("%c", &yn);
What the new scanf does is says "expect a char" and put it in the address of yn
To read into a char use
scanf("%c",&yn);
scanf() can only take pointers as parameters. name is an array so its a pointer by definition, but yn is not. You have to cast it as &yn, a pointer to yn, in order to make scanf() read to it.
Also, yn can only hold a single char, not an array of chars as in name, so you have to tell scanf() you want to read a %c for single char, not a %s for null-terminated string, because if you do you will most likely overwrite the stack and run in trouble.
That being said, use scanf("%c", &yn); instead.
or prepare yourself and do a simple malloc (but that's not the better choice)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char name[20];
char *yn;
yn = (char*) malloc(sizeof(char)*2);
printf("Welcome to Benjamin's first C programme! \n\n");
printf("What is your name? \t"); scanf("%s", name); printf("\n");
printf("Is your name %s? [y/n]", name); scanf("%s", yn);
}