How can I use boolean to check for valid user input in C++?

  • Context: C/C++ 
  • Thread starter Thread starter GregA
  • Start date Start date
  • Tags Tags
    Beginner
Click For Summary
SUMMARY

This discussion focuses on handling user input validation in C++ using boolean logic. The user is attempting to ensure that only valid long double values are accepted, avoiding crashes from invalid inputs such as letters. Key suggestions include utilizing the "isnumeric" function and understanding ASCII representations for input validation. The user also seeks to refine their loop conditions to prevent infinite loops when invalid input is detected.

PREREQUISITES
  • Basic understanding of C++ programming
  • Familiarity with data types, specifically long double
  • Knowledge of boolean logic and control flow in C++
  • Understanding of arrays and their usage in C++
NEXT STEPS
  • Implement the "isnumeric" function to validate user input
  • Explore error handling techniques in C++ to manage invalid inputs
  • Research ASCII values and their application in character validation
  • Learn about using loops effectively to control program flow based on user input
USEFUL FOR

Beginner C++ programmers, educators teaching input validation, and developers looking to improve user interaction in console applications.

GregA
Messages
210
Reaction score
0
Firstly I am a complete beginner to programming and C++...(well not *totally* complete perhaps because I can make my screen say "hello world")
From a differerent thread I decided to see if I could write a little program that defines a sequence and can then print all the terms of that sequence up to any point. I've sort of got that far but my major difficulty is sorting out problems with user input. I have it where it asks for a term and expects a long double (about as big a number that I know of at the moment)..problem is I don't want anything other than a number to be allowed.
If a letter is entered at the start I get a crash. If one is entered once it has run successfully it keeps looping the result of the last successful run.
Basically I'm asking if there is anyway of using a boolean in conjuction with a type of character, as opposed to that characters value.
(I've also just noticed that if I type "yes" or "No" etc... then I'm screwed because it sees 'y'..moves on then gets stuck again, though I don't think this is quite as problematic...need to play around with strings perhaps and another array? (If I try anything though I get compilor errors: ISO C++ forbids comparison between pointer and integer centred at my test for conditions)

What I do know is that each character has its own ASCII equivalent but I really can't think of any ways to make use of this yet. Trying to find where I should be looking is a problem because I don't know a great deal yet and am putting little bits together from here and there.
Any help would be appreciated and my code is below (apologies if it's a mess!)


#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;
// The sequence of numbers i_(0), i_(1), i_(2), i_(3)...where i_(0) is defined to be 3 and i_(n) is defined to be i(n-1) + 4^(n-1)

// HAVE NOT FIXED ALL STUPID INPUT YET...
// have to figure out how to force people to type integers instead of characters for 'Input'
// don't want to use a massive 'while' condition testing for a bazillion potential key combinations

long double Sequencer (long double a); //prototype function...declaring it here stops the compiler from whining when it encounters it later!

int main(int argc, char *argv[])
{
count.setf(ios::scientific,ios::fixed); // Probably don't need this
long double i =3; // the basis from which to calculate the i'th term.
char moreInput;
count << "This is the sequence of numbers i_(0), i_(1), i_(2), i_(3)...\n";
count << "where i_(0) is defined to be 3 and i_(n) is defined to be i_(n-1) + 4^(n-1)\n";
count <<"\n"<<"\n";

// This loop begins by calling the sequencer function
// once it has finished it asks if the user wants to try again
// As long as n or N is not the input the sequencer will run again (in some way)
while (moreInput != 'N' || moreInput != 'n')
{
i = Sequencer(i);
i = 3; //once sequencer has finished need to put i back to what it was initially
count <<"again?\n" << "Yes [press 'y'], No [press 'n']\n";
cin >> moreInput;
// testing for stupid input...
while (moreInput != 'n'&& moreInput != 'N' && moreInput != 'Y'&& moreInput != 'y')
{
count <<"eh? (y or n)?\n";
cin >> moreInput;
}
// program ends if user answers n or N to "again?"
if (moreInput == 'n'|| moreInput == 'N')
{
break;
}
}

system("PAUSE");
return EXIT_SUCCESS;
}
long double Sequencer (long double i)
// User says how far into the sequence they want to go by entering a number in "userInput"
// as long as counter is less than userInput this function puts i into an array, then takes i, assigns it to temp and adds d to it.
// counter is then incremented by one and d is multiplied by 4, finishes by assigning temp to i and starting over.
{
int g = 1; //this will be used to reverse the print order later on
int userInput;
int counter =1;
long double temp;
long double d = 4;
count << "which term in the sequence?\n";
cin >> userInput;
count <<"\n";
long double myArray[userInput]; //makes the array only as large as is 'userInput'
while (counter<=userInput)
{
count<< setprecision (14);
myArray[counter] = i; //the element 'counter' of myArray is assigned the value of i
temp = i+d;
counter++;
d = 4*d;
i = temp;
}
while (g <= userInput)
{
count << g <<")" << myArray[g] << ", "; //whatever number you inputted..the corresponding value of 'a' is printed.
g++; //input is incremented until there are no elements left to print
if (g%3 == 0)//don't want a massive column...want some on the same row
{
count << "\n";
}
}
count <<"\n"; //Want a space between the next sequence
}
 
Last edited:
Technology news on Phys.org
GregA said:
while (moreInput != 'N' || moreInput != 'n')
Think about this

True || True = True
True || False = True
False || True = True
False || False = False

True && True = True
True && False = False
False && True = False
False && False = False

For picking out numbers
Knowledge of the ascii representations is useful
as well as the "isnumeric" function

For the pointer thing see "cast"
 
cheers for the reply and pointing out an error NoTime...I changed that statement from || to && because I really only need it to be false just once! and as you suggest, with 'or' I can have a false but still evaluate true. (didn't give it enough thought)

I probably have a fair bit of reading and playing to do until I can sort this problem once and sort it again elsewhere. Though I will start with your suggestions...I think the isnumeric function will help me here but I have to figure out how I'm going to use it properly.
 
Last edited:

Similar threads

  • · Replies 6 ·
Replies
6
Views
12K
Replies
12
Views
2K
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 75 ·
3
Replies
75
Views
7K
  • · Replies 89 ·
3
Replies
89
Views
6K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K