Just did my overview of the first 3 chapters :D:D

I just created a code encompassing all the techniques in the first three chapters of my book. I'm brand new to programming and have started with C++ because it applies to video games (I'm an audio engineer but I thought knowing a bit about programming would help me work with the programmers a bit more closely).

One thing is I can't figure out how to make a new line happen after "for and char" lines. Another thing is I am confused by the differentiation literal constants vs symbolic constants. A literal constant is the an inputting of lets say, a number like "10". So "std::cout << (10 * 8)" would be multiplying the literal constants 10 and 8? Where a symbolic constant would have defined 10 as lets say, "myAge" like so "int myAge = 10" Am I right in this? Also I am not 10 lol.

Is there anything glaringly bad in this or in my analysis? I made the "//" analyses just so I can check this code when I need to review instead of painfully backtracking in the book anymore :P

Techniques Covered:
- Namespace
- Typedef
- Functions
- Symbolic, Literal, and Enumerated Constants
- How to define a variable (signed, unsigned, short, long etc...)

Thanks guys!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>

using namespace std;
typedef unsigned short int USHORT; // This makes it so I don't have to type UNSIGNED INT infront of every int value to define it's type
typedef signed short int SHORT; // This makes it so I don't have to type SIGNED INT just like the first case

const USHORT MyAge = 24; /* Defining the variables used by the program. That they are constantly the same throughout(const), that they are Unsigned short (USHORT), 
what the symbolic reference is (MyAge), and finally what the variable's value is (24)*/
const USHORT DaysYr = 365;
const USHORT SchoolYr = 17;


void Textfunction() // This is a simple text function which can be called at any point in the main() but typing "Textfunction()"
{
     cout << "This is the text function\n";
}

int main()
{
    cout << "First line of text directly outputted to the console\n";
    cout << (MyAge * DaysYr) << "\n"; // notice how the (<< "\n";) is used to make sure the next line is indeed printed as the NEXT line instead of the same line. Do this when created arimetical lines.
    Textfunction(); // This is the first call to the function
    for (int i = 32; i<128; i++)
    cout << (char) i;/* This is a "for" loop. I declare that "i" is REPRESENTING the integer (int) value of 32. 
    I then declare that "i" will enumerate itself up to 128. I then declare that it is to count up by ones (++). This is how CHAR is done and symbols based on the ASCII ledger are created */
    cout << "Hopefully the characters on the above line worked properly \n";
    Textfunction();
    cout << "USHORT caps at 65,535 and ULONG caps at 4,294,967,295 \n"; /* Just so you know how many intergers these two common variable definions use. 
    Signed versions split the cap in half and go into the negative. */
    
    enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }; // This is an enumerated constant. Each day is assigned a value starting from 0. So in this case it is 0-6 for the days.  
    Days today; // This declares that symbolic input to choose which of the values you want to use
    today = Sunday; // This is were you choose your value that you want your program to use from the enumerated list(Sunday)
    if (today == Sunday || today == Saturday) // This declares that "if today is saturday or sunday, then the output will be as follows,
    cout << "Gotta love the weekends!\n";
    else // This declares that if the choosen value is anything else (other than Saturday or Sunday) then the output will be as follows.
    cout << "Back to work :(\n"; // This is the end of the enumerated constant sequence. 
    Textfunction();
    
    /* These next twos lines of code are needed in this compiler specifically in order to keep the program onscreen and running instead of a flash on and off. */
    char response; 
    cin >> response;
    return 0;
}
Last edited on
You might want to replace your '\n's with an endl, it flushes the buffer as well as places it on another line.
Last edited on
or '\n' << std::flush;
and btw short int and short are the same just like int/long int is same as long
long long int is same as long long

Techniques Covered:
- Namespace

Where in your code are you using a namespace?

Also, if you are interested to know:
1) Avoid using global variables. Your constants might kind of slide through this convention, but even then you should wrap them in a namespace(hint) to avoid name conflicts. There is a keyword called using which allows you to use the variables without specifying the namespace, e.g.
1
2
3
using std::cout;
//...
cout << "whaddup" << endl;

2) Also avoid using using namespace std;. There is a humongous number of functions and classes in the std namespace that you will never use all in one file. Just use the using keyword mentioned above. This will help you avoid name clashes with the numerous items in std when you happen to name your stuff the same. For an idea of all the std:: items, you can look through this site's reference section and count the number of "std::'s" in the titles.
Topic archived. No new replies allowed.