Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi all, I was wondering for the following, is it possible to keep prompting user input (output window will not be close) without the use of while loops?

In case if I am not clear/ my english is no good, suppose if the user inputs in 20, it will return the output 'Wrong!' but it will re-prompts the user to re-enter in a new number

Can that be done?

C++
#include <iostream>
using namespace std;

bool numCheck (int num);

int main(){
    int input;
    cout << "Enter in a number : ";
    cin >> input;
    
    if (numCheck(input)){
        cout << "Correct!" << endl;
    }
    else{
        cout << "Wrong!" << endl;
    }
}

bool numCheck (int num){
    if (num == 10){
        return true;
    }
    else{
        return false;
    }
}


What I have tried:

I know that using of while loop could possible be the ideal solution for it, but would like to know if there are any other alternatives for something as simple as the code I have posted..
Posted
Updated 26-Feb-16 13:50pm
Comments
[no name] 24-Feb-16 23:07pm    
As a beginner why look for something else if you know a while loop will work? Every solution will involve some kind of looping. Just use Google - something like "c++ prompt for input loop".
yann00n 24-Feb-16 23:37pm    
I did and I have been getting results where while loops are being used... And hence my question
[no name] 24-Feb-16 23:51pm    
??
Sergey Alexandrovich Kryukov 25-Feb-16 0:25am    
???
Jochen Arndt 25-Feb-16 2:59am    
Using a while loop is common for this task. So why use something else?

However, you may use a for loop too as an alternative but it would be more obfuscated.

Based on your requirement, you should use do while[^] instead of while as you have to allow a user to enter at least once before checking the condition.
 
Share this answer
 
v2
Comments
yann00n 25-Feb-16 2:41am    
So I suppose only with the use of while or do..while loops, these two are the only ones that enables the output console/window to be present until the user input in the correct value?
Peter Leow 25-Feb-16 3:56am    
do...while loop is the right choice.
 
Share this answer
 
Quote:
Loop without the use of while loop
It is possible, but there is no point doing so.
There is many fashions to do loops in C but all fall in pretty much the same result, same capacities, same efficiency.
So there is no reason to avoid 1 kind of loop.
 
Share this answer
 
As many have said, this problem is clearly best solved wit a loop.
However...as an intellectual exercise:
Yes, you could do it with recursion: Recursion - Wikipedia, the free encyclopedia[^]
C++
#include <iostream>
using namespace std;

bool numCheck(int num);
typedef bool(*CheckFn)(int);
int enterCorrectNumber(CheckFn);

int main(){
  enterCorrectNumber(&numCheck);
}

bool numCheck(int num){
  return (num == 10);
}

int enterCorrectNumber(CheckFn checker){
  int input;
  cout << "Enter in a number : ";
  cin >> input;

  if (checker(input)){
    cout << "Correct!" << endl;
    return input;
  }
	
  cout << "Wrong!" << endl;
  return enterCorrectNumber(checker);
}

You must be careful to use this only when you can bound the number of times the function can call itself or else a stack overflow will occur!
This example is structured in a way called tail-recursion so an optimizing compiler can generate the code in such a way as to prevent the stack overflow from ever happening.
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900