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

  • Thread starter GregA
  • Start date
  • Tags
    Beginner
In summary, the programmer is a complete beginner to programming and C++. He is working on a program that calculates the i'th term in a sequence. He has difficulty with user input. He is having trouble with users input that consists of letters. If a letter is entered at the start the program crashes. If one is entered once it has run successfully it keeps looping the result of the last successful run. The programmer is asking for help with using a boolean in conjunction with a type of character.
  • #1
GregA
210
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
  • #2
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"
 
  • #3
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:

1. What is a Boolean?

A Boolean is a data type in computer science that has only two possible values: true or false. It is used to represent logical statements and is essential in programming and digital logic.

2. What is a Beginner Boolean question?

A Beginner Boolean question is a simple question that typically involves using basic Boolean logic to solve a problem. It is often used as an introductory exercise for those learning programming or digital logic.

3. How do I use Boolean logic in programming?

Boolean logic is used in programming to make decisions based on true or false conditions. It is typically used in conditional statements, such as if-else statements, to control the flow of a program.

4. What are some examples of Boolean operators?

Some common Boolean operators include AND, OR, and NOT. These operators are used to combine multiple Boolean values and evaluate them as a single statement.

5. Can Boolean values be used in mathematical operations?

Yes, Boolean values can be used in mathematical operations. In most programming languages, true is treated as 1 and false is treated as 0. This allows for Boolean values to be used in mathematical expressions and calculations.

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top