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 5 years ago.
Improve this question
I have a file which includes product name, product quantity and product price in the following format.
file.txt
3
Product Qty Price
Tv 2 10
Mobile 3 20
Computer 5 30
I want to read the number of products from the integer (such as 3) given above the product list and count the total price of the products. The program will use struct to read the product details such as
struct product {
Char name[30];
int qty ;
float price;
}
What are the best practices to make this program easier?
Please try if the following program can help you.
#include <stdio.h>
#include <stdlib.h>
struct product {
char name[30];
int qty;
float price;
};
int main(void) {
int count = 0;
char line[100];
FILE *fptr;
fptr = fopen("file.txt", "r");
fscanf(fptr, "%d", &count); // count = 3
struct product *p = malloc(sizeof(struct product));
int i = 0;
double sum = 0;
while (i < count + 2 && fgets(line, sizeof(line), fptr) != NULL) {
if (i > 1) {
sscanf(line, "%s %d %f\n", (*p).name, &(*p).qty, &(*p).price);
sum = sum + (*p).price;
}
i++;
}
printf("sum: %f\n", sum);
free(p);
return 0;
}
Test
$ gcc main.c
$ ./a.out
sum: 60.000000
$
Related
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 2 years ago.
Improve this question
I'm trying to read a text file with details into a struct other when I try to print out the output of a struct, i just get met with random numbers or a
.
Can I please get some help where i am going wrong?
Tthe text file is set up as
4, 2, 10000
typedef struct
{
int bed;
int bath;
int price;
}house;
int main()
{
FILE* f;
linkedList* list = createLinkedList();
house sydney[4];
int total;
int ii;
f = fopen("house", "r");
fscanf(f, "%d", &total);
for (ii = 0; ii < total; ii++)
{
fscanf(f, "%d%d%d", &house[ii].bed, &house[ii].bath, &house[ii].price);
printf("Price: %d", house[ii].price);
}
Try this:
fscanf(f, "%d, %d, %d", &house[ii].bed, &house[ii].bath, &house[ii].price);
Also I would suggest to check the return value of fscanf to make sure all the parameters were found.
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 2 years ago.
Improve this question
So i want my code actualy had different sum each cases, but the sum keep adding from the other cases, like for example the cases 2 sum are sum from loop 2nd + cases 1, the cases 4 sum are sum from cases 1,2,3 and loop number 4
the Output.
`
#include <stdio.h>
int main() {
int cases;
int day;
int animal;
int n;
int a;
int sum = 0;
animal = 0;
printf("Enter cases \n ");
scanf(" %d", &n);
for (cases = 0; cases < n; cases++)
{
printf("cases #%d\n", cases+1);
printf("Enter how many days.\n");
scanf(" %d", &a);
for(day=1;day<=a;){
printf("Enter how many animal that you capture at day #%d\n", day);
scanf(" %d", &animal);
day++;
sum = sum + animal;
}
animal = 0;
day = 1;
printf("cases#%d = %d\n", cases + 1, sum);
}
return 0;
}`
You need to return sum to 0 in every case
Put sum=0; under for (cases...
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 2 years ago.
Improve this question
Given a personnel database in an array of structs, the following code should scan for the oldest and youngest person and output both.
However, dependent on the order of the input (first the oldest or first the youngest) it outputs either only the youngest person or the oldest person.
#include <stdio.h>
typedef struct personal_matter_tag{
char name[10];
int age;
}personal_matter_t;
personal_matter_t personnel[3];
int main(){
int min,max,n;
for(n=0;n<2;n++){
printf("Person name:\n");
scanf("%s",&personnel[n].name);
printf("Person age:");
scanf("%d",&personnel[n].age);
}
for (n = 0; n < 2; n++) {
max = personnel[0].age;
if (max < personnel[n].age) printf("%s is older\n", personnel[n].name);
}
for (n = 0; n < 2; n++) {
min = personnel[0].age;
if (min > personnel[n].age) printf("%s is younger\n", personnel[n].name);
}
}
Can you help me in finding the bug which results in the missing output?
why it shows only 1 kid instead of 2 kids
you probably mean why only 2 kids are enter rather than 3, this is because of your for conditions being n<2 rather than n<=2 or better n < sizeof(a)/sizeof(*a)
But you have additional problems in your code
scanf("%s", &a[n].nume);
must be
scanf("%s", a[n].nume);
or
scanf("%s", &a[n].nume[0]);
but it is also dangerous to not limit the size of the read string because if the input name is too long you will write out of the array, at minimum do for instance :
scanf("%9s", a[n].nume);
Your way to get the min and max age is wrong because you always compare with the very first kid, can be
int max_rank = 0;
int min_rank = 0;
for (n = 1; n<sizeof(a)/sizeof(*a); n++) {
if (a[max_rank].anul < a[n].anul)
max_rank = n;
if (a[min_rank].anul > a[n].anul)
min_rank = n;
}
printf("%s is older\n", a[max_rank].nume);
printf("%s is younger\n", a[min_rank].nume);
You also do not check your scanfsuccess because you do not check the return value, this is dangerous and you can use not initialized values because of that.
May be also check the read ages are not negative to refuse them ?
#include <stdio.h>
struct copii {
char nume[10];
int anul;
}a[3];
int main(){
int n;
for(n=0; n < sizeof(a)/sizeof(*a); n++){
printf("Kid's name:\n");
if (scanf("%9s", a[n].nume) != 1) {
puts("EOF, abort");
return -1;
}
printf("Kid's year:");
if ((scanf("%d",&a[n].anul) != 1) || (a[n].anul < 0)) {
puts("invalid age, abort");
return -1;
}
}
int max_rank = 0;
int min_rank = 0;
for (n = 1; n<sizeof(a)/sizeof(*a); n++) {
if (a[max_rank].anul < a[n].anul)
max_rank = n;
if (a[min_rank].anul > a[n].anul)
min_rank = n;
}
printf("%s is older (%d)\n", a[max_rank].nume, a[max_rank].anul);
printf("%s is younger (%d)\n", a[min_rank].nume, a[min_rank].anul);
return 0;
}
Compilation and execution:
/tmp % gcc -Wall c.c
/tmp % ./a.out
Kid's name:
k1
Kid's year:50
Kid's name:
k2
Kid's year:10
Kid's name:
k3
Kid's year:12
k1 is older (50)
k2 is younger (10)
/tmp %
No, it is not the for condition. The check n<2 implies looping over indices 0 and 1, which are indeed two runs. The question was why it shows only 1 kid instead of two and the question was not why it shows only 2 kids instead of 3. Nor is it the syntax of the scanf (The & is not necessary but it is not wrong)
The problem is the that max and min variables are not updated within the for loop.
#include <stdio.h>
struct copii{
char nume[10];
int anul;
}a[3];
int main(){
int min,max,n;
for(n=0;n<2;n++){
printf("Kid's name:\n");
scanf("%s",&a[n].nume);
printf("Kid's year:");
scanf("%d",&a[n].anul);
}
max=a[0].anul;
for(n=0;n<2;n++){
if(max<a[n].anul) max = a[n].anul;
}
printf("The oldest kid is %d years old\n", max );
min=a[0].anul;
for(n=0;n<2;n++){
if(min>a[n].anul) min = a[n].anul;
}
printf("The youngest kid is %d years old\n", min);
}
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 have the following code, which is not how to implement in C, (if it has one, the keyword with.On the other hand because the program does not access the data type structure, it should not give an error because the variables partial_n2, final_n2, name2 are defined in the struct.
The program has to store in an array of records the names of the students, their partial and final grades. Find the average grade and show a message of SUIT if the student exceeds or equals the grade of 5 or NOT SUIT if it is not enough. Do it for a number of 5 students.
#include <stdio.h>
#include <windows.h>
#include <conio.h>
//PROGRAM EJER009
#define numstudents 5
typedef struct notas{
char name2[20];
float partial_n2, final_n2;
}tnotas;
tnotas notas[numstudents];
tnotas clase;
char name[20];
float partial_n, final_n, n_media;
int i;
int main(){
for (i = 0; i <= numstudents;i++)
{
printf("Enter the student's name% d: ",i);
scanf("%s",name);
printf("Enter your partial note: ");
scanf("%f",&partial_n);
printf("Enter your final note: ");
scanf("%f",&final_n);
printf("\n");
with (clase[i])
{
partial_n2 = partial_n;
final_n2 = final_n;
name2 = name;
}
}
printf("cls");
printf("NAME\tPartial\tFinal\tMedia\tQUALIFICATION\n");
for (i = 1; i<=numstudents;i++){
with clase[i]
{
n_media = (partial_n2 + final_n2) / 2;
printf("%d %d %d",name2,partial_n2,final_n2);
system("color 14"); printf("%lf",n_media);
if (n_media >= 5)
{
system("color 11");
printf("SUITABLE :-)");
}
else
{
system("color 1");
printf("NOT SUITABLE :-(");
}
system("color 7");
}
}
getch();
return 0;
}
You can read a value of a member by:
float f;
f = notas[0].partial_n2;
You can write a value of a member by:
notas[0].partial_n2 = 10.3;
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 8 years ago.
Improve this question
I've been asked to write a program that accepts a list of numbers until a non-numeric is encountered (up to 30 numbers), putting the numbers into an array, and keeping track of how many numbers were inserted. Then it should scan through the array to find the largest number, and print the largest.
This is what I've come up with:
#include<stdio.h>
int main()
{
const int INPUT = 30 ;
int size [INPUT];
int i, big;
printf("Type integer numbers, followed by q to quit: ");
while (scanf("%d", &size[INPUT]) != 'q')
{
for(i=0;i<size;i++)
scanf("%d",&INPUT[i]);
big = INPUT[0];
for(i=1;i<size;i++)
{
if(big<INPUT[i])
big=INPUT[i];
}
printf("The largest number is %d",big);
return 0;
}
Besides the problems, I listed in the comments. You seems to be comfused by the varaible names~ Anyway, I made some code for you.
#include<stdio.h>
int main()
{
const int MAX_INPUT = 30 ;
int input[MAX_INPUT];
int size=0, big;
printf("Type integer numbers, followed by q to quit: ");
while(size < MAX_INPUT){
if(scanf("%d", &input[size]) != 1){
break;
}
++size;
}
if(size ==0){
return 0;
}
big = input[size-1];
while( size-- > 0)
{
if(big<input[size]){
big=input[size];
}
}
printf("The largest number is %d\n",big);
return 0;
}
Tested with GCC 4.1.2 and Linux.
Return value of scanf:
Upon successful completion, these functions return the
number of successfully matched and assigned input items
further, you are mixing the size and input, you actually want the size to be a constant and input to be an array:
const int SIZE = 30 ;
int input[SIZE];
So the while loop should look like:
while (scanf("%d", &input[some_index]) == 1)
and of course this is wrong:
scanf("%d",&INPUT[i]); // should be ==> &input[i]