break

R

Richard

The program I write ask two questions. If user enter of the question invalid
or out of range, then the program will exit. My code cannot do that with a
break. Can anyone take a look and help.


#include <iostream>

#include <iomanip>

using namespace std;

void main()

{

double Weight, Distance, Charge;

// ask for information

cout<<"How many Kg is the package weighted? : ";

cin>>Weight;

if (Weight < 0 || Weight > 20)

{

cout<<"We do not ship package weight 0 or below, and more than 20
kg."<<endl;

break;

}


cout<<endl;

cout<<"How many miles is the packaged shipped? :";

cin>>Distance;

if (Distance < 10 || Distance > 3000)

{

cout<<"We do not ship package in a distance of less than 10 or more than
3000 miles";

break;

}




cout.setf(ios::fixed |ios::showpoint);

cout.precision(2);

if (Weight <= 2)

{

Charge = (Weight * 1.10)* (Distance/500);

cout<<"The total cost for this package to be shipped is:
"<<Charge<<endl<<endl;

}

if (Weight > 2 && Weight <= 6)

{

Charge = (Weight * 2.20)* (Distance/500);

cout<<"The total cost for this package to be shipped is:
"<<Charge<<endl<<endl;

}

if (Weight > 6 && Weight <= 10)

{

Charge = (Weight * 3.70)* (Distance/500);

cout<<"The total cost for this package to be shipped is:
"<<Charge<<endl<<endl;

}

if (Weight > 10 && Weight <=20)

{

Charge = (Weight * 4.80)* (Distance/500);

cout<<"The total cost for this package to be shipped is:
"<<Charge<<endl<<endl;

}

else

{

cout<<"You enter invalid data. Please try again"<<endl;


}



}
 
M

mlimber

Richard said:
The program I write ask two questions. If user enter of the question invalid
or out of range, then the program will exit. My code cannot do that with a
break. Can anyone take a look and help.


#include <iostream>

#include <iomanip>

using namespace std;

void main()

