I'm trying to use scanf to assign values to some integers but whichever scanner I put last will get suck as if it it wasn't seeing the new line. I've tried flushing the input buffer before every scan, but its hasn't helped. Do i need to malloc the pointers, even if they are being passed as the address of an existing variable out of function?
code:
void settings(int *x, int *y, int *l, int *m){
printf("Please enter the size of the game board (e.g. 3x6): ");
scanf("%dx%d",x,y);
printf("Please select the level of the game (0-9): ");
scanf("%d",l);
printf("Please enter the largest card value (1-99): ");
scanf("%d",m);
printf("Please enter the seed of the random number generator: (0-9999): ");
int s;
scanf("%d",&s);
srand(s);
return;}
Thanks
Actually, it should work.
Pay attention that your pointers should be initialized before the call, they can't be NULL:
int *x = (int *)malloc(sizeof(int));
int *y = (int *)malloc(sizeof(int));
int *l = (int *)malloc(sizeof(int));
int *m = (int *)malloc(sizeof(int));
settings(x,y,l,m);
No problem besides that...
Related
I've been working on a C project for a while now and I'm fairly new to C, I've tried multiple things by now but nothing seems to work. Whenever I enter my while loop and get to my sscanf() function, the loop becomes infinite and there is no ability to take the the input. Meaning that I cannot grab and store it in mt 2d array
I've also tried using strtok() as another option, but the code became too messy.
typedef struct item {
char name[50];
double price;
} item;
int main() {
// 1 begins
char userInput[100];
printf("Hello, welcome to the shelf-O-matic!\n");
printf("Please press enter and then type the number of shelves followed by ',' and number of slots\n");
int shelf = 0;
int slot = 0;
scanf("%d , %d", &shelf, &slot);
struct item **thing = malloc(shelf * sizeof(item));
for (int i = 0; i < slot; i++) {
thing[i] = malloc(slot * sizeof(item));
}
while (strcmp(userInput , "quit")!= 0) {
//variables symbolizing the slots and shelves
printf("type quit to exit or set input to the array\n");
//scanf("%100s", userInput);
//Putting the size in memory location
double *cash = malloc(sizeof(cash));
int *x = malloc(sizeof(x));
int *y = malloc(sizeof(y));
//scanf("%100s", userInput);
// 1 ends
char *itemName = malloc(sizeof(itemName));
printf("Please add things to the shelves and slots by specifying <name> <price> <shelf> <slot> or type quit to finish adding\n");
sscanf(userInput,"%100s, %lf, %d, %d", itemName, &cash, &x, &y);
printf(itemName);
printf(cash);
printf(x);
printf(y);
}
return 0;
}
The expected result should be the input taken into memory so it can be assigned to array values afterwards and printed out for validation.
Can someone tell me whats wrong with that code?And i cant use malloc because i havent learn it in class.I mean can i make a 2d array of strings without malloc and if yes how i am i supposed to write an element when i want to change it/print it/scan it.Thanks in advance
int main() {
size_t x,y;
char *a[50][7];
for(x=0;x<=SIZEX;x++)
{
printf("\nPlease enter the name of the new user\n");
scanf(" %s",a[x][0]);
printf("Please enter the surname of the new user\n");
scanf(" %s",a[x][1]);
printf("Please enter the Identity Number of the new user\n");
scanf(" %s",a[x][2]);
printf("Please enter the year of birth of the new user\n");
scanf(" %s",a[x][3]);
printf("Please enter the username of the new user\n");
scanf(" %s",a[x][4]);
}
return 0;
}
So, you need a 2d array of strings (char arrays). One way to achieve this would be to allocate a 3d array of char as:
char x[50][7][MAX_LENGTH];
You can think as having a matrix of array start (of pointers) and then, another dimension to give depth to your matrix (i.e. storage space for your string).
Your approach is also fine, as long as you are willing to allocate manually using malloc or similar storage space for your strings.
can i make a 2d array of strings without malloc
Sure. Let's reduce this to 2*3:
#include <stdio.h>
char * pa[2][3] = {
{"a", "bb","ccc"},
{"dddd", "eeeee", "ffffff"}
};
int main(void)
{
for (size_t i = 0; i < 2; ++i)
{
for (size_t j = 0; j < 3; ++j)
{
printf("i=%zu, j=%zu: string='%s'\n", i, j, pa[i][j]);
}
}
}
Output:
i=0, j=0: string='a'
i=0 j=1: string='bb'
i=0, j=2: string='ccc'
i=1, j=0: string='dddd'
i=1, j=1: string='eeeee'
i=1, j=2: string='ffffff'
I wrote a small C program which takes some user input and prints it,
#include<stdio.h>
int main()
{
char name[], char add[];
short int height;
unsigned char buffer[1024];
int i,j,n,m;
printf("enter your name_length num: n is:-\n");
scanf("%d",&n);
printf("enter your name\n");
for(i=0;i<n;i++)
{
scanf("%c",&name[i]);
}
printf("enter your address: m is :-\n");
scanf("%d",&m);
for(j=0;j<n;j++)
{
scanf("%c",&add[j]);
}
printf("enter your height\n");
scanf("%d",&height);
for(i=0;i<n;i++)
{
printf("your entered name is:\t",name[i]);
}
for(j=0;j<n;j++)
{
printf("your entered address is:\t",add[i]);
}
printf("your entered height is:\n",height);
return;
}
but while running I am getting an error -
So I am at my wits end on why the array size missing is coming, is there anything that I have missed ???
There are few problems in declaration syntax.
Change this line
from
char name[], char add[];
to
char name[256], add[256];
After this you get run-time errors ( welcome to C programming )
As others have pointed out, the line char name[], char add[]; is invalid. This is because there is no way of knowing how much memory to assign to the array. There are two solutions, depending on your problem.
The best solution in this instance would be to move the declaration to below where you know the length of the user's name (assuming you're not using a strict format requiring declarations at the top):
printf("enter your name_length num: n is:-\n");
scanf("%d",&n);
char name[n];
This creates a fixed length array (of length n), and given a user's name isn't likely to change length over the course of this program this is more than acceptable.
The alternate solution is to use memory allocation, which allows you to increase or decrease the size of the array during runtime.
char *name = null;
...
printf("enter your name_length num: n is:-\n");
scanf("%d",&n);
name = malloc( n * sizeof(char));
This assigns n * sizeof(char) bytes to the array, which can later be extended, reassigned or freed. This is the more flexible option.
In C you must declare array with a constant integer like,
char name[1000], add[1000];
OR
char name[1000];
char add[1000];
OR
int size=1000;
char name[size];
char add[size];
And your main function should return integer at the end, use
return 0;
I am trying to take in array which contains some integer values/string values from the user, and not sure how long the array is going to be.. If i intitalise like array[500], i know it is a poor solution and poor programming skills.. How do i improve this?
Sample code below:
int main(void){
int t=0;
char str[200];
int count[20];
printf("Please enter the number of test cases(between 1 to 20):");
scanf("%d",&t);
for ( int i = 1; i<=t;i++)
{
printf("Please enter each of %i test case values:",i);
gets(str);
//set_create(str);
printf("\n");
}
for(int i = 0;i<t;i++)
{
prinf("%i",count[i]);
printf("\n");
}
return 0;
}
The code above is wrong definitely.. Need some help to improve the code...Thanks
Edited code:
int main(void){
int T=0;
int *count;
printf("Please enter the number of test cases(between 1 to 20):");
scanf("%d",&T);
count = malloc(T * sizeof *count);
for ( int i = 1; i<=T;i++)
{
printf("Please enter each of %i test case values:",i);
fgets(count);
printf("\n");
}
return 0;
}
Just use a pointer and malloc / realloc memory as needed. Don't use gets - it's unsafe and no longer in the standard. Use fgets instead.
For example, if you don't know how many count elements you need:
int *count;
scanf("%d", &t);
count = malloc(t * sizeof *count);
In such situation you can allocate memory for array in heap with functions malloc/calloc or on the stack with function alloca() (in header "alloca.h");
Note: This is a homework question.
Use FOR construction to fill 2D board with values that were given by
user. The program asks for board size n, m and then it asks for each
board value.
My try
#include <stdio.h>
int main(){
printf("Enter the number of columns");
int i = scanf("%d",&i);
printf("Enter the number of rows");
int y = scanf("%d",&y);
int r[i][y];
int a;
int b;
for (a=0; a<i; a++){
for(b=0; b<y; b++){
int r[a][b] = scanf("%d",&a,&b); //bug
}
}
}
Bug: c:13 variable-sized object may not be initialized
EDIT:
#include <stdio.h>
int main(){
printf("Enter the number of columns");
int i;
scanf("%d", &i);
printf("Enter the number of rows");
int y;
scanf("%d", &y);
int r[i][y];
int a;
int b;
for (a=0; a<i; a++){
for (b=0; b<y; b++){
scanf("%d",&r[a][b]);
}
}
}
scanf takes the address of the variable that is being read and returns the number of items read. It does not return the value read.
Replace
int i = scanf("%d",&i);
int y = scanf("%d",&y);
by
scanf("%d",&i);
scanf("%d",&y);
and
int r[a][b] = scanf("%d",&a,&b);
by
scanf("%d",&r[a][b]);
EDIT:
You are using variable length array (VLA) in your program:
int r[i][y];
as i and y are not constants and are variables. VLA are a C99 standard feature.
you have to allocate the 2D array dynamically cause you don't know it size in compilation.
replace
int r[i][y];
with
int *r = malloc(i*y*sizeof(int));
and when finish, add:
free(r);
*and also the SCANF errors, people already answered here.
First the return value of scanf isn't the value that was read from stdin, instead it is the number of input values scanf read.
Secondly, C does not allow creation of arrays by using variables. You have to first create one array by dynamically allocating it. And for each entry in the first array you have to create yet another array.
Don't forget to free the memory you allocate!
The use of scanf(%d, &var) is incorrect.
scanf reads from console an integer (this type is specified by its first paramenter %d) and stores it in the second parameter.
This second parameter must be a pointer, so an & is needed when your variable is not a pointer yet.
Therefore you should correct your code in this way:
int i;
scanf("%d",&i);
int y;
scanf("%d", &y);
And in your for loop
scanf("%d", &r[a][b]);
It doesn't print the message because of line buffering.
If you add \n to your strings (to start a new line), it might do what you expect:
printf("Enter the number of columns\n");
Alternatively, if you really want the user to type on the same line, you need to flush the buffer manually:
printf("Enter the number of columns");
fflush (stdout);