while...do loop

Hi, first post so slightly nervous!

Very new to C++, but as part of my engineering degree we have to 'scratch the surface'!

I've had to create a programme that allows the user to input 3 different variables (1 stays constant for the purpose of the calculation) the programme then performs a calculation and throws out the answer.

However at the end I need to include a 'do...while' loop where the user can input either a 'Y' to continue and repeat the process obviously with different variables or a 'N' to terminate the programme!

I've put something in, but it just doesn't appear to be working quite right??

The code I've used is as follows (I've put the do..while in bold!);



// Co-Ax-Assignment.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream> // in's and out's
#include <cmath> // maths library
using namespace std; // saves lots of time


int main ()
{
cout <<"Ryan Broomhall - South Cheshire College \n "; // shows my name and where I'm studying

double d; // declaration of the variable d
double D; // declaration of the variable D
double e; // declaration of the constant e
double z; // declaration of the variable z
double number; // declaration of the number

cout << " \n Enter the constant e "; // appears on the screen to tell the user to input the constant for e
cin >> e;

loop_1: // the first loop of the programme
cout << " \n Enter your value for d "; // appears on the screen to tell the user to input a value for d
cin >> d;
if (d <= 0) cout << " \n The value you have entered is negative, please enter a positive value \n"; // tells the user that the value they've inputted is negative and to use a positive number
if (d <= 0) goto loop_1; // takes the user back to the 'enter your value for d' command



loop_2: // the second loop of the programme
cout << " \n Enter your value for D "; // appears on the screen to tell the user to input a value for D
cin >> D;
if (D <= 0) cout << " \n The value you have entered is negative, please enter a positive value \n"; // tells the user that the value they've inputted is negative and to use a positive number
if (D <= 0) goto loop_2; // takes the user back to the 'enter a value for D' command





number = D / d; // specifies that it is the value chose for 'D' divided by the value chosen for 'd'

z = (138 / sqrt (e)) * (log10 (number)); // The equation that is used to calculate the characteristic impedance of co-axial cable

cout << " \n The characteristic impedance of the coax cable is "<<z<<" ohms \n"; // appears on the screen to tell the user what the characteristic impedance of co-axial cable is based on the values they have chosen

char ans;

do

{
cout<< " \n Do you want to continue with another calculation (Y/N)?\n";
cout<< " \n You must type a 'Y' or an 'N'.\n";
cin>>ans;
}

while((ans !='Y')&&(ans !='N')&&(ans !='Y')&&(ans !='n'));


















return 0;
}
Hint - Do you only want to ask the question over and over and over? Or do you want to perform the entire calculation over and over and over?

Hope this helps.
Hi mate, thanks for the reply!

Basically I want to be able to get a value, then to be able to repeat the whole calculation again if I want!

I.E-

...... ohms

Do you wish you perform a new caluclation (Y/N)

You must type 'Y' or 'N'

A Y selection would take you back to the beginning, a N selection would end the programme!

Sorry if that's hard to make sense of...as I said I'm a complete newbie to all this!!!!

Thanks in advance!!!
Theres a few lil things you need to read up on Ryan so that you can clean up your code a little bit. Firstly: http://www.cplusplus.com/doc/tutorial/control/ << this is a must read. You can use examples in that tutorial that will rid you of bad code like loop_1: etc.

Secondly, asfter you use cin >> use cin.ignore(); on the line after otherwise you will have problems with your prgoram. If you need further details then just google the use of it and you will get your answers.

