As already commented on the condition of for
is not correct and should be:
nome[i] != '\0' && nome[i] !='+'
Which means, as long as I don’t get to the end and as long as I don’t get to one '+'
.
In addition to the cont
is not being incremented within the for
. Actually that cont
nor is it necessary and so I suggest that you remove it and keep only the i
which is more than enough to solve the problem.
Rewrite to:
#include <stdio.h>
int main(int argc, char** argv) {
char nome[100], num1[100];
int i; //sem cont
scanf("%s", nome);
for(i = 0; nome[i] != '\0' && nome[i] !='+'; i++) {
num1[i] = nome[i]; //utilizando so o i
}
num1[i] = '\0'; //com i aqui tambem
printf("%s\n", num1);
return 0;
}
See the code running on Ideone
I changed the last printf
by one that shows the string
copied to make it easier to view the result.
I think the idea of showing the cont
at the end was to show until which letter was copied. In this solution the final value of i
has the same information if you want to show it.
C does not accept "Aggregate" conditions so your for should be "for(i = 0; i < name[i] && name[i] != '+'; i++)"
– Penachia
Thank you @Penachia, this detail went unnoticed
– rafael marques