#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:
#Hello i have problem, how can i solve this?
129 messages · Page 1 of 1 (latest)
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.
I know the code could be better, but this is a exam problem so i must get to what teacher says
whats the question?
What is that switch-statement supposed to do?
oh you're passing a file name to fscanf not the file
Also instead of passing file_name you need to pass db to the fscanf call
snap
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
yeah, you got it be4 me :/
Im trying and is what is giving the problem, to get form a file determinate values
In English please?
I think its a matrix?
Sorry bad englis
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
fscanf doesn't return a pointer
NULL is specifically for pointers
if you want to compare against 0 you should use 0
Okay
thanks all
But problem continues
Why this happen
But i can still enter text
nothing happen
I share it so can see problem
GDB online Debugger
Online GDB is online ide with compiler and debugger for C/C++. Code, Compiler, Run, Debug Share code nippets.
ah you should probably be comparing against EOF rather/ as well as 0
what's EOF
EOF means end of file
Okay, will try
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
I cannot use more advanced code
Because teacher get mads
the thing is that i can continue writting
and dont know why
@runic torrent Has your question been resolved? If so, type !solved :)
have what you had before
where it stopped working the second time
go back to that
and replace 0 with EOF
don't have extra loops or whatever
It's working, nice
But still some problems
id id01 id02 id03 ain't getting their values
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
I don't get you
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
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
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
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
so what im doing wrong
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
Im using 1 file only
and i have used more lot
still doesnt work
But this is good for taking 4 things?
#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;
}```
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
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).
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)
redind
where stream is whatever file you want to do this to
rewind
buddy
it works
like shound
should
buddy im happy
thanks you
too much
really
I hope god is on your side
always
@runic torrent Has your question been resolved? If so, type !solved :)