int main()
{

double Weight, Distance, Charge;

// ask for information

cout<<"How many Kg is the package weighted? : ";

cin>>Weight;

if (Weight < 0 || Weight > 20)

{

cout<<"We do not ship package weight 0 or below, and more than 20
kg."<<endl;

break;

return -1;
}


cout<<endl;

cout<<"How many miles is the packaged shipped? :";

cin>>Distance;

if (Distance < 10 || Distance > 3000)

{

cout<<"We do not ship package in a distance of less than 10 or more than
3000 miles";

break;

return -2;
}




cout.setf(ios::fixed |ios::showpoint);

cout.precision(2);

if (Weight <= 2)

{

Charge = (Weight * 1.10)* (Distance/500);

cout<<"The total cost for this package to be shipped is:
"<<Charge<<endl<<endl;

}

if (Weight > 2 && Weight <= 6)

{

Charge = (Weight * 2.20)* (Distance/500);

cout<<"The total cost for this package to be shipped is:
"<<Charge<<endl<<endl;

}

if (Weight > 6 && Weight <= 10)

{

Charge = (Weight * 3.70)* (Distance/500);

cout<<"The total cost for this package to be shipped is:
"<<Charge<<endl<<endl;

}

if (Weight > 10 && Weight <=20)

{

Charge = (Weight * 4.80)* (Distance/500);

cout<<"The total cost for this package to be shipped is:
"<<Charge<<endl<<endl;

}

else

{

cout<<"You enter invalid data. Please try again"<<endl;


}

return 0;
 
R

Richard

i am a newbie to C++. can you offer some detail and explaination? It won't
work with the code you gave me.
 
M

mlimber

Richard said:
i am a newbie to C++. can you offer some detail and explaination? It won't
work with the code you gave me.

Your original post was exceedingly unclear due to grammatical mishaps.
Please restate the problem more clearly, and we'll see what we can do.
I was guessing that you simply wanted to exit in the places where you
had break statements, and in that case, you should simply return from
the main function, which is what the modified code should do if you
*replace* the break statements with return statements.

BTW, it's conventional to indent programs so they are more legible. For
example:

#include <iostream>
using namespace std;

int main()
{
bool done = false;
while( !done )
{
cout << "Enter a number between 1 and 10: " << flush;
int i;
cin >> i;
if( i < 1 || i > 10 )
{
cerr << "\nBad value. Try again." << endl;
}
else
{
done = true;
cout << "\nDouble that number is " << 2*i << endl;
}
}
return 0;
}

Cheers! --M
 
R

Richard

If I do a reture 0; , then the program will ask me a question. However, no
matter how I entered the data, valid or inavalid, the program will exit. I
do not know why. Please help again.
 
K

Karl Heinz Buchegger

Richard said:
If I do a reture 0; , then the program will ask me a question. However, no
matter how I entered the data, valid or inavalid, the program will exit.

Fire up your debugger(*) and step through the program.
Then you will know why your program behaves the way it does. This
knowledge then enables you to fix your thinking and implement it the
way it should be.



(*) A 'debugger' is a special program that usually comes with your development
system. The job of a debugger is to control the actual running program. The debugger
is able to 'single step' through your code, that is: one statement at a time, to
give you a look at the variables as they change, etc ....
You will need to learn how to operate your debugger, since those 2 (your editor
and the debugger) are usualy those programs you spend most time with in your
development effort.
 
K

Karl Heinz Buchegger

Karl said:
Fire up your debugger(*) and step through the program.
Then you will know why your program behaves the way it does. This
knowledge then enables you to fix your thinking and implement it the
way it should be.

(*) A 'debugger' is a special program that usually comes with your development
system. The job of a debugger is to control the actual running program. The debugger
is able to 'single step' through your code, that is: one statement at a time, to
give you a look at the variables as they change, etc ....
You will need to learn how to operate your debugger, since those 2 (your editor
and the debugger) are usualy those programs you spend most time with in your
development effort.

Oh, I forgot.
If you don't have a debugger or don't want to use it (which is a bad idea anyway)
then you can always insert output statements in your code, to see what is going on.
That worked for programmers since more then 40 years, so it will work for you too.

The first thing you should *always* check is if the program or function really gets
the input you expect it to get. That includes input statements also. So whenever you
have an input, output it immediatly to see if the numbers (or texts) are fine:

Example:

cout << "How many Kg is the package weighted? : ";
cin >> Weight;

cout << "You entered a weight of " << Weight;

Once your program runs as expected, you cal always remove those additional
output statements.
 
J

Jaspreet

Richard said:
The program I write ask two questions. If user enter of the question invalid
or out of range, then the program will exit. My code cannot do that with a
break. Can anyone take a look and help.


#include <iostream>

#include <iomanip>

using namespace std;

void main() This should be - int main()

{

double Weight, Distance, Charge;

// ask for information

cout<<"How many Kg is the package weighted? : ";

cin>>Weight;

if (Weight < 0 || Weight > 20)

{

cout<<"We do not ship package weight 0 or below, and more than 20
kg."<<endl;

break;

break is used to move out of a loop or a switch-case construct. It
would have no effect in an if condition. The solutions suggested above
that is usage of return should solve your problem.
[snip rest of the program]
 
H

Howard

Richard said:
The program I write ask two questions. If user enter of the question
invalid or out of range, then the program will exit. My code cannot do
that with a break. Can anyone take a look and help.
else
{
cout<<"You enter invalid data. Please try again"<<endl;
}
}


This code and your question disagree. Your question says it should exit if
there is invalid data entered. But the statement above suggests they will
be allowed to try again. You need to be very specific what the program
should do.

After reporting either the calculation results or one of those error
messages, you need to know whether the program is supposed to exit, or
whether it is supposed to loop back and allow them to enter the data again.
If it is supposed to exit in a given case, then you can use break to exit
the loop.

But first, you need to look up the "while loop" construct in your books, and
learn how to use it. There is no loop in your program, so it will always
just drop out the bottom and exit.

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,301
Latest member
BuyPureganics

Latest Threads

Top