Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 557231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:00:16+00:00 2026-05-13T12:00:16+00:00

I’ve only just recent began learning C++ and am having a little issue with

  • 0

I’ve only just recent began learning C++ and am having a little issue with while loops when the condition for the while loop is an input, of type double, from the user. I understand that if the user doesn’t enter a value compatible with the double type then the loop is automatically broken. The issue is my console application exits upon entering anything other than a double.

The current exercise I’m working on has asked to make use of a while loop and if statements to calculate a number of entered measurements and output the total. The user enters a value and then the measuring system (i.e 25 m for 25 metres). It also has to record and output the highest and lowest values entered.

double value = 0;
double total = 0;
double high = 0;
double low = 0;
string unit = " ";

while (cin >> value >> unit && unit != "convert")
{
if (unit == "in") {total = total+(value*2.54);// in = inches converting to cm
        if (value*2.54 > high) high = value;
        if (value*2.54 < low) low = value;
        }

else if (unit == "m"){total = total+(value*100);// m = metres, converting to cm
        if (value*100 > high) high = value;
        if (value*100 < low) low = value;
        }

else if (unit == "ft"){total = total+(value*30.48);// ft = feet, converting to ft 
        if (value*30.48 > high) high = value;
        if (value*30.48 < low) low = value;
        }
else if (unit == "cm"){total = total+value;// cm = centremetres 
        if (value*2.54 > high) high = value;
        if (value*2.54 < low) low = value;
        }

else cout << "Unable to calculate unit type - " << unit << endl;

value = 0;
}

cout << "Total length in centre-metres: " << total << endl
     << "Total length in metres: " << total/100 << endl
     << "Total length in feet: " << total/30.48 << endl
     << "Total length in inches: " << total/2.54 << endl
     << "\nHighest value: " << high << ", Lowest value: " << low << endl;


keep_window_open();
return 0; 

keep_window_open() is part of the custom header file that came with my C++ book, all it does is ask for a user to enter a character to quit.

I know the current state of the program is a bit bloated but I wanted to get it all working correctly first. I had to use a work around of, if the user enters ‘convert’ as a unit the loop is killed so I could actually see if it was working correctly. The program does finish/complete its task when anything other than an double is entered into value but the results flash up on the console window and then it exits immediately.

Any help is appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:00:17+00:00Added an answer on May 13, 2026 at 12:00 pm

    Your problem is that when you say:

    cin >> value
    

    and enter anything other than a double, the stream goes bad, because value is expecting a double. Your keep_window_open() function also fails, because the stream is still bad.

    There are two ways round this:

    • Run your program from an existing command line prompt window – this will stay open after the program ends. This is the best general purpose solution.

    • Clear the stream before using your “pause” code. Unfortunately, the endline you typed will still be in the buffer. Replace your “pause” code with:

    _

    string line;
    if ( ! cin ) {
        cin.clear();
        getline( cin, line );
    }
    getline( cin, line );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I need to clean up various Word 'smart' characters in user input, including but
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.