Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to ignore all whitespace up to the first character (desperately needing a simple nudge)

Tags:

c++

I'll be flat out honest, this is a small snippet of code I need to finish my homework assignment. I know the community is very suspicious of helping students, but I've been racking my head against the wall for the past 5 hours and literally have accomplished nothing on this assignment. I've never asked for help on any assignments, but none have given me this much trouble.

All I'm having trouble with is getting the program to strip the leading whitespace out. I think I can handle the rest. I'm not asking for a solution to my overall assignment, just a nudge on this one particular section.

I'll post the full assignment text here, but I am NOT posting it to try to get a full solution, I'm only posting it so others can see the conditions I have to work with.

"This homework will give you more practice in writing functions and also how numbers are read into a variable. You need to write a function that will read an unsigned integer into a variable of type unsigned short int. This will have a maximum value of 65535, and the function needs to take care of illegal numbers. You can not use "cin >>", inside the function. The rules for numeric input are basically as follows:

1) skip all leading white spaces 2) first character found must be numeric else an error will occur 3) numeric characters are then processed one at a time and combine with number 4) processing stops when non-numeric found

We will follow these rules and also add error handling and overflow. If an illegal entry is made before a numeric than an error code of "1" will be sent back, if overflow occurs, that is number bigger then 65535, then error code of "2" will be sent back. If no error then "0" is sent back.

Make sure the main function will continue to loop until the user enters a “n” or “N” for NO, the main should test the error code returned from the function called “ReadInt” and display appropriate error messages or display the number if there is no error. Take care in designing the “ReadInt” function, it should be value returning and have a reference parameter. The function needs to process one character at a time from the input buffer and deal with it in a correct fashion. Once the number has been read in, then make sure the input buffer is empty, otherwise the loop in main may not work correct. I know this is not how the extraction works, but lets do it this way.

You do not need to turn in an algorithm with this assignment, but I would advise you to write one. And the debugger may prove helpful as well. You are basically rewriting the extraction operator as it works on integers."

A majority of my code won't make sense as I've been deleting things and adding things like crazy to try everything I can think of.

#include <iostream>
#include <CTYPE.h>

using namespace std;

int ReadInt (unsigned short int &UserIn);

int main()
{
    int Error;
    unsigned short int UserInput;
    char RepeatProgram;

    do
    {
        Error=ReadInt(UserInput);

        if (Error==0)
            cout << "Number is " << UserInput << endl;

        else if (Error==1)
            cout << "Illegal Data Entry\n";

        else if (Error==2)
            cout << "Numerical overflow, number too big\n";

        cout << "Continue?  n/N to quit: ";
        cin >> RepeatProgram;

        cout << endl;

    } while (RepeatProgram!='N' && RepeatProgram!='n');

}

int ReadInt (unsigned short int &UserIn)
{
    int Err=0;
    char TemporaryStorage;
    long int FinalNumber=0;

    cout << "Enter a number: ";

    //cin.ignore(1000, !' '); this didn't work

    cin.get(TemporaryStorage);

    cout << TemporaryStorage;//I'm only displaying this while I test my ideas to see if they are working or not, before I move onto the the next step

    cout << endl;

    return Err;
}

I really appreciate any help I may get and hope I don't give the impression that I'm looking for a full free solution to the whole problem. I want to do this on my own, I'm just lot on this beginning.

like image 681
joe_04_04 Avatar asked Nov 06 '15 08:11

joe_04_04


1 Answers

As a preface, I want to state that this is a question made by a student, but unlike most of their type, it is a quality question that merits a quality answer, so I'll try to do it ;). I won't try to just answer your concrete question, but also to show you other slight problems in your code.

First of all, let's analyze your code step by step. More or less like what a debugger would do. Take your time to read this carefully ;)...

#include <iostream>
#include <CTYPE.h>

Includes headers <iostream> and <ctype.h> (the uppercase works because of some flaws/design-decisions of NTFS in Windows). I'ld recommend you to change the second line to #include <cctype> instead.

using namespace std;

This is okay for any beginner/student, but don't get an habit of it! For the purposes of "purity", I would explicitly use std:: along this answer, as if this line didn't existed.

int ReadInt (unsigned short int &UserIn);

