#Hello i have problem, how can i solve this?

129 messages · Page 1 of 1 (latest)

runic torrent
#
#include <stdio.h>

int main()
{
    FILE *db;
    char file_name[255];
    float id, id_01, id_02, id_03, x,y,z;
    int win;
    
    printf("File name: ");
    scanf("%s", file_name);
    
    db =fopen(file_name, "r");
    if(db==NULL) {
        printf("FILE NOT FOUND");
    } else {
        while( fscanf(file_name,"%f %f %f %f",&id, &id_01, &id_02, &id_03)!= NULL){
        x+=id_01;
        y+=id_02;
        z+=id_03;
        }

        switch(win){
        case 1: { x>z&&x>y; }
        break;
        case 2: { y>x&&y>z; }
        break;
        case 3: { z>x&&z>y; }
        break;
        }
        return win;
    
    
}
return 0;
}```

CONSOLE LOG:
shut prairieBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

runic torrent
#

I know the code could be better, but this is a exam problem so i must get to what teacher says

hardy slate
#

whats the question?

tardy flame
#

What is that switch-statement supposed to do?

hardy slate
#

oh you're passing a file name to fscanf not the file

tardy flame
#

Also instead of passing file_name you need to pass db to the fscanf call

hardy slate
#

snap

runic torrent
#

Im trying and is what is giving the problem, to get form a file determinate values

I have the next values
id id_01 id_02 id_03

and refers to politics results being id the place where they vote and 01 02 03 the users so i trying to get it in diferent forms

tardy flame
runic torrent
tardy flame
#

Im trying and is what is giving the problem, to get form a file determinate values
In English please?

hardy slate
#

I think its a matrix?

runic torrent
#

Sorry bad englis

hardy slate
#

and you want the determinant?

#

could be wrong

runic torrent
#

but someone told to change de file name and it worked

#

nice

#

how to solve this

#

I want to evoid some more lines of code

hardy slate
#

fscanf doesn't return a pointer

#

NULL is specifically for pointers

#

if you want to compare against 0 you should use 0

runic torrent
#

Okay

#

thanks all

#

But problem continues

#

Why this happen

#

But i can still enter text

#

nothing happen

#

I share it so can see problem

#
hardy slate
#

ah you should probably be comparing against EOF rather/ as well as 0

runic torrent
#

what's EOF

hardy slate
#

EOF means end of file

runic torrent
#

Okay, will try

hardy slate
#

checking for 0 should error

#

checking for EOF should be when you stop looping

runic torrent
#

so i must put EOF?

#

Or how

#

i cannot imagine that

hardy slate
#
int res = fscanf(...);
if(res == 0) { /* error */ }
if(res == EOF) { /* end loop */ }
else {
  /* continue loop */
}
#

you're likely gonna need more than a simple while loop

#

OR

#

just replace 0 with EOF and assume there will be no error

#

and your simple while loop is fine

runic torrent
#

I cannot use more advanced code

#

Because teacher get mads

#

the thing is that i can continue writting

#

and dont know why

shut prairieBOT
#

@runic torrent Has your question been resolved? If so, type !solved :)

hardy slate
#

where it stopped working the second time

#

go back to that

#

and replace 0 with EOF

#

don't have extra loops or whatever

runic torrent
#

But still some problems

#

id id01 id02 id03 ain't getting their values

hardy slate
#

well your breakpoint is outside the loop

#

which is where you should be if you got to the end

#

and you don't have values

#

if you want to see values you need to break inside the loop

runic torrent
hardy slate
#

your switch isn't in the loop

#

your breakpoint is on the switch

#

if you want to see your values put your breakpoint inside the switch

#

OH well x y and z are 0 anyway lol

runic torrent
#

I get you now but if switch isnt working

#

i want to know why this doesn get the work done

#

those aren't getting their values

hardy slate
#

um

#

switch isn't working because you don't assign a value to win

#

speaking of that you never assign values to x y or z so you're doing illegal code currently

#

you need to assign initial values to a variable before you can use it

runic torrent
#

okay

#

but this ?

#

those aren't working too

hardy slate
#

yeahhhh idk could be a UB thing. YOu should fix UB before you think about that

#

UB means undefined behaviour

#

means youre doing something c says youre not allowed, but doesn't necessarily tell you you're not allowed (because in most cases you just want to assume its not happening rather than check for it)

#

and it can break a bunch of things

runic torrent
#

so what im doing wrong

hardy slate
#

using values you never assigned

#

e.g. win, x, y, z

#

int win; just says win exists, it doesn't say what value it has

#

int win = 0; means it starts as 0

#

its also entirely possible this only compiler doesn't work with multiple files

#

so whatever you do it will never load to other file properly

#

but idk

#

ive not used it

runic torrent
#

Im using 1 file only

#

and i have used more lot

#

still doesnt work

#

But this is good for taking 4 things?

hardy slate
#

yes

#

should be

#

can you just post the code here (not pictures)

runic torrent
#
#include <stdio.h>
#define n 0
int main(){
    FILE *db;
    char file_name[255], text[255];
    float id, id_01, id_02, id_03, x=0,y=0,z=0, all_01,all_02,all_03,half,result;
    int win=0, i;
    
    printf("File name: ");
    scanf("%s", file_name);
    
    db =fopen(file_name, "r");
    if(db==NULL){printf("FILE NOT FOUND\n"); return 0;} 
    printf("Text for db\n");
    while(fgets(text, 255, db)!= NULL) {
        printf("%s",text);
    }
    
   while( fscanf(db,"%f %f %f %f",&id, &id_01, &id_02, &id_03)!= EOF){
        x+=id_01;
        y+=id_02;
        z+=id_03;
        half = (id_01 + id_02 + id_03)/2;
        if (id_01>half) {}
        if (id_02>half) {}
        if (id_03>half) {}
       
   }
   
    if ( x>z&&x>y ) {
        printf("\nElections winner:");
    }
    if ( y>z&&y>x ) {
        printf("\nElections winner:");
    }
    if ( z>x&&z>y ) {
        printf("\nElections winner:");
    }

   /* switch(win){
        case 1: { x>z&&x>y; }
        break;
        case 2: { y>x&&y>z; }
        break;
        case 3: { z>x&&z>y; }
        break;
    }
    
    
   
    printf("\nElections winner:");
    */
    
fclose(db);
return 0;
}```
hardy slate
#

wooooah

#

where did this fgets come from

runic torrent
#

that's the only thing that works

#

some updates

hardy slate
#

you realise that fgets also move the file pointer forward

#

same as fscanf

#

so if you do an fgets you skip all the data

#

and now fscanf can't see it

runic torrent
#

i must get to the init

#

buddy here the thing i must do

There is an existing text file with information on the results of a vote. The file contains in four columns per line: the census tract identifier (one integer), the votes of party 1 (one integer), Party 2 votes (one integer) and Party 3 votes (one integer). The total number of lines in the file is unknown. It is requested: a) Write a program that asks the user for the name of an input file, opens it and displays it for a reason. screen all the contents of the file. (0.5 points). b) Reprocess the file and print in the main program which of the matches (1, 2 or 3) won the Elections. (1 point). c) Reprocess the file and print in how many census tracts there is a party with a majority absolute (more than half of the votes). (1.5 points).

hardy slate
#

well you need to fseek to the start of the file again in order to read from it a second time

#

fseek(stream, 0, SEEK_SET);

#

or rewind(stream)

runic torrent
#

redind

hardy slate
#

where stream is whatever file you want to do this to

runic torrent
#

rewind

#

buddy

#

it works

#

like shound

#

should

#

buddy im happy

#

thanks you

#

too much

#

really

#

I hope god is on your side

#

always

shut prairieBOT
#

@runic torrent Has your question been resolved? If so, type !solved :)