Hi there,

I am very new to C++ so please bear with my what is clearly terrible work

I am trying to figure out what should be (I think) a simple program that will check a couple of pieces of information and tailor an output based on the input, if you get me

Here is my (albeit amateur) pseudo code:

char scheduled 		// Whether a particular user is scheduled for work on that date
char logonattempt 	//whether a particular user has attempted to log on
char entry 		// whether a particular user has used their security pass to enter the building

Output:  Is the user scheduled for work today? (Y/N)
Output:  Has the user attempted to log onto their PC? (Y/N)
Output:  Has the used gained entry to the building using their security pass? (Y/N)


if
	scheduled=1
	and
	logonattempt=1
	and
	entry=0
	then output "User has attempted to log in but has not passed security to enter building"
elseif
	scheduled=0
	and
	logonattempt=0
	and
	entry=1
	then output "user has entered building but is not scheduled for shift"
elseif
	scheduled=0
	and
	logonattempt=1
	and
	entry=0
	then output "User has attempted to log in but is not scheduled for shift"
elseif
	scheduled=0
	and
	logonattempt=1
	and
	entry=1
	output "User has gained entry and attempted to log in but is not scheduled for shift"
else
	"User is fully authorised"

and here is the start of my C++ code, which I'm sure I have done incorrectly anyway and yet do not know where I am going wrong:

#include <iostream>
using namespace std;

main()
{
	// variable declarations
	char ScheduledIn;     	// Whether a particular user is scheduled
                                // for work on that date
        char LogonAttempt; 	// Whether a particular user has attempted
                                // to log on
        char AuthdEntry; 	// whether a particular user has used their
                                // security pass to enter the building


	// Check if the user was scheduled for work
	cout<< "Is the user scheduled for work today? (Y/N)"/n;
        cin>> ScheduledIn;      // Sets the variable to whatever the user
                                // inputs
                if !(ScheduledIn == ("y" || "n" || "Y" || "N"))
                {
                        cout<< "Please enter a correct value"/n
                                "Is the user scheduled for work today? (Y/N)"/n;
                else
                }

        // Check if the user has attempted to log in
        cout<< "Has the user attempted to log in? (Y/N)"/n;
        cin>> LogonAttempt;     // sets the variable to whatever the
                                //user inputs
                if !(LogonAttempt == ("y" || "n" || "Y" || "N"))
                {
                        cout<< "Please enter a correct value"/n
                                "Has the user attempted to log in? (Y/N)"/n;
                else
                }
        // Check if the user gained authorised entry
        cout<< "Has the user used their security pass to gain entry? (Y/N)"/n;
        cin>> AuthdEntry;       // sets the variable to whatever the
                                //user inputs
                if !(AuthdEntry == ("y" || "n" || "Y" || "N"))
                {
                        cout<< "Please enter a correct value"/n
                                "Has the user used their security pass to gain entry? (Y/N)"/n;
                else
                }
};

Hopefully my pseudo code will give you a rough idea of what I am trying to do. I was thinking of maybe doing a loopwhile of some sorts for the questions and inputs but do not know how to do this. Then after the input had been given, run an if statement to produce the warnings similar to that in the pseudo code?

I think my if statement goes wrong at the first part, where I'm trying to tell it to discard any other input aside from y, n, Y or N but I can't be sure

Any help would be greatly appreciated, and sorry for the mistakes so far :(

Thanks

Recommended Answers

All 16 Replies

line 19: >> if !(ScheduledIn == ("y" || "n" || "Y" || "N"))

Formed wrong. if !(ScheduledIn == "y" ||ScheduledIn == "n" || ScheduledIn =="Y" ||ScheduledIn == "N")) The other if statements have similar problem. You can shorten it up by converting to upper-case first

ScheduledIn = toupper(SchecduledIn);
if (ScheduledIn =="Y" || ScheduledIn == "N"))
{

}

If ScheduledIn will be of type char (i.e. char ScheduledIn) then you cannot use double quotation marks in the if expression, use ' ' instead ...

char ScheduledIn;
...
ScheduledIn = toupper(SchecduledIn);
if (ScheduledIn == 'Y' || ScheduledIn == 'N'))
{

}
commented: You are right -- I should have caught that too :) +28

for looping u can use this way:

while(1) {
     cout<< "Is the user scheduled for work today? (Y/N)"/n;        
     cin>> ScheduledIn;      
      ScheduledIn = toupper(ScheduledIn)
     if (ScheduledIn !='Y' || ScheduledIn != 'N')    {
        cout<< "Please enter a correct value"/n ;
     } else {
        break;
     }
}

////Here start witting other validations same as above

Other than that you can use this with shorter code.

cout<< "Is the user scheduled for work today? (Y/N)"/n;        
     cin>> ScheduledIn;      
      ScheduledIn = toupper(ScheduledIn)

while((ScheduledIn !='Y' || ScheduledIn != 'N')) {
    cout<< "Please enter Either Y OR N";
     cout<< "Is the user scheduled for work today? (Y/N)"/n;        
     cin>> ScheduledIn;      
      ScheduledIn = toupper(ScheduledIn)
     }

Firstly, thank you for your help so far.

Secondly, Borland does not seem to like the statement:

ScheduledIn = toupper(ScheduledIn);

I think it could possibly have something to do with me changing my declarations this morning to:

char ScheduledIn[1];

