CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).

1. ## Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).

Yes, you're got the hang of if now. Just a comment though. There's no need for the second if in the if statement

Code:
```if (num1 >= 1 && num1 <= 100)
cout << "\nYes " << num1 << " is between 1 & 100.\n" << endl;
else if (num1 < 1 || num1 > 100)
cout << "\nNo " << num1 <<  " is not between 1 & 100.\n" << endl;```
if the result of the first if fails - is false - then the number must be outside the range ie is not between 1 and 100 so the second if is not needed in this case as it is always true. So this could be written

Code:
```    if (num1 >= 1 && num1 <= 100)
cout << "\nYes " << num1 << " is between 1 & 100.\n" << endl;
else
cout << "\nNo " << num1 <<  " is not between 1 & 100.\n" << endl;```
The same with the num2 if test. Again the second if test is not needed. Although in this case the inverse logic of not 3 16 or 45 is

Code:
`if (num2 != 3 && num2 != 16 && num2 != 45)`
If you want to invert an if condition, then reverse each conditon (eg = to !=, > to <= etc) and change || to && and && to ||. For info, this is known as De Morgan's law.

2. ## Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).

So how does your code know how to assign Ace, King, etc?
The code doesn't assign Ace, King etc. It creates an array of strings with the individual elements being the strings "Ace", "King" etc. The same with the suits. The individual elements of these arrays can then be accessed by specifying which element is required starting from 0. So "Ace" is element 0, "Two" is element 1 etc.

So looking at

Code:
`cout << value[(card - 1) % 13] << " of " << suits[(card - 1) / 13] << endl;`
% is the integer modulo operator. It returns the remainder of the the two numbers. So 7 % 3 is 1 as when 7 is divided by 3 the answer is 2 remainder 1. 8 % 5 is 3 as 8 divided by 5 is 1 remainder 3. The result is always a number beween 0 and 1 less than the second number. For 8 % 5 this would be 4, for 7 % 3 this would be 2.

So for the card values, we want a number between 0 and 12 inclusive which is used as the index to the array. The first suite is the input range 1 to 13, the second is 14 to 26 etc. So first subtract 1 to get in range 0 to 51. Then the result of the modulo 13 operator is a number in the range 0 to 12 which indexes the value array to get the card value.

For the suits, again we want an index number in the range 0 to 3 inclusive. In this case we use integer division. So 7 / 3 is 2, 8 / 5 is 1 etc. Using the range 0 to 51 and dividing by 13 this gives a number in the required range as 0 / 13 is 0 and 51 / 13 is 3.

Hope this helps.

3. Member
Join Date
Jan 2013
Posts
71

## Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).

Thanks for the link Eri523! Looks like that will help a ton!

And thanks for the info 2k! You guys have been awesome! I'll have to re-read some of that, but I get the gist of it. but again, thank you guys for shooting straight with me and leading me to how to learn this better

4. Junior Member
Join Date
Feb 2013
Posts
5

## Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).

please also tell me how to compile and run a program in Dev c++ latest version.usual methods dont work.but when i hit open and run it opens an open dialog box.it should compile the program shouldnt it.Devc++ 5.4.0
Regards.

5. ## Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).

kfger, please don't tag a completely different question onto the end of a thread that has nothing to do with what you are asking. You should start a new thread.

6. Member
Join Date
Jan 2013
Posts
71

## Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).

ok, something that i cant wrap my head around are identifiers like char, int, double, float....etc etc. I cant consistently understand which one to use except for 'char', which is like a single character like 'm'. And I've been guessing most of the time when I've tried to use 'int' or 'double'. I know I'm supposed to use it based on the range, but I dont grasp how that works. This is one of the things i either try to figure out with trial and error. and as stated here before, i cant learn to do this like that. ive read through my book, Ive read this page (http://www.cplusplus.com/doc/tutorial/variables/).

so if i want to create an identifier for 'phoenix', how exactly do i determine if it's 'int', 'double', or what?

