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 */
}
}
Related
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.
I am trying to dynamically initialize an array, but when I enter the while loop the first time printf prints the statement, but the next printf statement doesn't execute unless I put another value. I want to put values between
0--->n-1
First time printf statement executed but the 2nd time does not execute unless I enter any value. tried to enter 5 for the size, and put 0,1,2,3,4 for the values.
#include <stdio.h>
#include <malloc.h>
void main() {
Ex5();
system("pause");
}
void Ex5()
{
int size_a,n_res=0,res=0;
int *arr_a = input_array_dyn(&size_a);
res = includes(arr_a, size_a);
printf("res is %d ", res);
free(arr_a);
}
int* input_array_dyn(int *size) {
int i=0, *p_to_arr;
printf("enter size of arr:");
scanf_s("%d", size);
p_to_arr = (int*)calloc(*size,sizeof(int));
while(i<*size) {
printf("enter %d element", i);
scanf_s(" %d ", &p_to_arr[i]);
i++;
}
return p_to_arr;
}
The format string in
scanf_s(" %d ", &p_to_arr[i]);
is troublesome and could possibly be the cause of your problem.
The problem with the format string is the trailing space. The trailing space means that scanf_s will read all trailing space characters, until there are no more spaces. The problem is that for scanf_s to know there are no more spaces, you must enter some non-space input.
This leads to scanf_s blocking until you write a second input.
The solution is to not have any spaces in the format string at all:
scanf_s("%d", &p_to_arr[i]);
The leading space isn't needed either, as the "%d" specifier will skip leading space automatically.
If I understand you correctly, changing the format of the second scanf to "%d" should help. I've tested it locally and I'm able to enter all the values at once.
So I made a program for deleting a char from a string, it looks like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* deleteChar(char* texto,int n,char del,int i,int j)
{
/*for(i=0,j=0;i<n;i++)
if(texto[i]!=del)
{
texto[j]=texto=i;
j=+1;
}
for(n-i;n-i<n;i--)
texto[n-i]=NULL;
return(texto);*/
if(i!=n)
{
if(texto[i]!=del)
{
texto[j]=texto[i];
j+=1;
}
i+=1;
texto=deleteChar(texto,n,del,i,j);
}
else
{
i=i-j;
for(n-i;n-i<n;i--)
texto[n-i]=NULL;
return(texto);
}
}
void main()
{
char del;
printf("Remover: \n");
scanf("%c",&del);
char* texto;
texto=(char*)calloc(0,sizeof(char));
printf("Texto: \n");
scanf("%s",texto);
int n=0;
n=strlen(texto);
/*char del;
scanf("%c",&del);*/
texto=deleteChar(texto,n,del,0,0);
printf("%s ",texto);
}
Focus on main(), for some reason if I scanf("%c",&del) after getting my string, the program breaks (before even getting the del input'), but if I do it after it works greatly.
I have no idea why.
The problem is that you have redeclared del in line:
char del;
scanf("%c",&del);
So just remove second deceleration of del.
You are only allocating space for one char
texto = (char*)calloc(0, sizeof(char));
You need to allocate enough space for your entire string, including NULL terminator, this will let you have a string of 20 chars plus a NULL terminator
texto = (char*)calloc(21, sizeof(char));
And you should at the very least control how many characters you can read into your string in scanf() so you can't overflow your buffer
scanf("%20s", texto);
For safer and more robust user input, you should look up fgets().
You have problem reading del after reading the string because, scanf() used to read the string leaves the final \n in the input buffer unconsumed if your format string is like
scanf("%s", stringname);
scanf("%c", charactername);
You could solve this by
scanf("%s", stringname);
scanf(" %c", charactername);
adding a space before the %c in the scanf() used to read the character. This will consume any white space character still left in the input buffer before reading the actual character.
This is because for %c leading white space is not automatically skipped.
Note that you are allocating memory only for a single character with
char* texto;
texto=(char*)calloc(0,sizeof(char));
instead you could do
char* texto;
texto=calloc(0,sizeof(char)*10);
where 10 is the size of the string including the \0 character. So you can read maximum 9 characters into texto otherwise overflow will occur.
To avoid the overflow, while reading with scanf() use a width specifier as in
scanf("%9s", texto);
You don't need to explicitly cast the return value of calloc() in C. The conversion to char * from the void * returned by calloc() will be done implicitly.
And to delete you could just do
char* deleteChar(char* texto,int n,char del,int i,int j)
{
char *ptr=strchr(texto, del);
if(ptr!=NULL)
{
sprintf(texto, "%.*s%s", ptr-texto, ptr+1);
}
}
You don't need to return texto because what was passed is an array.
char* deleteChar(char* texto,int n,char del,int i,int j)
{
char *ptr=strchr(texto, del);
if(ptr!=NULL)
{
sprintf(texto, "%.*s%s", ptr-texto, texto, ptr+1);
}
}
strchr() is used to get the first occurrence of a character in a string.
Also, the return type of main() shouldn't be void. See Return type of main function.
i am starting to learn C and I have ran into the problem of adding a string input to a 2D array, i am able to get the string input correctly but when i try and add the string element to the array it is not working as expected.When printing the array(which is how i test the program) it will assign each index in the array a single character instead of the entire string.
And here is my code for viewing, thank you very much in advance i appreciate any help that is posted.
#include <stdio.h>
main()
{
char c_array[3][3];
int x;
int y;
int z=10;
int i_user_input;
char c_name[256];
for(x=0;x<=+2;x++)
{
for(y=0;y<=2;y++)
{
printf("\nPlease enter a name to the system:");
scanf(" %s",&c_array[x][y]);
printf("The string is %s\n",c_name);
printf("Please press '1' if you would like to keep entering names\n");
printf("Please press '2' if you would like to print the list of names:");
scanf("%d",&i_user_input);
if(i_user_input==1)
continue;
if(i_user_input==2)
for(x=0;x<=2;x++)
{
for(y=0;y<=2;y++)
{
printf("c_array[%d][%d]=%c\n",x,y,c_array[x][y]);
}
}
}
}
}
The output looks as follows with the sample input 'Penelope!'
c_array[0][0]=P
c_array[0][1]=e
c_array[0][2]=n
c_array[1][0]=e
c_array[1][1]=l
c_array[1][2]=o
c_array[2][0]=p
c_array[2][1]=e
c_array[2][2]=!
When you declare:
char c_array[3][3];
This means that each element of your 2D array is a "char" (a single character); not a "string of characters".
To have each element be a string of characters, you need to declare your array in the following way:
char string_array[3][3][256];
I am not sure this is what you want to do though. Mw feeling is that you want an array of 3 elements where each element is a string. In that case, [3] is hardly enough, your strings will have to be less than two characters (the third being reserved for the terminating zero).
Strings aren't a type. They're a value pattern, like if you say an int stores a multiple of ten, then it ends in 0... If you say an array of char stores a string, then it ends at the first '\0', see? We can store multiples of ten in different kinds of integer variables, and likewise for strings.
Strings are patterns of values, not types. When choosing which integer type we want to use, we consider the range for integers. We choose int for small integer values (less than 32768), long for larger values (less than 2147483648) or long long for values larger than that. As a result, we choose the correct type of integer depending on the multiple of ten we expect. Likewise for strings, we need to make sure we have enough memory for the characters in the string followed by the '\0' at the end.
The character &c_array[x][y] only has enough memory for 0 characters followed by a '\0' at the end; it's only useful for storing an empty string. Perhaps you meant to declare c_array like this:
char c_array[3][3][256];
In this case, scanf expects %s to correspond to an argument of the type char *... but as your compiler will probably warn you, &c_array[x][y] will have the type char (*)[256]. If you want to fix that warning, you'll need to remove the & from your scanf code:
if (scanf("%s", c_array[x][y]) != 1) {
/* XXX: Handle an error here, *
* e.g. by exiting or returning or something */
}
While we're on that topic, you'll notice that I removed the redundant space; %s already performs the functionality that the format directive would perform. I also wrote code to check the return value of scanf, as you should...
Remember how we spoke about choosing the types of integers that we use to store data? One other consideration is whether or not the data we intend to store can be negative. Consider this: In your code, should x and y be able to store negative values? What sense does a negative array index make? It is advisable that all array indexes be declared as size_t. When you use printf to print a size_t value, you'll want to use the %zu format specifier rather than %d.
char c_name[256];
/* ... */
printf("The string is %s\n",c_name);
Now, if the string is stored into c_array[x][y], then what is the point of c_name? That's an unnecessary variable. Use c_array[x][y], instead.
printf("\nPlease enter a name to the system:");
Common implementations will often refrain from printing characters until a '\n' is written; the line is printed all at once rather than character by character. As a result, you might see some strange behaviour such as this line not appearing at the right time. Fix this problem by putting the '\n' on the end of the line, rather than the beginning:
printf("Please enter a name to the system:\n");
... or use puts, which adds a '\n' for you automatically:
puts("Please enter a name to the system:");
... or use fflush, which will write all pending unwritten data even if there is no '\n':
fflush(stdout);
In your code
scanf(" %s",&c_array[x][y]);
printf("The string is %s\n",c_name);
both the statements are wrong.
%s expects a pointer to an array, not a single char. To scan a single char, you need %c format specifier.
c_name, as used in the printf() is uninitialised. Using uninitialised value invokes undefined behaviour.
Solution: To take input element by element, you can do do something like
for(x=0; x<=2 ;x++)
{
printf("Start entering the name, one character at a time\n");
for(y=0; y< 2; y++) //note only '<' here
{
scanf(" %c", &c_array[x][y]); // note the leading space
}
c_array[x][y] = 0; //null termination of array to be used as string
}
If you want to assign a string to each index in the array, then create the array as follows :
#include<stdio.h>
int main(void)
{
char a[2][10];
int i=0;
char temp[20];
for(i=0;i<2;i++)
{
scanf("%s",temp);
strcpy(a[i],temp);
}
printf("%s",a[0]);
return 0;
}
Each string that you enter will be stored in each index of the array.
#include <stdio.h>
#include <string.h>
int main(void){
char c_array[3][3];
int x;
int y;
//int z=10;//unused
int i_user_input;
char c_name[256];
printf("\nPlease enter a name to the system:");
scanf("%255s",c_name);
printf("The string is %s\n",c_name);
printf("Please press '1' if you would like to keep entering names\n");
printf("Please press '2' if you would like to print the list of names:");
scanf("%d", &i_user_input);
if(i_user_input==1)
;
else if(i_user_input==2){
memcpy(c_array, c_name, sizeof(c_array));
for(x=0;x<3;x++){
for(y=0;y<3;y++){
printf("c_array[%d][%d]=%c\n", x, y, c_array[x][y]);
}
}
}
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 3
int main(void){
char *c_array[SIZE][SIZE] = { NULL };
int x;
int y;
//int z=10;//unused
int i_user_input;
char c_name[256];
for(x=0;x<SIZE;x++){
for(y=0;y<SIZE;y++){
printf("\nPlease enter a name to the system:");
scanf("%255s", c_name);
printf("The string is %s\n", c_name);
printf("Please press '1' if you would like to keep entering names\n");
printf("Please press '2' if you would like to print the list of names:");
scanf("%d", &i_user_input);
c_array[x][y] = strdup(c_name);
if(i_user_input==1){
continue;
}
if(i_user_input==2){
int x, y;//local variable
for(x=0;x<SIZE;x++){
for(y=0;y<SIZE;y++){
if(c_array[x][y] != NULL)
printf("c_array[%d][%d]=%s\n", x, y, c_array[x][y]);
}
}
}
}
}
for(x=0;x<SIZE;x++)
for(y=0;y<SIZE;y++)
free(c_array[x][y]);
}
Why not use pointers in you array/matrix?
#include <stdio.h>
#include <string.h>
main()
{
char *c_array[3][3];
int x;
int y;
int z=10;
int i_user_input;
char c_name[256];
for(x=0;x<=+2;x++)
{
for(y=0;y<=2;y++)
{
printf("\nPlease enter a name to the system:");
scanf(" %s",c_name);
new_string = malloc(strlen(c_name)+1)
if (new_string == NULL) {
print("error allocating string\n")
exit(-1);
}
strcpy(new_string, c_name);
c_array[x][y] = new_string
printf("The string is %s\n",c_name);
printf("Please press '1' if you would like to keep entering names\n");
printf("Please press '2' if you would like to print the list of names:");
scanf("%d",&i_user_input);
if(i_user_input==1)
continue;
if(i_user_input==2)
for(x=0;x<=2;x++)
{
for(y=0;y<=2;y++)
{
printf("c_array[%d][%d]=%c\n",x,y,c_array[x][y]);
}
}
}
}
for(x=0;x<=2;x++) {
for(y=0;y<=2;y++) {
free(c_array[x][y])
}
}
}
The code above stores the input string in each cell of the array, if that is your goal.
Note: I have no tried it, so there may be subtle errors. My point is to show you how to use pointers and their equivalence to arrays. Using C without pointers is pointless. :)
My code only takes 5 values as input.? What am i doing wrong?
#include<stdio.h>
#include<stdlib.h>
int main()
{
char arr[3][3];
int i,j,n;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%c",&arr[i][j]);
}
}
return 0;
}
How should i correct it?
Change
scanf("%c",&arr[i][j]);
to
scanf(" %c",&arr[i][j]);.
Notice the space given before specifier to consume \n left in stdin buffer when you pressed enter.
Each \n is working as input taking your space from input space.
It should work, but note that %c will read only a single character. Whitespace is not suppressed as it is for other (more "high-level") format specifiers, so if you're separating your characters by blanks of any kind those will be read instead of the actual characters.
Also note that you should check the return value of scanf(), it can fail if no suitable input is present.
The first input is stored in arr[0][0] then when you press enter(return key) it is stored in arr[0][1] and while you think you are inputting the second character actually you are giving the third input. Try the code below to see if you didnt get it.
#include<stdio.h>
#include<stdlib.h>
int main()
{
char arr[3][3];
int i,j,n;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Input array [%d][%d]",i,j);
scanf("%c",&arr[i][j]);
}
}
}
And as for the correction you need a scanf(" %c",&arr[i][j]); a space infront of %c to consume the \n
Hope it answers your question
Instead you can use gets(arr[i][j])
instead of scanf("%c",&arr[i][j]);
This will work perfectly. It is good idea to use gets() and puts() function instead of printf() and scanf() function for string and character in C
you can use fflush(stdin)
char arr[3][3];
int i,j,n;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%c",&arr[i][j]);
fflush(stdin);
}
}