### 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;
}
Last edited on
since your do{ is only at the bottom of your code its only looping thu the question.
Put your do { right after your main()
Topic archived. No new replies allowed.