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
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.
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!
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!
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!