Something is always wrong with this [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
#include <stdio.h>
struct Bank {
char name[500];
int mobile;
float balance;
};
int main() {
int i;
struct Bank a[100];
for (i = 0; i < 3; i++) {
printf("Enter the name of person %d: ", i + 1);
gets(a[i].name);
printf("Enter the mobile of person %d: ", i + 1);
scanf("%d", &a[i].mobile);
printf("Enter the mobile of person %d: ", i + 1);
scanf("%f", &a[i].balance);
}
for (i = 0; i < 3; i++) {
puts(a[i].name);
printf("His Balance is: %f", a[i].balance);
printf("His Mobile number is : %d \n\n", a[i].mobile);
}
return 0;
}
Try running this the user input requests don't come like I want them to, just run it and one would understand. What am I doing wrong?

The reason it's not working as expected is because there is a newline character left on the input buffer on the second and third iteration to gets(), presumably from the previous "enter" hit.
It works as expected (kind of, see below) if you change the gets() call to scanf("%s", a[i].name).
Please note that:
this code is badly vulnerable to buffer overflow on multiple lines (all scanf() and the gets() call) - see here, or here, or here, etc., bottom line: you should not use gets() at all, nor scanf() without a boundary
you have a typo in the third printf and it again asks for the mobile number

Related

Adapted to the windows compiler, I feel pathetic. What is the problem [closed]

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 2 years ago.
Improve this question
I am in C language
Arrays were implemented using input and output functions.
The code is as follows.
#include <stdio.h>
int main()
{
int n;
int arr[1000];
printf("Enter the number of numbers to enter : \n");
scanf("%d", &n);
for(int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for(int i = n - 1; i >= 0; i--)
{
printf("&d ", arr[i]);
}
return 0;
}
However, after running the compilation, this resulted. Below is the (vscode, gcc) console window.
Enter the number of numbers to enter :
5
1 2 3 4 5
&d &d &d &d &d
I don't understand why the notation like %d %d %d appears here.
Adapted to the windows compiler, I feel pathetic. What is the problem
This line ie causing the issue:
printf("&d ", arr[i]);
You are printing not scanning for inputs, it should be like this:
printf("%d ", arr[i]);

Bug in simple C source code? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I want to know if the format of user defined function i wrote i.e return(xxx) is correct or not .
Because when i compile my code, i have to enter the input 2 times.It might be a silly mistake because i just began learning C language
****MY CODE:****``
#include<stdio.h>
long cube(long x );
long input,answer;
int main (void )
{
printf("Enter a number:");
scanf("%ld ",&input);
answer = cube(input);
printf(" The cube of %ld is %ld",input ,answer);
return 0;
}
long cube(long x )
{
return (x*x*x);
}
****ANswer****
#include <stdio.h>
long cube(long x);
long input, answer;
int main( void )
{
printf("Enter an integer value: ");
scanf("%d", &input);
answer = cube(input);
printf("\nThe cube of %ld is %ld.\n", input, answer);
return 0;
}
long cube(long x)
{
long x_cubed;
x_cubed = x * x * x;
return x_cubed;
}
remove the space after '%ld' it will take one input.
according your code,
scanf("%ld '&input) ;
here compiler at first wants one input for '%ld' then it waits for blank space you used after '%ld'. remove it then it will go next step after one input.
you should use,
scanf("%ld%",&input);

Ask user to enter an integer value for each element of the array [closed]

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 7 years ago.
Improve this question
• Declare an array type of int and size of 5
• Ask user to enter an integer value for each element of the array
• Then display every element of the array user entered
please help me where to start i don't know much about array.
One advice, if you don't learn for yourself, then nobody can teach you. So, please try researching a bit on the internet, or reading a good book before you post a question.
Assuming this is your first mistake, and you wont repeat this without working a little on your own, i'll provide the code and the explanation..
#include<stdio.h>
int main(void)
{
int arr[5], i;
printf("please enter the numbers one by one");
for(i = 0; i < 5; i++)
{
scanf("%d", &arr[i]);
}
printf("The numbers you entered are\n");
for(i = 0; i < 5; i++)
{
printf("%d ", arr[i]);
}
}

I am very new in c, and I need some help of arrays [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
So basically, I want user to put their family size and depending on their size, I want to ask their ages as many times as family member( so input depends on them).
Now, I want the age input as array listing. for example, if they are 2 of them , then I want to ask their age twice and save it as
scanf("%d",age[0]);
scanf(%d",age[1]);
Here I could think of c code:
#include <stdio.h>
int main(int n, int age[0]){
printf("enter your family number:");
scanf("%d", n);
}
for(i=1; i>=n; i++){
printf("Can I have your family's age one by one:");
scanf("%d",&age[i]);
}
#include <stdio.h>
int main(void){
int n;
printf("enter your family number:");
scanf("%d", &n);
int i, age[n];
for(i=0; i<n; ++i){
printf("Can I have your family's age one by one:");
scanf("%d", &age[i]);
}
return 0;
}

fwrite there got bug, but i don't know how? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
help me solve this coding... it keeps said got errors :-
it said fwrite there got problems..
// #define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
struct prod
{
char ProductCode [5];
int Expired_year;
char Product_country [25];
}product;
struct prod product;
void main()
{
char x ;
FILE* data;
data = fopen("product.dat","wb");
while(x != 'N')
{
printf("Enter product code :");
scanf("%s", product.ProductCode);
printf("Enter expired year of the product :");
scanf("%d", &product.Expired_year);
fflush(stdin);
printf("Enter product country :");
scanf("%[^\n]", product.Product_country);
fflush(stdin);
fwrite(&prod, sizeof(product), 1, data);
printf("\nPlease enter anykey to continue or 'N' to stop: ");
scanf("%c", &x);
fflush(stdin);
printf("\n");
}
fclose(data);
}
fwrite(&prod, sizeof(product), 1, data);
prod doesn't refer to a struct instance but to the name of the structure, it should be
fwrite(&product, sizeof(product), 1, data);
Wrong syntax for fwrite().
It should be,
fwrite(&product, 1, sizeof(product), data);
The first argument needs a pointer to some data, but &prod is not valid. prod is a type, not an instance variable. You probably want to change it to fwrite (&product, ...)
First of all, you're declaring struct prod product twice.
Second, the correct sintax for fwrite() is fwrite(&product,sizeof(struct prod),1,data);

Resources