1

Just started programming with Turbo C++. Why does this simple program not display the additional cprintf functions after I input a value?

#include <stdio.h>
#include <conio.h>

main()
{

 float Speed;
 Speed =0;

 clrscr();

 textcolor(YELLOW);
 cprintf("Speed: ");
 cscanf("%f",&Speed);

 if(Speed>60){
  textcolor(RED+BLINK);
  cprintf("Overspeeding");
  }
 else{
  textcolor(GREEN+BLINK);
  cprintf("Normal Speed");
  }

 getch();
 return 0;
}

Since I just started programming, I really don't know if my code is easily readable to others. Please advise me on some basic coding practices.

PHD6N2
  • 19
  • 3
  • 1
    I don't know the answer to your question, but a suggestion for readability: if you have more than one expression in the body of an `if` or `else`, it's more common to enclose them in curly braces (`{ }`), instead of separating them with a comma: `if (...) { textcolor(); cprintf(); }` (Usually you'd write one expression per line.) – legoscia Mar 08 '16 at 14:34
  • do you get the output when you use printf? – Umamahesh P Mar 08 '16 at 14:53
  • Remove the calls to `textcolor` and show what output you get for a given input. Also your code won't compile because ther is an `"` missing after `"%d"` here: `cscanf("%f,&Speed);`. Did you retype your code in stackoverflow?? – Jabberwocky Mar 08 '16 at 14:56
  • 3
    Possible duplicate of [Why does printf not flush after the call unless a newline is in the format string?](http://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin) – Karoly Horvath Mar 08 '16 at 14:56
  • Note: `getch();` simply consume the leftover `\n` from `cscanf("%f,&Speed);` and the exits the program. With buffered output, final output (lacking a \n) is not displayed. – chux - Reinstate Monica Mar 08 '16 at 14:57
  • Yes, i did retype my code in stackoverflow. I just joined and still reading the formatting and other guidelines. – PHD6N2 Mar 08 '16 at 14:59
  • 1
    @JerikoDP: Always copy & paste your code *exactly*. Retyping means we might point out problems not existing in your program, or miss problems that *are* existing there, because we are looking at a *different* program. – DevSolar Mar 08 '16 at 15:03
  • Since I am not on Windows ATM, I culled this program to standard C by dropping `#include `, `clrscr()`, `textcolor()`, and `getch()` as well as using `printf()` and `scanf()` instead of their `c*` versions -- something you should do as well because standard trumps platform when still learning things. I also fixed the typo of course. That leaves me with a perfectly functional program. Perhaps the lack of a newline at the end of the `printf()` is confusing you? The text is immediately followed by the command prompt, i.e. output and new input prompt are on the same line. – DevSolar Mar 08 '16 at 15:22

0 Answers0