Hi
I have a candidate[MAX][100]
i must compare if an element start by "M"
i put like this but not working like it should.
Any help please?Code:for(i=0;i<MAX;i++ { if(strcmp((char*)candidatos[i],"M")==0) { } }
Thanks
Hi
I have a candidate[MAX][100]
i must compare if an element start by "M"
i put like this but not working like it should.
Any help please?Code:for(i=0;i<MAX;i++ { if(strcmp((char*)candidatos[i],"M")==0) { } }
Thanks
First, don't cast to char*. If you added a cast to silence a warning, that is wrong. Rarely is casting the right way to fix a compiler warning.
Now, you need to provide more information. You forgot to give the type of "candidatos". You say it is "not working like it should", but you fail to explain how it is not working.
Here's a tip: provide a complete program (one that compiles), that shows the problem. Don't just half-assedly type in a part of your source code. Who knows what errors you've introduced by typing it in. At the very least, you're missing a ) character.
First read How To Ask Questions The Smart Way. Also, most people just skip code that does not compile unless the question is "why does not my code compile", and even then your answer will probably be "Because you wrote X lines of code without compiling and testing".
So read the link that I posted and then come back with code that compiles and enough of that so you can actually answer any questions about it. Also state what you have tried to fix the problem yourself.
You'll definitely learn a lot more if you put some effort into it.
OK,
So the type is
The function isCode:#define MAX 5 char candidatos[MAX][100]
The file must be writen if the names start by "M"Code:int escreveCandidatosTXT(char candidatos[][100]) { FILE *f; int i; f=fopen("candidatosM.txt", "a+"); if(!f) { printf("Erro no ficheiro"); return -1; } for(i=0; i<MAX; i++) { if(candidatos[i][0]=='M') { fprintf(f, "%s",(char*)candidatos[i]); } else { printf("Nothing to write..."); return 0; } } return 0; }
This is still not a question. Did you read the link that I gave you?
Come back again with a question, preferably a question that shows that you have made an effort to solve this yourself.
I mean, what happens when you try to compile your program? What happens when you run it, if it compiles? What do you expect to happen?
If you want to ask for help and have a function that does not work then you should at least include the code for the function and any code it needs to run. You should also include code showing how you call that function.
A good way to learn programming is to start with a small program that compiles. Add a few lines of code to that program. Fix it so that it compiles cleanly without warnings. Verify that it does what you intend it to do. Then add some more lines of code and rinse and repeat.
I don't mean to be mean. You will learn more and faster this way.
I highly recommend using a debugger and stepping through the program. What happens when you come across a name which doesn't start with a 'M'?Code:if(candidatos[i][0]=='M') { fprintf(f, "%s",(char*)candidatos[i]); } else { printf("Nothing to write..."); return 0; }
Bye, Andreas
The program compiles, no errors at all
I can write names into the array, i can sort the array, i can print the array, and then i must write into a txt file, the names starting by M.
This is the full code
So, the program runs well, the problem is if i have a name started by M he doesn't write anything into my txt file.Code:#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 3 int ordenaCrescente(char candidatos[][100]) { int i,j; char aux[100]; for(i=0; i<MAX-1; i++) { for(j=MAX-1;j>i; j--) { if(strcmp((char*)candidatos[j],(char*)candidatos[j-1])<0) { strcpy(aux,(char*)candidatos[j]); strcpy((char*)candidatos[j],(char*)candidatos[j-1]); strcpy((char*)candidatos[j-1], aux); } } } return 0; } int escreveCandidatosTXT(char candidatos[][100]) { FILE *f; int i; f=fopen("candidatosM.txt", "a+"); if(!f) { printf("Erro no ficheiro"); return -1; } for(i=0; i<MAX; i++) { if(candidatos[i][0]=='M') { fprintf(f, "%s",(char*)candidatos[i]); } else { printf("Nothing to write..."); return 0; } } return 0; } int listar(char candidatos [][100]) { int i; for(i=0; i<MAX; i++) { printf("%s",(char*)candidatos[i]); } return 0; } int main() { char candidatos[MAX][100]; int i; for(i=0; i<MAX;i++) { printf("Nome: \n"); fgets((char*)candidatos[i], 100, stdin); } printf("List: \n"); listar(candidatos); printf("Sorted list: \n"); ordenaCrescente(candidatos); for(i=0; i<MAX;i++) { printf("%s",(char*)candidatos[i]); } escreveCandidatosTXT(candidatos); system("pause"); return 0; }