That link i gave you should cover everything you need to know for this program.
Hint**a do...while loop will be helpful for your last post.
put the do{ at the top of your code for which you would like to repeat from, the do while will always go through once then if the condition is true it will repepat
You could do this:

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
 #include "stdafx.h"
#include <iostream> // in's and out's
#include <cmath> // maths library
using namespace std; // saves lots of time


int main ()
{

do
{
char ans;
cout <<"Ryan Broomhall - South Cheshire College \n "; // shows my name and where I'm studying

double d; // declaration of the variable d
double D; // declaration of the variable D
double e; // declaration of the constant e
double z; // declaration of the variable z
double number; // declaration of the number

cout << " \n Enter the constant e "; // appears on the screen to tell the user to input the constant for e
cin >> e;

loop_1: // the first loop of the programme
cout << " \n Enter your value for d "; // appears on the screen to tell the user to input a value for d
cin >> d;
if (d <= 0) cout << " \n The value you have entered is negative, please enter a positive value \n"; // tells the user that the value they've inputted is negative and to use a positive number
if (d <= 0) goto loop_1; // takes the user back to the 'enter your value for d' command



loop_2: // the second loop of the programme
cout << " \n Enter your value for D "; // appears on the screen to tell the user to input a value for D
cin >> D;
if (D <= 0) cout << " \n The value you have entered is negative, please enter a positive value \n"; // tells the user that the value they've inputted is negative and to use a positive number
if (D <= 0) goto loop_2; // takes the user back to the 'enter a value for D' command





number = D / d; // specifies that it is the value chose for 'D' divided by the value chosen for 'd'

z = (138 / sqrt (e)) * (log10 (number)); // The equation that is used to calculate the characteristic impedance of co-axial cable

cout << " \n The characteristic impedance of the coax cable is "<<z<<" ohms \n"; // appears on the screen to tell the user what the characteristic impedance of co-axial cable is based on the values they have chosen

cout<< " \n Do you want to continue with another calculation (Y/N)?\n";
cout<< " \n You must type a 'Y' or an 'N'.\n";
cin>>ans;
} while((ans !='Y')&&(ans !='N')&&(ans !='Y')&&(ans !='n'));

return 0;
}


From the way you spell program, you are English? So am I, but for some reason I always spell programme as program. Most programmers are/speak US English so except for in my comments I always spell things the American way (color rather than colour, for example). Once in an English essay after I learned HTML I caught myself spelling colour as 'color'. >:(
Last edited on
Hi, yes I'm English- it's just poor spelling I guess LOL!

Again, I apologise for the daft questions. The problem is that my degree is in Electrical and Electronic Engineering and my heart lies with electrical principles and power. However we have to do a programming and microproccesors module...and I just can't get my head around it LOL!!!

chrisname- thanks for the help, I've modified my code, however it's throwing up the following errors;



1>------ Build started: Project: Coaxial_Cable_Assignment_3, Configuration: Debug Win32 ------
1>Compiling...
1>Coaxial_Cable_Assignment_3.cpp
1>c:\users\ryan\documents\visual studio 2008\projects\coaxial_cable_assignment_3\coaxial_cable_assignment_3\coaxial_cable_assignment_3.cpp(24) : error C2371: 'ans' : redefinition; different basic types
1> c:\users\ryan\documents\visual studio 2008\projects\coaxial_cable_assignment_3\coaxial_cable_assignment_3\coaxial_cable_assignment_3.cpp(15) : see declaration of 'ans'
1>c:\users\ryan\documents\visual studio 2008\projects\coaxial_cable_assignment_3\coaxial_cable_assignment_3\coaxial_cable_assignment_3.cpp(60) : error C2088: '>>' : illegal for class
1>c:\users\ryan\documents\visual studio 2008\projects\coaxial_cable_assignment_3\coaxial_cable_assignment_3\coaxial_cable_assignment_3.cpp(63) : error C2065: 'ans' : undeclared identifier
1>c:\users\ryan\documents\visual studio 2008\projects\coaxial_cable_assignment_3\coaxial_cable_assignment_3\coaxial_cable_assignment_3.cpp(63) : error C2065: 'ans' : undeclared identifier
1>Build log was saved at "file://c:\Users\Ryan\Documents\Visual Studio 2008\Projects\Coaxial_Cable_Assignment_3\Coaxial_Cable_Assignment_3\Debug\BuildLog.htm"
1>Coaxial_Cable_Assignment_3 - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Last edited on
closed account (z05DSL3A)
It is not quite as simple as moving the do{ to the top of the code.

This code should do what you want, I will leave it to you to neaten it up. You do need to address the use of cin, the program will go tits up if you enter incorrect data. I would suggest reading Using cin to get user input. http://www.cplusplus.com/forum/articles/6046/

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Co-Ax-Assignment.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream> 
#include <cmath> 
using namespace std; 


int main ()
{
    cout << "Ryan Broomhall - South Cheshire College" << endl; 

    double d; 
    double D; 
    double e; 
    double z; 
    double number; 

    bool finished = false;
    do //while not finished
    {
        cout << " \n Enter the constant e "; 
        cin >> e;

        do
        {
            cout << " \n Enter your value for d "; 
            cin >> d;
            if (d <= 0)
            {
                cout << " \n The value you have entered is negative, please enter a positive value \n";
            }
        } while(d <= 0); 


        do
        { 
            cout << " \n Enter your value for D "; 
            cin >> D;
            if (D <= 0) 
            {
                cout << " \n The value you have entered is negative, please enter a positive value \n";
            }
        }while (D <= 0); 

        //
        number = D / d; 

        z = (138 / sqrt (e)) * (log10 (number)); 

        cout << " \n The characteristic impedance of the coax cable is "<<z<<" ohms \n"; 

        char ans;
        do
        {
            cout<< " \n Do you want to continue with another calculation (Y/N)?\n";
            cout<< " \n You must type a 'Y' or an 'N'.\n";
            cin>>ans;
            
        }while((ans !='Y')&&(ans !='N')&&(ans !='y')&&(ans !='n'));

        if((ans =='N') ||(ans =='n'))
            finished = true;
    
    }while(!finished);

    return 0;
}
Topic archived. No new replies allowed.