Archived

This topic is now archived and is closed to further replies.

Problem In This Code

This topic is 5638 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need some help if any of you can help me with this code. Okay, I get an error on line 16 of this code. Its in the if statement after asking for input. My compiler just says this parse error before ''short'' Thats all its saying. See what I want it to do is check and see if the input for the two Variables and see if either one is a character or a number higher than an unsigned short integer. If it is I want it to display an error message, but yeah, you know the problem, now please look over this code... #include <iostream> #include <stdlib.h> using namespace std; long int Add(unsigned short int a, unsigned short int b); int main() { unsigned short int VarOne, VarTwo; int Answer; cout << "Please enter unsigned short integer to add with another: " << endl; cin >> VarOne; cout << "Enter second unsigned short integer to add with other: " << endl; cin >> VarTwo; if ((VarOne != unsigned short int) || (VarTwo != unsigned short int)) { cout << "Error! Values you entered were either not usigned" << endl; cout << "short integers or else they were exceeding the limit!" << endl; system("pause"); return 0; } Answer = Add(VarOne,VarTwo); cout << "Your Answer is: " << Answer << endl; system("pause"); return 0; } long int Add(unsigned short int a, unsigned short int b) { long int c; c = a + b; return c; }

Share this post


Link to post
Share on other sites
That doesn''t work unfortunately.


  
if ((VarOne != unsigned short int) || (VarTwo != unsigned short int))
{
cout << "Error! Values you entered were either not usigned" << endl;
cout << "short integers or else they were exceeding the limit!" << endl;

system("pause");
return 0;
}


The problem is that "unsigned short int" can ONLY be used in two ways: variable declarations, and type-casting. If you don''t know what type-casting is, that''s where you tell the compiler to perform an explicit type conversion, say from a floating point number to an integer.

Anyway back to the problem; you really don''t even need to perform a check. The variable CANT hold more than an "unsigned short integer" can; if the user enters a number too large, it will just wrap-around to a negative value. So this won''t work and isn''t necessary.

Now a quick question. Is this part of a larger project or are you just experimenting with data types? Just curious.

Share this post


Link to post
Share on other sites
Alright, well that sucks. See I wanted it to go and just display an error if they did put a number that exceeds a usigned short intger. See I want it so you can''t put a character or a number going over a unsigned short integer. I put a character in before I even put an if statement in and it gave me a value and stuff. Thats when I decided to see if there was a way that I could change that and make a error message. Just didn''t see why you couldn''t put a data type in as a test.

Okay, well I was doing more. I''m actually only a beginner. I bought this book and I read through most of it and all that stuff. Then I started to get a little confused since I missed a few things I see when I went through the book. Just didn''t see it or something like that. So I''m reviewing all I know and stuff. I suppose this doesn''t answer your question so I''ll answer it now. Yes, I am messing around with data types and not a big project. Alright, I''m done.

Share this post


Link to post
Share on other sites
The thing is what you were trying to do wasn''t necessary because of the way C++ works (except that it was a learning experience which is ALWAYS good! ). Here''s how it works:

An unsigned integer is (let me see if I can remember how many bits it is...it''s platform dependent; oh well I''ll just guess, it won''t hurt the concept). Let''s say the unsigned short int is 8-bits. That means that each usi (unsigned short int) variable you declare takes up exactly 1 byte (8 bits) of memory in your computer. Now in 8 bits you can represent values from 2 to the 0 all the way to 2 to the 8 power which is a range of 0 to 255. Now let''s say you want to store some values in your variable:


  
unsigned short int a, b;

a = 47; //Somewhere in RAM you now have the bit sequence 00101111 which is 47 in binary.

b = 256; //Somewhere in RAM you now have the bit sequence 00000000 which is 0 in binary.




Why wasn''t the value 256 stored in b? Why was 0 stored there instead even though you told it to store 256? Because you CAN''T physically store a 9-bit value in an 8-bit storage location; since 256 requires nine bits to store (it''s 100000000 in binary) and you only have 8-bits for your variable, the compiler just ignores the most significant bit (the 1 in this case; the MSB is the leftmost bit). You caused an overflow but the compiler let you get away with it, simply resulting in incorrect (or at least unitended) data.

Alright, now how this applies to your problem. Your code took input from the user using the statement "cin >> varOne". You wanted to make sure the user did not enter a number that was too big for the unsigned short int to hold.

Actually I just understood what you wanted to do. Oh well the long rambling paragraphs above may help you or someone else who reads it so I''ll just leave it. Now to your problem AGAIN...

Okay basically...hmmm...AlekM had a feasible solution. Obviously you could go with a larger data type but you could still have the same problem. That is actually a tricky problem and I don''t know of a truly elegant solution. Kind of ironic since I wrote a whole bunch!

Share this post


Link to post
Share on other sites
Yeah, I totally understand what your talking about. When you wrote all those paragraphs, bob_the_third. Yeah, you seemed to understand my question well too. Well, thats just the way programming is. So, thanks for all your guys'' help. I''ll just figure something different out or work on some other stuff I should review on.

Share this post


Link to post
Share on other sites
Just wanted to point out that on most systems unsigned short int is a 16 bit type, not 8 bit which means that bob_the_third''s post wasn''t entirely correct. An unsigned char now, that is 8 bit.

char 8 bit
short 16 bit
long 32 bit

Your probably know the difference between signed and unsigned.

Share this post


Link to post
Share on other sites
quote:
Original post by nyStagmus
Just wanted to point out that on most systems unsigned short int is a 16 bit type, not 8 bit which means that bob_the_third''s post wasn''t entirely correct.

The C++ Standard does not mandate sizes for the built-in types. Since bob_the_third was talking in terms of arbitrary sizes simply for exposition purposes, there was nothing wrong with what he said. Importantly, he pointed out that the sizes are platform dependent.

Share this post


Link to post
Share on other sites
I am sorry if I offended anyone. I just wanted to show experimentx the differences between the types, not so much criticize bob. And yes, the types are platform dependent, but on most popular systems (Windows, Linux etc) these are the default sizes.

Share this post


Link to post
Share on other sites
quote:
Original post by nyStagmus
...but on most popular systems (Windows, Linux etc)...

AIX, HP-UX and Alpha are very popular operating systems. Their standard word size is 64, and the sizes of an unsigned short int vary. Naturally, I''m just stressing you, but it''s also important that you learn and teach as much platform agnostic programming as possible.

Share this post


Link to post
Share on other sites