Hi all, Just wondering if you could help me out here. bought a copy of c++ for dummies today and the first program in the book converts fahrenheit to celsius. Now I have some C and Java experience (I am a recent graduate of a software design degree) but have not touched C++ before. I have modified the conversion program from the c++ for dummies book so that it has a user interface and is capable of converting celsius to fahrenheit. The problem is, after a conversion is made, it reverts back to the main menu (expected), but displays it twice (unexpected). I presume the issue is that there is input left in the stream but cannot find out how to clear it so that the menu is only displayed once, not twice. Any help would be greatly appreciated.

Attachments
``````// Celsius to Fahrenheit Conversion Program.
// Phillip Wells.
// If C = 20, Factor is equal to 180.
// C x F / 100 +32.
// 3600 / 100 +32.
// 36 + 32 = 68F.

#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;
int ctof(void);
int ftoc(void);

int main()
{
char option;

do{

printf("\nWelcome to the Temperature Converter\n");
printf("\n");
printf("1 - Convert Celsius to Fahrenheit\n");
printf("2 - Convert Fahrenheit to Celsius\n");
printf("q - quit\n");
scanf("%c",&option);

switch(option){
case '1':
ctof();
break;

case '2':
ftoc();
break;

case 'q':
break;

default:
printf("\n");
break;
}
}while(option!='q');

return 0;
}

int ctof(void)
{
// enter the temperature in Celsius

double celsius;
cout << "Enter the temperature in Celsius:";
cin >> celsius;
// calculate conversion factor for Celsius
// to Fahrenheit

//int factor;
//  factor = 212 - 32;
// use conversion factor to convert Celsius
// into Fahrenheit. values F = (C x 1.8) + 32
// C = (F- 32) / 1.8
//
double fahrenheit;
fahrenheit =  celsius *1.8 +32;
// output the results (followed by a NewLine)
cout << "Fahrenheit value is:";
cout << fahrenheit << endl;
// wait until user is ready before terminating program
// to allow the user to see the program results
system("PAUSE");
return 0;
}

int ftoc(void)
{
// enter the temperature in Celsius

double fahrenheit;
cout << "Enter the temperature in Fahrenheit:";
cin >> fahrenheit;
// calculate conversion factor for Celsius
// to Fahrenheit

//int factor;
//  factor = 212 - 32;
// use conversion factor to convert Celsius
// into Fahrenheit. values F = (C x 1.8) + 32
// C = (F- 32) / 1.8
//
double celsius;
celsius =  (fahrenheit-32)/1.8 ;
// output the results (followed by a NewLine)
cout << "Celsius value is:";
cout << celsius << endl;
// wait until user is ready before terminating program
// to allow the user to see the program results
system("PAUSE");
return 0;
}``````

## Featured Replies in this Discussion

• First, the program you posted is a #######ization of C and C++ languages. As for your problem you need to flush the '\n' (Enter key) from the keyboard buffer after the sscanf(). There are several ways to do that but in C the easiest is to call getc() to remove the '\n'.

First, the program you posted is a #######ization of C and C++ languages. As for your problem you need to flush the '\n' (Enter key) from the keyboard buffer after the sscanf(). There are several ways to do that but in C the easiest is to call getc() to remove the '\n'.

hi, thanks for the (very) swift reply! Am I correct in thinking that the menu (int main) block is C and the rest is C++, because I just whacked that menu system in there from an old C file I had? Thanks again for your reply.

hi there Ancient Dragon, been tinkering around with the code somewhat, could you let me know if it is now just in c++ (as mixing languages is not good, I hear) thanks for your help with this.

Attachments
``````// Celsius to Fahrenheit Conversion Program.
// Phillip Wells.
// If C = 20, Factor is equal to 180.
// C x F / 100 +32.
// 3600 / 100 +32.
// 36 + 32 = 68F.

#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;
int ctof(void);
int ftoc(void);

int main()
{
char option;

do{

cout << "\nWelcome to the Temperature Converter\n";
cout << "\n";
cout << "1 - Convert Celsius to Fahrenheit\n";
cout << "2 - Convert Fahrenheit to Celsius\n";
cout << "q - quit\n";
cin  >> option;

switch(option){
case '1':
ctof();
break;

case '2':
ftoc();
break;

case 'q':
break;

default:
cout << "\n";
break;
}
}while(option!='q');

return 0;
}

int ctof(void)
{
// enter the temperature in Celsius

double celsius;
cout << "Enter the temperature in Celsius:";
cin >> celsius;
// calculate conversion factor for Celsius
// to Fahrenheit

//int factor;
//  factor = 212 - 32;
// use conversion factor to convert Celsius
// into Fahrenheit. values F = (C x 1.8) + 32
// C = (F- 32) / 1.8
//
double fahrenheit;
fahrenheit =  celsius *1.8 +32;
// output the results (followed by a NewLine)
cout << "Fahrenheit value is:";
cout << fahrenheit << endl;
// wait until user is ready before terminating program
// to allow the user to see the program results
system("PAUSE");
return 0;
}

int ftoc(void)
{
// enter the temperature in Celsius

double fahrenheit;
cout << "Enter the temperature in Fahrenheit:";
cin >> fahrenheit;
// calculate conversion factor for Celsius
// to Fahrenheit

//int factor;
//  factor = 212 - 32;
// use conversion factor to convert Celsius
// into Fahrenheit. values F = (C x 1.8) + 32
// C = (F- 32) / 1.8
//
double celsius;
celsius =  (fahrenheit-32)/1.8 ;
// output the results (followed by a NewLine)
cout << "Celsius value is:";
cout << celsius << endl;
// wait until user is ready before terminating program
// to allow the user to see the program results
system("PAUSE");
return 0;
}``````

>(as mixing languages is not good, I hear)

That is, in fact debatable. C++ is considered a multi-paradigm compiled language, ho ho ho.

Like anything, if you don't know what you are doing, you can fubar it.

Question Answered as of 7 Years Ago by Ancient Dragon and iamthwee
Isn't it about time forums rewarded their contributors?

Earn rewards points for helping others. Gain kudos. Cash out. Get better answers yourself.

It's as simple as contributing editorial or replying to discussions labeled or OP Kudos

You