0
I am making a clock simulator program. However, when printing the result, I am having errors.
Here’s how I do it:
float abc = 1:00 + 2:00 ;
printf("horario: %f",abc);
How do I then make and print a string with special characters?
0
I am making a clock simulator program. However, when printing the result, I am having errors.
Here’s how I do it:
float abc = 1:00 + 2:00 ;
printf("horario: %f",abc);
How do I then make and print a string with special characters?
1
There are several types of data abstraction. In C, for example, we have two types that fit the question: float
and char
(in jail or array).
The guy float
stores numbers in floating point. Switching on kids, numbers with decimals (something comma something else). Therefore, it does not store different characters of digits - and, in a way, the "point" which finally indicates the decimal places (here we use the comma but, when programming, we adopt the English standard).
The guy char
stores 1 character any data the limits of both compiler and operating system. However, you can create a string (array) of char
which simulates the concept of string.
Remember, guys can’t do each other’s operations using the same symbols. That is: you can "add up" two numeric characters but that does not mean adding up two numbers. It means concatenate (or join). Examples:
'a' + 'b'
results in "ab"
'1' + '1'
does not result in 2
and yes in "11"
.Considering your code
float abc = 1:00 + 2:00 ;
printf("horario: %f",abc);
You are just trying to add up something that is not part of what the float type understands: the two dots :
. You must first sum up the hours and only then turn into string as appropriate - recital C, C++ and Rust.
For example, in C, one could consider:
int hora_1 = 1;
int hora_2 = 20;
int soma_hora = hora_1 + hora_2;
printf("Horário: %d:00", %soma_hora);
In this example, I summed up the hours. Note that the two points were in charge of the printf
since there is no need to deal with it for hours calculations.
Was used int
for the float
is unnecessary in this example - and perhaps it is more interesting to divide the minutes and hours to make work less complex. It was not taking into account the minutes because it is interesting to think about how to connect with cases where the sum is, for example, equal to 61 - even if it is not necessary, it is interesting from the technical point of view for those who are learning.
Your example would look like this:
float abc = 1 + 2 ;
printf("horario: %f:00", abc);
If you are actually going to treat the hours with "two points" - that is, with strings - I will suggest you read about the functions that deal with this in the language of your choice. In C/C++, you can look on this website for possible utilities. And the theory gets here and here. And of course, it’s worth always searching Google for other ways that might have more effect on your understanding.
Browser other questions tagged c string
You are not signed in. Login or sign up in order to post.
vlw friend " solved
– user132866
@Gustavoalves is no problem! If the response was adequate and solved, you can mark it with the "right" sign right up to the top of the answer so it doesn’t stay open.
– José