Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Beginner Boolean question

  1. May 8, 2006 #1
    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 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')

    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;
    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: May 8, 2006
  2. jcsd
  3. May 8, 2006 #2


    User Avatar
    Science Advisor
    Homework Helper

    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"
  4. May 8, 2006 #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 gonna use it properly.
    Last edited: May 8, 2006
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook