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

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);

Related

Why this code not working in code blocks( working in VS coe)? [closed]

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 3 months ago.
Improve this question
#include<stdio.h>
int qt( int *x);
struct student
{
int id;
char name[10];
char b_g[10];
float cgpa;
char address[20];
};
int num,i;
int main()
{
printf("Input How Many students You Have: ");
scanf("%d",&num);
qt(&num);
}
int qt(int *x)
{
int v,n;
v=*x;
struct student ar[v];
printf("Enter Your ID, Name, Blood Group, CGPA, Adress:\n");
for(i=1;i<=v;i++)
{
scanf("%d %s %s %f %s",&ar[i].id,ar[i].name,ar[i].b_g,&ar[i].cgpa,ar[i].address);
}
printf("\nEnter Your ID:");
scanf("%d",&n);
for(i=1;i<=v;i++)
{
if(ar[i].id==n){
printf("Student ID:%d\n\nName: %s\nBlood Group: %s\nCGPA: %f \nAddress: %s\n",n,ar[i].name,ar[i].b_g,ar[i].cgpa,ar[i].address);
}
}
}
it taking many inputs in code blocks that i don't want to do,,but in same code is working fine in VS code.
I need to run it in code blocks and that should show proper output base on search id.
Firstly I would say that your qt function should probably actually return an integer at some point, seeing as you've declared the return data type without a return statement. Also, as someone mentioned above, it might be helpful going forwards to adopt the widely accepted standard of starting for loops at 0. If adding the return does not fix the issue, could you post the error message you are getting??

How to solve segmentation error in C program [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 1 year ago.
Improve this question
Guys I'm getting a segmentation error, exactly this error to be more specific:
Segmentation fault
Does anyone know how to solve this? my code is simple I don't know why the error, does anyone know how to solve it step by step?
I'm using this command to convert my c file into an executable gcc test.c -o test then I run my test file with./test, but then I get that error as I said above.
My code in c:
#include <stdio.h>
int main(){
int age;
int aget;
int total;
printf("Input your age: ");
scanf("%i", &idade);
printf("Input your age two: ");
scanf("%i", &age);
base = age + aget;
printf(total);
return 0;
}
printf needs a format string. Here we need to tell it %d for integer in decimal.
total = idade + age;
printf("%d\n", total);
Some random resource on how to use printf: https://man7.org/linux/man-pages/man3/printf.3.html
Variable base is undeclared.
Variable idade is undeclared.
It should be printf("%d",base) if your are using base.
#include <stdio.h>
int main(){
int age;
int aget;
int total;
printf("Input your age: ");
scanf("%i", &age);
printf("Input your age two: ");
scanf("%i", &aget);
total = age + aget;
printf("%d",total);
return 0;
}

C programming If statement variable change [closed]

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 5 years ago.
Improve this question
Later on in my program I want to print out the destination that the user has chosen here, how do I do this?
void create(){
char name[200],from,to;
int age;
printf("Please enter your name :\n");
scanf("%s",&name);
printf("Please enter your age :\n");
scanf("%d",&age);
printf("Pick your current destination");
printf("\n1 for London\n2 for Manchester\n3 for Brighton\n4 for
Liverpool\n4 for reading\n5 for coventry\n");
scanf("%s",&from);
if(from == 1){
printf("Select your desired destination %s",&from);
from =("London");
It seems you mean
scanf( " %c", &from );
^^^^^
//...
if( from == '1' ){
^^^
In short, if you want to access the local variable from, declared inside the functoion create(), it is not possible [you can hope the variable lies in the same address after the function has returned and try to access that same address in hope that the value is still there but this is UB].
Waht you can do is to declare it globaly, or return it from the function
// option 1) global variable
char from;
int main(){
... create(); // don't declare from inside the function, because it is already declared globaly
// do something with from
... }
// option 2) return from function - need to change the create function to return the from variable
char from;
int main(){
from = create();
// do something with from
... }
Note that you're using scanf / printf incorrectly:
scanf("%s",&from); should be scanf("%c",&from); since from is char, not char[] or char*
printf("Select your desired destination %s",&from); should be printf("Select your desired destination %c",from);
Here...The program doesn't have any variable for the location.... It means you can't fetch data to print from no where.... So the program must have the data in program in itself.... if not..... No source no data....
If you want to continue the program as it is... You can use switch statement and display the value later as:
switch(from){
case 1:
printf("London\n");
break;
case 2:
printf("Manchestar\n");
break;
case 3:
printf("Brigeton\n");
break;
default:
//(default Option);
}
You can also use character array.
for eg:
int from;
int location[3][10]={"London","Manchestar","Brigeton"};
printf("\n1.London\n2.Manchestar\n3.Brigeton");
printf("\nChoose whose city do you want to visit: ");
scanf("%d",&from);
printf("You have choosen %s",location[from-1]);
In this way, we can give choice to end user and also we can fetch data directly...

Something is always wrong with this [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 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

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;
}

Resources