Reputation: 11
I have this program that i want to make. There's a string that can be made from "troduplo" "duplo" "minus". A variable needs to start with 1 (c=1) and read the string. If it reads "troduplo" it triples c thats at that moment by 3 (c*=3), for "duplo" its double and for "minus" is just c=c-1. Ive made this that works well i guess with small numbers for the output, but when it gets over 10 digits it doesnt work anymore. How can i do this?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char string[250];
long long c=1;
scanf("%s", &string);
for(int i=0;i<strlen(string);i++){
if(string[i]=='t'||string[i]=='T'){
c*=3;
i+=7;
/*printf("%d - Troduplo\n", c);*/
continue;
}
if(string[i]=='D'||string[i]=='d'){
c*=2;
i+=4;
/*printf("%d - Duplo\n", c);*/
continue;
}
if(string[i]=='M'||string[i]=='m'){
c-=1;
i+=4;
/*printf("%d - Minus\n", c)*/;
continue;
}
}
printf("%d", c);
return 0;
}
example
input :
tROdUPLotrOdUplomiNuSmiNusDuPlodUPLoTRODuPLOtRodUpLodupLOdupLOmINuSMInUsduplOduPloTROdupLoTrODUpLoTRoDuPLOdUPloDUPLODupLoduplodUpLoDUPlOduplODUpLODUpLODUPLoduPLoduPloDUPLodupLoDupLODUPLodUplOdUplODUpLODuPLoDupLodUPLoduPlODuplOduPLO
output :
3645621927936
Upvotes: 0
Views: 136
Reputation: 663
c
in your output example is more than 32 bits. You did define it as long long
which is 64 bits so the program is just fine.
The problem is with the display at the end printf("%d", c);
because %d
is a specifier for numbers up to 32 bits only so any bigger numbers cause an overflow and you might see -ve numbers on the console.
To fix that, you need to write printf
with %ll
specifier printf("%ll", c);
ll
represents long long
and can display up to 64 bits numbers.
Upvotes: 1