help with if/else...

closed account (zT7X92yv)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    double speed, time; 
    bool distance, choice;
    cout<<"Please Select:";
    cout<<"\n 1 for Air";
    cout<<"\n 2 for Water";
    cout <<"\n 3 for Steel"<<endl;
    cin>>choice;
    cout<<"enter distance"<<endl;
    cin>>distance;
     
    if (distance < 0) 
    cout<<"enter distance that is greatater than zero";  
    
    if (choice == 1)
    speed=1100;
    else if (choice == 2)
    speed=4900;
    else if (choice ==3)
    speed=16400;
     
     time=distance/speed;
     cout<<fixed<<setprecision(4)<<"time is  "<<time<<" sec"<<endl;
 
    system("PAUSE");
    return EXIT_SUCCESS;
}



I want...
1
2
if (distance < 0) 
    cout<<"enter distance that is greatater than zero"; 

to go back to cout<<"enter distance"<<endl; if possible.

Great Thanks in advance!
http://www.cplusplus.com/doc/tutorial/control/

Look for do{...} while (...);
closed account (zT7X92yv)
I want something similar to this (logically)

if (distance > 0)

then I get to use

1
2
3
4
5
6
if (choice == 1)
    speed=1100;
    else if (choice == 2)
    speed=4900;
    else if (choice ==3)
    speed=16400;


otherwise I go back to entering distance > 0;
1
2
3
 else 
cout<<"enter distance that is greater than zero";
 cin >>distance; 
Look into switches, too. You may have to nest (put one thing inside of another) some things.
closed account (zT7X92yv)
1
2
3
4
5
if ( distance < 0)
    cout << "enter distance that is greater than zero";
    cin>>distance;
    else (distance >0)
    cin>>choise;


not working.

also, hwo do I
nest (put one thing inside of another) some things.
I use { and } between if/else?

thanks!
Last edited on
1
2
3
4
5
do { // Loop
    if (...) { // If is nested inside of the do loop.
        ...
    }
} while (...);
closed account (zT7X92yv)
Thanks!
Topic archived. No new replies allowed.