btw, i dont have any projects this week. Im just doing some extra work to correct the way ive been building codes

7. ## Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).

(http://www.cplusplus.com/doc/tutorial/variables/) covers it. However, lets keep this simple for the moment. There are basically 3 different types of in-built c++ identifiers.

int is used when you want to store integers (whole numbers with no decimal part) eg 2, -4, 456 etc.

double is used when you want to use numbers with a decimal part eg 3.456, 4.78, -2.45 etc

char is used when you want to use a single character eg 'M', 'T', 'a' etc.

so we can do

Code:
```int  a;
double b;
char c;

a = 2;          //a is defined to be an integer so assigning 2 is OK
b = 4.546;      //b is defined to be a double so can hold numbers with a decimal part
c = 'j';        //c is defined to be a char so can hold 1 character```
There are variations on these (like signed, unsigned, long etc) as you can see from the article but ignore these for the moment until you have mastered the basics.

The next issue is with strings. A c-style string is an array of characters terminated by the NULL character (NULL or \0). For the moment, though, I'd pass on this until you understand the others.

A string literal are characters delimited by ". So "qwerty" is a string literal and so is "string literal".

c++ also has what's called the string class. This is much easier to use than c-style strings and should be used where possible for strings. "phoenix" is a string literal so needs to be assigned to a string variable.

Code:
```#include <string>
#include <iostream>
using namespace std;

string s1;

s1 = "phoenix";
cout << "String s1 is " <<  s1 << endl;```
The type of variable you use depends upon what you want it to hold. Once you know for what you are going to use the variable, the type can then be chosen to match (ie int, double, char or string). Once you are comfortable with choosing and using these then you can start looking at the variations and c-style strings (which can cause problems if not used carefully).

Hope this helps. Ask if you don't understand this because this is fundamental to c++ programming and until you do you'll have problems trying to go further.

8. Member
Join Date
Jan 2013
Posts
71

## Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).

ah that helps a ton! i didnt even think about the 'a = 2' situation for int. I was thinking of it like if i need to use 'a' it needs to be 'char'.

I'll probably need to come back and use this as a reference but this better explains to me what i was seeing with the errors before. But yea we have not touched signed/unsigned yet.

Thanks for simplifying that for me! you the man!

9. ## Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).

Pleased to be of help. If (when!) you have further questions, perhaps you could start a new thread as this one is now getting very long and what it's covering now is far from the original thread question.

If you want some extra simple programming practice, you might want to have a go at these:

1) Your uncle owns several convenience stores in New England and Canada. He originally prices all of the produce sold in his stores in terms of US pennies per pound. In an effort to maintain uniform pricing of the produce for all of the stores, he must translate prices of produce in US pennies per pound to Canadian dollars per kilogram. Your Uncle assumes the exchange rate of US dollars to Canadian dollars is US\$ 1 = C\$1.26. You have offered to solve this problem for him. The program needs to be user friendly, prompting for input and producing nicely formatted output.

2) A furniture store has just lost the records of the wholesale prices it paid for certain items that it recently sold. However, it still has the records of how much customers paid for these items. The ticket price of an item is always 40% above the wholesale price, and 6.5% retail sale tax is levied on each item. Design and implement a solution to the problem of determining wholesale prices of items from the amounts of money customers paid to purchase the items. The program needs to be user friendly, prompting for input as required including the customer name and item name. The output should be nicely formatted for ease of understanding.

Happy programming!

10. Member
Join Date
Jan 2013
Posts
71

## Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).

probably a good idea. I didn't want to flood the first page with a bunch of noob questions. I wish i could install the compiler here at work, but they wont let me. So I'll do these this weekend. I may type them out in notepad or something here at work, but that's as much as i can do at work But I think i will post the codes for these 2 practice programs on this thread, but any further questions on other stuff, ill go ahead and create a new one.

I like this forum! You guys have been a great help! Hopefully one day, I can be as helpful to others!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•