Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The issue I have is when I input a negative number it's show newtons and I don't want it to appear when a negative number is present.

Here is the instructions:
Mass and Weight 
Scientists measure an object’s mass in kilograms and its weight in newtons. If you know
the amount of mass that an object has, you can calculate its weight, in newtons, with
the following formula:  Weight = mass * 9.8  Write a program that asks the user to enter an object’s mass, and then calculates and
displays its weight. If the object weighs more than 1,000 newtons, display a message
indicating that it is too heavy. If the object weighs less than 10 newtons, display a mes-
sage indicating that the object is too light. The program will continue until a negative number is entered for the mass.


This is how my instructor has it
[^]

What I have tried:

#include <iostream>
using namespace std;

int main()
{
double mass, weight;
do
{
   cout << "Enter the object's mass (in kilograms).  A negative mass stops the program.";
cin>>mass;

weight = (mass*9.8);
 cout << "The object weighs " << weight
        << " Newtons.\n";

if(weight>1000){

    cout << "The object is too heavy!\n";
}


else if(weight<10){

      cout << "The object is too light!\n";
}



}while(weight>=0);

   return 0;
}
Posted
Updated 28-Oct-17 14:06pm

1 solution

Learn to indent properly your code, it show its structure and it helps reading and understanding.
C++
#include <iostream>
using namespace std;

int main()
{
  double mass, weight;
  do
  {
    cout << "Enter the object's mass (in kilograms).  A negative mass stops the program.";
    cin>>mass;

    weight = (mass*9.8);
    cout << "The object weighs " << weight
    << " Newtons.\n";

    if(weight>1000){
      cout << "The object is too heavy!\n";
    }
    else if(weight<10){
      cout << "The object is too light!\n";
    }
  }while(weight>=0);
  return 0;
}

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
Quote:
The issue I have is when I input a negative number it's show newtons and I don't want it to appear when a negative number is present.

Simple! You check for negative at wrong place in code.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900