Though I did this to limit the user input - can you only do this with int's? This conversion to uppercase will be very useful however, especially considering the potential length of covering all lower and uppercase letters, thanks!

Also, I changed the if statement to:

if (ScheduledIn != 'Y' || ScheduledIn != 'N'))
{
}

As I wanted it to produce the error message only if there was a different output. I see that the issue will probably be solved better with the loop (which was what I thought originally, I just didn't really know how to do it) but I still do not understand why my change to put a NOT statement in there does not work?

A char will always only hold 1 character so: char ScheduledIn[1]; is useless.
It is indeed the cause of your problems. ScheduledIn is now a pointer to the first element of an array of chars. So change it back the way it was.

if (ScheduledIn != 'Y' || ScheduledIn != 'N'))
{
}

The above will always be true, so it's wrong. Look closely at what your wrote: if ScheduledIn is not 'y' OR ScheduledIn is not 'n' ScheduledIn will always be "not the one" or "not the other".

Well the function Toupper(); is defined in a different library and u need to add in another header file for it to work.

add

#include <cctype>

into your previous code and then try to run the program agian.

A char will always only hold 1 character so: char ScheduledIn[1]; is useless.
It is indeed the cause of your problems. ScheduledIn is now a pointer to the first element of an array of chars. So change it back the way it was.

if (ScheduledIn != 'Y' || ScheduledIn != 'N'))
{
}

The above will always be true, so it's wrong. Look closely at what your wrote: if ScheduledIn is not 'y' OR ScheduledIn is not 'n' ScheduledIn will always be "not the one" or "not the other".

Ahh I think understand now, clearly if I had been using a str ScheduledIn then the [1] would have been more appropriate, although still useless as char should be used anyway?

With regards to the if statement change, my aim was to say basically if the input was not Y and was not N (for example it could have been a, B, z, 2 etc. then an error code would have been produced using a cout in the {} part?

If that was my aim, should I have used an ampersand instead?

if (ScheduledIn != 'Y' && ScheduledIn != 'N'))
{
}

?


With regards to the if statement change, my aim was to say basically if the input was not Y and was not N

If that was my aim, should I have used an ampersand instead?

if (ScheduledIn != 'Y' && ScheduledIn != 'N'))
{
}

?

Correct.

Ahh I think understand now, clearly if I had been using a str ScheduledIn then the [1] would have been more appropriate, although still useless as char should be used anyway?

A char something[1] is never usefull. Use char if you want 1 character and if you want an array of characters use char something[morethen1] But if you want to use the char array as a word or sentence, I strongly suggest you have a look at the std::string which is a nice c++ 'addon'

Thanks, things are certainly getting clearer for me now.

I have a sneaking suspicion however that all my code is going to come out on one line, because every time I used /n borland flags it as incorrect and won't let me carry on until I've deleted it or used:

cout<< "Statement" << endl;

Is there really any difference?

it should be: " \n" (in quotes and a backslash)

Apologies for my lack of speed in replies,

Thank you - I was worried that if I did what you said it would actually output \n on screen, but I see it is similar to using < br >

After completing my big if statement to analyse all the inputs all seems to be fine, though I get a linker error at the bottom of Borland relating to unresolved external something or other - I think that's related to where the save is though rather than my code

One more question, if I declare another variable at the start, "str UserID[8]" - is there any known way to force the use of an input-mask like one may do with a database for example?

If this is possible I may (I think?) be able to use a particular user ID in the output, for example:

if      ((ScheduledIn == 'Y' &&
                LogonAttempt == 'Y' &&
                AuthdEntry == 'N'))
                {
                cout<< "User " << UserID << " has attempted to log in but has not used " << "\n";
                cout<< "their security pass to enter the building!" << "\n";
                }

This would work fine so long as the input was given in the correct format, I think. However an input mask feature such as in Databases would be ideal.

I have been doing some reading and it looks like the best way to do othis would be to store the input in a buffer (?) and analyse each character for the correct format?

The format itself would be

>L<0000000 in a database,

One more question, if I declare another variable at the start, "str UserID[8]" -

str is not a variable type, I guess you meant: char UserID[8]; The rest of the code is good, but I'm not sure I understand your question. What is >L<0000000 ? Which character represents what?

Let me give you an example on char-arrays:

/*declare a char-array size 8. The 8th place will be the '\0' char (end of string) */ 
char something[8] = "testing";
cout << something[0] << something[1] << something[2] << something[3] << "\n";

output will be 'test'.

So if each zero in: >L<0000000 represents some status, you could check them in a similair way

str is not a variable type, I guess you meant: char UserID[8]; The rest of the code is good, but I'm not sure I understand your question. What is >L<0000000 ? Which character represents what?

Thanks for the reply

Basically when you enter data into a field in a database you can specify a specific format:
> makes the text uppercase
< reverts the test to lower case
L represents any letter
0 represents any number

So the input mask >L<0000000 would result in only input of format A1234567 or a1234567 (the second would automatically make an uppercase letter appear, however)

So in some case you may want to use a British National Insurance number, which is always in the format A 12 34 56 B - the input mask would be >LL<000000>L<

I think what you mentioned would be the most appropriate way - storing all input characters into an array and then doing a multitude of checks on each element in order to verify the format

British National Insurance number format is

AB 12 34 56 C

Sorry

To check is a char is a letter of the alphabet, you could use isalpha()
isdigit() does the same for numbers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.