1

If I run the code below it won't display the whole answer (last printf("") is the answer). I would like to know why it doesn't display the whole answer. Does anyone know this problem?

#include <stdio.h>

int main()
{
    int M,T,P,K,H;
    printf("Teekonna pikkus:\n");
    scanf("%u", &P);
    printf("Tarbitud k\u00FCtuse kogus:\n");
    scanf("%u", &K);
    printf("K\u00FCtuse liitri hind:\n");
    scanf("%u", &H);
    T = K/(P/100);
    M = K*H;
    printf("Olete s\u00F5itnud %u", P," kilomeetrit ning kulutanud %u", K," liitrit kütust. Teie auto k\u00FCtusekulu oli %u", T," liitrit 100 kilomeetri kohta. S\u00F5idu maksumus oli %u", M," eurot.");
    while(1);
}
3
  • 1
    Why do you have a while(1) at the end of your program? First of all, lookup printf syntax. Then printf is probably not flushing the string to stdout, try adding a \n at the end of the format string, or add fflush(stdout); after the printf statement. Also, technically your program has undefined behavior because you have a infinite loop.
    – Praetorian
    Commented Jan 10, 2016 at 0:59
  • @Praetorian while(1) is possibly for not to lose the console window. a getch or scanf would be better of course. Commented Jan 10, 2016 at 1:05
  • @SedatKapanoglu You are right, it was written there for that :) Commented Jan 10, 2016 at 1:09

4 Answers 4

7

The printf has the following syntax:

int printf ( const char * format, ... );

the first parameter is the format and all the others variables can substitute placeholders present in the format.

You should write:

printf("Olete s\u00F5itnud %u kilomeetrit ning kulutanud %u liitrit kütust. Teie auto k\u00FCtusekulu oli %u liitrit 100 kilomeetri kohta. S\u00F5idu maksumus oli %u eurot.", P, K, T, M);
2

That isn't the way printf works. Put all the arguments after the format string.

printf("Olete s\u00F5itnud %u kilomeetrit ning kulutanud %u liitrit kütust. Teie auto k\u00FCtusekulu oli %u liitrit 100 kilomeetri kohta. S\u00F5idu maksumus oli %u" eurot.", P, K, T, M);
0
1

There are 2 problems in the code:

  • printf() should be used as: printf("P: %d - T: %d\n", P, T);

  • but there is also the possibility of a division by zero:

T = K/(P/100);

P is an integer, and if P/100 becomes small enough, it will be rounded off to 0. Then K/0 (zero) will create a problem.

So, those values should be checked before division. And it might be better to use (or convert to) float or double for some variables.

2
  • T = K/(P/100); was actually suggested by my teacher so it shouldn't make any problems, but thanks for the info :) Commented Jan 10, 2016 at 1:10
  • @MihkelKull - You can be sure it is a problem :) - You really should check those values, and maybe even use floats instead. (try using P<100 for example)
    – Danny_ds
    Commented Jan 10, 2016 at 1:30
0

printf() output is based on the first string parameter (formatting string), replacing the special % characters ("formatting specifier) in it with the additional parameters following the string.

So it displays only what you've asked to display, because you have only %u (unsigned int, which corresponds to P). The remaining parameters are ignored, as no other formatting specifier can be found in the formatting string

You should concatenate all the formatting strings:

  printf (""Olete s\u00F5itnud %u  kilomeetrit ning kulutanud %u ...%u...%u...", P, K, T, M); 
2
  • Firstly, I think <iostream> (not <iostreams>, by the way) is an absolutely terrible API, for many reasons. And secondly, the question is tagged with c and not c++. The fact that <stdio.h> (or <cstdio>) remains popular even in C++ says a lot, and it's definitely not legacy (even in C++, let alone in C!).
    – Tim Čas
    Commented Jan 10, 2016 at 1:14
  • @TimČas sorry, but it was tagged c++ when I answered. About using printf vs. streams in C++: you may have your opinion on that. It's just that printf() doesn't allow for object orientation as much as streams allow it. Now with a C tag, this is no longer an issue
    – Christophe
    Commented Jan 10, 2016 at 1:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.