I have a c++ problem regarding looping. I am writing a program which will include a problem similar to below.

1) ok, i am writing a program that asks the user to enter a number.

2) Then ask the user if the number entered was correct.

3) If the number is correct, the user will enter 'y' or 'yes' and the program will terminate.
(pretty simple so far ?

4) If the user enters 'n' or 'no' the program should loop back to the beginning of the program and start again. goes back to 1)

5) However, if the user enters anything other than 'yes' or 'no', the program rejects this input and tells the user to re-enter, using only 'yes' or 'no'.

6) This should be asked infinitely until either 'yes' or 'no' is inputted, after which the program then loops back to the beginning.
how would i do this ?

i've been trying do while and switch but no luck. would i have to use a for loop ?

Thanks

A do while combined with a switch sounds correct. What is your while condition? It should probably be something like:

char c;
do{
...switch...
}
while (c!='y');

(but adding an OR (||) to the while to account for an entry of "yes")

oh, and just so you know, for loops are typically used when you want to carry out an operation a specified number of times. Here, as you can probably see, that is not the case as it could be as many times as the user doesn't input 'y'.

Edited 6 Years Ago by maddav: re-read first post, corrected code

A do while combined with a switch sounds correct. What is your while condition? It should probably be something like:

char c;
do{
...switch...
}
while (c!='y');

(but adding an OR (||) to the while to account for an entry of "yes")

oh, and just so you know, for loops are typically used when you want to carry out an operation a specified number of times. Here, as you can probably see, that is not the case as it could be as many times as the user doesn't input 'y'.

#include <iostream.h>
#include <string>
#include <conio.h>

main()
{
	string date;
   char answer; //either yes or no
   bool check;

do	{  //beginning of loop********************************
		cout << "\nplease enter the date in dd/mm/yyyy: "; 
      cin >> date;

      cout << "\nIs this the correct date ? press y/n \n" << date << "\n\n";
      cin >> answer;

      switch (answer) {
      	case 'y':
         	cout << "\nThank you!!\n";
            break;

      }while (answer != 'y');
   }
}

why does this not compile ?
btw, i am using borland 5.03 and HAVE to use it.

Info :Compiling E:\program\noname00.cpp
Warn : string.h(549,3):Functions containing for are not expanded inline
Warn : string.h(557,3):Functions containing while are not expanded inline
Warn : string.h(563,3):Functions containing for are not expanded inline
Warn : string.h(575,3):Functions containing for are not expanded inline
Warn : string.cc(686,32):Comparing signed and unsigned values
Warn : string.cc(658,22):Cannot create pre-compiled header: code in header
Warn : noname00.cpp(7,14):Use qualified name to access member type 'std::string'
Warn : noname00.cpp(23,30):Code has no effect
Error: noname00.cpp(25,2):do statement must have while
Error: noname00.cpp(25,1):Compound statement missing }
Warn : noname00.cpp(25,1):'check' is declared but never used

Your while statement is on the wrong line. It should be at the close of your do loop, but it's currently at the close of your switch.

Your while statement is on the wrong line. It should be at the close of your do loop, but it's currently at the close of your switch.

thank you for your help

Well first of all you are not mentioning the namespace for "cin" and you shoud any way use "using namespace std;" just after you include the libraries.
Second of all you should put the "int" operator before "main()" because te way you wrote it you don't have a core for your program.
And the switch statement is missing a bracket.

This article has been dead for over six months. Start a new discussion instead.