Scanf function not working as it is supposed to be

Hi,
In the following program that converts temperature in Celsius into Fahrenheit, I am trying to use the Scanf(). The program works fine with the cin function, but is not working properly with Scanf. I did some debugging, and realized that the numbers read by the scanf function are not correct.

I tried fflush()but it did not work. What am I doing wrong?

While I am new to C++, I have done programming in VBA and 8086 assembly language.

Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
double number1=0;
double number2=0;
double number3=0;
double number4=0;

//cout << "Please give in a lower limit, limit >= 0:";
printf( "Please given in a lower limit, limit >= 0:");

//cin >> number1;
scanf( "%f", &number1);
//fflush(stdin);

//cout << "Please give in a higher limit, 10 > limit <= 50000:";
printf( "Please give in a higher limit, 10 > limit <= 50000:");

//cin >> number2;
scanf( "%f", &number2);
//fflush(stdin);

//cout << "Please give in a step, 0 < step <= 10:";
printf( "Please give in a step, 0 < step <= 10:");

//cin >> number3;
scanf( "%f", &number3);
//fflush(stdin);


//cout << "Celsius       Fahrenheit"<< endl;
printf( "\nCelsius\t\t\Farenheit");
printf( "\n--------\t---------\n");
//cout << "-------       ----------"<< endl;

while (number1 < number2) {
   //cout << number1 << "            " <<(number4= number1 * 9/5 +32)<< endl;

   printf("%f\t%f\n", number1, (number4 = number1 * 9/5 +32) );

   number1=number1+number3;
}


getchar();
    return 0;
You are using doubles, so you must scanf( "%lf", &foo );.

(But you have it correct for printf().)
Thanks Duoas.
It works like a charm.

I have a follow up question if you don't mind.

When I use the exe file of this program, it displays the results for a fraction of second and closes. It seems the getchar(); is not doing its job.
Any idea why it is happening?

P.S. I am using Code::blocks.

Thanks.
Last edited on
Because there was an unread character still in the input by the time your program got to line 48.

Remember, all input comes as a stream, or sequential list, of characters.

Imagine the following program, that produces output (in normal characters) and takes the input (in italics):
C:\Prog\foo> a.exe
What is your name? Mr Bean
How old are you? 58
Hello Mr 58.
C:\Prog\foo> _

Let's examine that carefully:

Output: "What is your name? "
Input "Mr Bean" + ENTER KEY
Output: "How old are you? "
Input "58" + ENTER KEY
Output: "Hello Mr 58.\n"

If you were to stick it all together, the input would be a string:

    "Mr Bean\n58\n"

(The ENTER key becomes a newline '\n' in the input.)


Here's the trick.

When you read a string with std::getline(), it reads and throws away the user's ENTER KEY/newline.

But when you read an integer with something like cin >> n;, it does not throw anything away. That ENTER KEY/newline is still unread in the input.

So, here we get to the last line of your program, which asks to read a single character from the input. The input has an unread ENTER KEY/newline sitting there, just waiting. It gets read. Program ends.


This is why you must never forget that the user will always press ENTER at the end of every input. Which means that you must be careful to properly synchronize your inputs so that things like a misplaced newline or any other garbage the user enters is not left behind in the stream.

The fflush(stdin); you had there was an attempt to fix that. Unfortunately, it is not valid C or C++, and even if it works for your compiler (it will if you use Microsoft's), it probably won't for someone else's compiler (like your teacher's).

So you must scan for the newline yourself.

1
2
3
4
5
6
7
8
// Obtain final input
scanf("%lf", &number3);

// Extract everything to the end of line, including the newline/ENTER key.
while (getchar() != '\n') ;

// At this point, we know that user's input is properly synchronized 
// with the beginning of the next line of input from the user. 

And, of course, you can now use that getchar() at the end of your program, because there won't be any unread characters waiting in the user's input -- so the computer will wait for the user to give it some.

Hope this helps.
Thanks a lot Duoas.
Topic archived. No new replies allowed.