Declares a function ReadInt that takes a reference UserIn to type unsigned short int and returns an object of type int.

int main()
{

Special function main; no parameters, returns int. Begin function.

    int Error;
    unsigned short int UserInput;
    char RepeatProgram;

Declares variables Error, UserInput, and RepeatProgram with respective types int, unsigned short int, and char.

    do
    {

Do-while block. Begin.

        Error=ReadInt(UserInput);

Assign return value of ReadInt of type int called with argument UserInput of type int& to variable Error of type unsigned short int.

        if (Error==0)
            std::cout << "Number is " << UserInput << endl;

If Error is zero, then print out UserInput to standard output.

        else if (Error==1)
            std::cout << "Illegal Data Entry\n";

        else if (Error==2)
            std::cout << "Numerical overflow, number too big\n";

Otherwise, if an error occurs, report it to the user by means of std::cout.

        std::cout << "Continue?  n/N to quit: ";
        std::cin >> RepeatProgram;

Query the user if he/she wants to continue or quit. Store the input character in RepeatProgram of type char.

        std::cout << std::endl;

Redundant, unless you want to add padding, which is probably your purpose. Actually, you're better off doing std::cout << '\n', but that doesn't matters too much.

    } while (RepeatProgram!='N' && RepeatProgram!='n');

Matching expression for the do-while block above. Repeat execution of the given block if RepeatProgram is neither lower- or uppercase- letter N.

}

End function main. Implicit return value is zero.

int ReadInt (unsigned short int &UserIn)
{

Function ReadInt takes a reference UserIn to unsigned short int and returns an object of type int. Begin function.

    int Err=0;
    char TemporaryStorage;
    long int FinalNumber=0;

Declares variables Err, TemporaryStorage, and FinalNumber of respective types int, char, and long int. Variables Err and FinalNumber are initialized to 0 and 0, respectively. But, just a single thing. Didn't the assignment said that the output number be stored in a unsigned short int? So, better of this...

    unsigned short int FinalNumber = 0;

Now...

    std::cout << "Enter a number: ";

    //std::cin.ignore(1000, !' '); this didn't work

Eh? What's this supposed to be? (Error: Aborting debugger because this makes no logic!**). I'm expecting that you just forgot the // before the comment, right? Now, what do you expect !' ' to evaluate to other than '\0'? istream::ignore(n, ch)will discard characters from the input stream until either n characters have been discarded, ch is found, or the End-Of-File is reached.

A better approach would be...

   do
       std::cin.get(TemporaryStorage);
   while(std::isspace(TemporyStorage));

Now...

    std::cin.get(TemporaryStorage);

This line can be discarded with the above approach ;).

Right. Now, where getting into the part where you obviously banged your head against all solid objects known to mankind. Let me help you a bit there. We have this situation. With the above code, TemporaryStorage will hold the first character that is not whitespace after the do-while loop. So, we have three things left. First of all, check that at least one digit is in the input, otherwise return an error. Now, while the input is made up of digits, translate characters into integers, and multiply then add to get the actual integer. Finally, and this is the most... ahem... strange part, we need to avoid any overflows.

    if (!std::isdigit(TemporaryStorage)) {
        Err = 1;
        return Err;
    }

    while (std::isdigit(TemporaryStorage)) {
        unsigned short int OverflowChecker = FinalNumber;

        FinalNumber *= 10; // Make slot for another digit
        FinalNumber += TemporaryStorage - '0'; '0' - '0' = 0, '1' - '0' = 1...

        // If an unsigned overflows, it'll "wrap-around" to zero. We exploit that to detect any possible overflow
        if (FinalNumber > 65535 || OverflowChecker > FinalNumber) {
            Err = 2;
            return Err;
        }

        std::cin.get(TemporaryStorage);
    }

    // We've got the number, yay!
    UserIn = FinalNumber;

The code is self-explanatory. Please comment if you have any doubts with it.

    std::cout << TemporaryStorage;//I'm only displaying this while I test my ideas to see if they are working or not, before I move onto the the next step

    cout << endl;

    return Err;

Should I say something here? Anyway, I already did. Just remember to take that std::couts out before showing your work ;).

}

End function ReadInt.

like image 115
3442 Avatar answered Sep 28 '22 18:09

3442