0

I posted an earlier question asking for help with using a validation script in my code. After a very helpful answer I was able to sort of figure out how I needed to proceed. Well, I've hit a big obstacle;

#include <iostream>
#include <sstream>
#include <string>
#include <ctype.h>

using namespace std;

int main()
{
        unsigned __int64 input = 0;
        int n, i;
        char str[]="c3po...";
        i=1;
        n=0;


        for (cout << "Input a number" << endl; cin >> input; cin.ignore(numeric_limits<int>::max(), '\n'))
    {
        cout << "We're parsing your input '" << input << "'\n";

     if (input % 2 == 0) 
        {
            cout << "Even!" << endl;
        }

    else if (input % 2 == 1)
        {
            cout << "Odd" << endl;
            cout << "Lets make it even shall we? " << "Your new number is... " << input + 1 << endl;
        }
    else (isalnum(str[i]));i++;
        { 
        cout << "We could not parse '" << input << "' as a number.\n";
        }    
    }
    system ("pause");
    return 0;
}

As you can see from my code, the validation script is well, sort of working. I have some bugs I wish to iron out.

1- When I input a number, it runs though the code as it should but it also displays

Could not parse 'inputted number' as a number

obviously when a number is inputted you don't want this to happen!

2- For the error message, it is showing the inputted number as [0]. Is this to do with using an integer? How can this be fixed?

Thanks!

John Brown
  • 151
  • 3
  • 14
  • I'm off to bed now, sorry folks. Early start tomorrow. – John Brown Nov 19 '12 at 00:22
  • You are not putting in practice what you got for you previous question. You are still extracting directly in input instead of using a variable to validate the user's input. – PRouleau Nov 19 '12 at 01:13
  • I couldn't understand that code. I broke it down and changed the layout so it looked like how I would code, but I have no idea what the parts do :( I've kept it, so when I get more experience I will go back to it and modify it. – John Brown Nov 19 '12 at 07:52
  • Then rewrite it by using a regular loop or remote it entirely. You basically have two steps: 1) get user input, 2) validate it. I can point two problems: a) you are using isalnum() instead of isalpha() or !isdigit(); b) you are reading directly in "input" which is of type __int64 and so it can only contains a number. Of course, there is also the else-thing. – PRouleau Nov 20 '12 at 00:25

1 Answers1

2

your problem is quite simple, you have small mistake on this line

else (isalnum(str[i]));

Your else statement ends at the semicolon and does actually nothing. Following statements will be executed every time.

i++;
    { 
    cout << "We could not parse '" << input << "' as a number.\n";
    } 
CodeBro
  • 199
  • 4
  • Hmm, how would I go across fixing this? – John Brown Nov 19 '12 at 07:52
  • I think you can just delete it. Statement following else just checks second letter of string in str, since str and i don't change. Since there is no if statement your error message is printed every time. If you want to check if user input was actually a number check out http://stackoverflow.com/questions/4654636/how-to-determine-if-a-string-is-a-number-with-c – CodeBro Nov 19 '12 at 10:47
  • Is there any way to get it sorted using my own code? I've haven't been doing c++ for very long, so ok trying to get hold of the basics, the link you sent me just seems to go straight over my head! – John Brown Nov 19 '12 at 11:49
  • There is no point in validating your variable if it is unsigned integer, because it will always contain a valid number whether user gave good or bad input. Doing real validation requires bit more complicated code like the one in the link. – CodeBro Nov 20 '12 at 10:48