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

  • Thread starter Thread starter GregA
  • Start date Start date
  • Tags Tags
    Beginner
Click For Summary
The discussion revolves around a beginner's attempt to write a C++ program that defines and prints a mathematical sequence. The main challenge faced is handling user input correctly to ensure that only numerical values are accepted. The user experiences crashes when non-numeric characters are entered and encounters issues with input validation, particularly when trying to prevent the program from looping with invalid inputs. Suggestions include using the "isnumeric" function to validate input and considering the ASCII values of characters for better control over input handling. The user also notes confusion with logical operators in their loop conditions, realizing that changing from "||" (or) to "&&" (and) may resolve some issues. The importance of understanding pointers and casting in C++ is highlighted as a potential area for further learning. Overall, the user expresses a need for guidance on effective input validation techniques and improving their understanding of C++.
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[])
{
cout.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;
cout << "This is the sequence of numbers i_(0), i_(1), i_(2), i_(3)...\n";
cout << "where i_(0) is defined to be 3 and i_(n) is defined to be i_(n-1) + 4^(n-1)\n";
cout <<"\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
cout <<"again?\n" << "Yes [press 'y'], No [press 'n']\n";
cin >> moreInput;
// testing for stupid input...
while (moreInput != 'n'&& moreInput != 'N' && moreInput != 'Y'&& moreInput != 'y')
{
cout <<"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;
cout << "which term in the sequence?\n";
cin >> userInput;
cout <<"\n";
long double myArray[userInput]; //makes the array only as large as is 'userInput'
while (counter<=userInput)
{
cout<< 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)
{
cout << 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
{
cout << "\n";
}
}
cout <<"\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:
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

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