1,105,312 Community Members

c++ temperature conversion

Member Avatar
majestic0110
Nearly a Posting Virtuoso
1,310 posts since Oct 2007
Reputation Points: 187 [?]
Q&As Helped to Solve: 80 [?]
Skill Endorsements: 11 [?]
 
0
 

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 temperature_conversion_suite.cpp (2.2KB)
Member Avatar
Ancient Dragon
Achieved Level 70
27,576 posts since Aug 2005
Reputation Points: 5,231 [?]
Q&As Helped to Solve: 3,028 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

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'.

Member Avatar
majestic0110
Nearly a Posting Virtuoso
1,310 posts since Oct 2007
Reputation Points: 187 [?]
Q&As Helped to Solve: 80 [?]
Skill Endorsements: 11 [?]
 
0
 

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.

Member Avatar
majestic0110
Nearly a Posting Virtuoso
1,310 posts since Oct 2007
Reputation Points: 187 [?]
Q&As Helped to Solve: 80 [?]
Skill Endorsements: 11 [?]
 
0
 

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 temperature_conversion_suite.cpp (2.19KB)
Member Avatar
iamthwee
Posting Sage
7,037 posts since Aug 2005
Reputation Points: 1,307 [?]
Q&As Helped to Solve: 592 [?]
Skill Endorsements: 74 [?]
Featured
 
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 6 Years Ago by Ancient Dragon and iamthwee
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article