Thursday 27 October 2011

cin.get() not pausing??

As I wrote in one of my post - why you should use cin.get() when you test a program (apply to IDE that wont wait as you test a code and as soon as the code finished the window closes, leaving you without a glance on the result =p).

Let's look at this example:
#include <iostream>
using namespace std;
main()
{
    for(int x=0;x<20;x++)
    {
            cout<<x<<endl;
    }
    cin.get();
}

But sometimes, cin.get() might refuses to work, as in this example here:-
#include <iostream.h>
    main()
    {
        int hours, minutes, seconds;
       
        cout <<"Input hours: ";
        cin  >> hours;
        cout <<"Input minutes: ";
        cin  >> minutes;
       
        seconds=hours*3600+minutes*60;

        cout << "Time in seconds : "<<seconds<<endl; 
        cin.get();
        return 0; 
      }

The window closes taking all the data with it, and you not even get a chance to see the result.

What happen here actually? 
Why cin.get() works in the first example but not in the second?
In the second example, after the program read the value using cin, a \n is left in the buffer. Thus the cin.get() that you meant for pausing consumes that \n.

What you can do??
You may use two cin.get(). Which the first call to cin.get() is used to consumes that \n left in the buffer and the second cin.get() to pause your windows from closing.

Or, you may flush the \n from the buffer by using cin.ignore()

#include <iostream.h>
    main()
    {
        int hours, minutes, seconds;
       
        cout <<"Input hours: ";
        cin  >> hours;
        cout <<"Input minutes: ";
        cin  >> minutes;
       
        seconds=hours*3600+minutes*60;

        cout << "Time in seconds : "<<seconds<<endl; 
        cin.ignore();
        cin.get();
        return 0; 
      }

Why you should avoid system("pause") in C++

In windows, the system() function is used to execute any operating system command, like dir, ls, rename, etc. However, it is not encourage to use system("pause") to pause command windows from closing in C++. There are two main reasons why:-
  1. A call to system("pause") will pause the programs execution and make a system call and thus allocate unnecessary resources.
  2. Another reason it should be avoided is because it isn't portable. It works only on systems that have the PAUSE command at the system level, like DOS or Windows.
system("pause") is pretty popular and usually introduced to new beginners. It provides a clean and perfect pausing action. Also commonly used in debugging.

That means, when you do system("pause"), it does: Press any key to continue...

Just ANY key on your keyboard! Even an arrow or a space bar will do. 

Unlike cin.get(), it didn't show anything, or what you suppose to press to exit. Until you figure it out, you probably hit almost every key on your keyboard, and finally accidentally hit on the RETURN key. 

Of coz, both code: system("pause") and cin.get() does the same job, but quoting from a friend, using system("pause") is like using a bulldozer to open your front door. It works, but you can open your front door in an easy way, do you? So, why would you want to waste the unnecessary resources?

In spite of all the bad things most programmer would said about system("pause"), and how you shouldn't mix an operating system command with a C++ programming code, I reckon system("pause") is introduced to new beginner to make their life easier in learning  and exploring programming language. Just like how we were taught that gravity is equal to 10 when we were in primary school, and then few years later they told us that actually we should calculate gravity as 9.81.You wouldn't tell a little boy that he should calculate gravity as 9.81 while he is still struggling with multiplication involving decimal, would you??

So, it's your call if you wanna be a beginner and stay as a beginner, or evolve and be a professional =)

Friday 21 October 2011

UFS103/201109 - PLANNER

UFS103 - PC COMPETENCY
Credit hours : 3

Classcode: 23-201108D270029
Group A - Tuesday (9-1pm) 
Assignment 1  : 10% (20 Sept 2011)
Assignment 2  : 10% (4 Oct 2011)
Assignment 3  : 10% (11 Oct 2011)
Assignment 4  : 10% (18 Oct 2011)
Assignment 5  : 20% (25 Oct 2011)
Final Project   : 40% (23 Nov - 14 Dec 2011)

Classcode: 23-201108D270035
Group B - Wednesday (2-6pm)
Assignment 1 : 10% (21 Sept 2011)
Assignment 2 : 10% (5 Oct 2011)
Assignment 3 : 10% (12 Oct 2011)
Assignment 4 : 10% (19 Oct 2011)
Assignment 5 : 20% (9 Nov 2011)
Final Project : 40% (23 Nov - 14 Dec 2011)

Click here for your Final Project score-sheet.

Wednesday 19 October 2011

EDS105/201109 - PLANNER

EDS105 - FUNDAMENTAL OF PROGRAMMING
Classcode: 23-201109D060026
Credit hours : 3

Following are your assessments evaluation:
Assignment 1 (Quiz) : 10% (5 October 2011)
Assignment 2 (Lab 1,2) : 10% (Deadline : 16 October 2011, before 11:59pm)
Assignment 3 (Lab 3,4) : 10% (Deadline : 22October 2011, before 11:59pm)
Assignment 4 (Lab 5,6,7) : 20% (Deadline : 12 December 2011, before 11:59am)
Midterm (Ch 1 - 5) : 10% (3 November 2011, 4-5.30pm)
Final exam : 40% 

Others:
Click here for your assignment format.

LESSON PLAN

Week 1
14/09/2011
Introduction
Week 2
21/09/2011
Chapter 1
Week 3
28/09/2011
Chapter 2
Week 4
05/10/2011
Chapter 3
Week 5
12/10/2011
Chapter 4
Week 6
19/10/2011
Chapter 5





MIDTERM
02/11/2011
Chapter 1-5
BREAK
09/11/2011






Week 7
16/11/2011
Chapter 6
Week 8
23/11/2011
Chapter 7
Week 9
30/11/2011
Chapter 8
Week 10
07/12/2011
Chapter 9
Week 11
14/12/2011
Summary: Presentation
Week 12
21/12/2011
Revision 1: Compiling
Week 13
28/12/2011



Revision 2
Week 14
10/01/2012
Tuesday
10:30-12:30pm
Library
15ActiveStudents

Final Exam

Tuesday 18 October 2011

MinGW - Getting Started

What is MinGw??
MinGW (Minimalist GNU for Windows), formerly mingw32, is a native software port of the GNU Compiler Collection (GCC) and GNU Binutils for use in the development of native Microsoft Windows applications; 

MinGW can function either as a cross compiler targeting Windows or as a native toolchain run on Windows itself. It is deployed along with a set of freely distributable import libraries and header files for the Windows API.

Source : Wikipedia


First you have to be familiar with basic DOS command. Go here for basic cmd.
Before continue, make sure you have installed MinGW.


Open up an elevated command prompt window and set the current directory to where your *.cpp file is.

Example:
For the file calculator.cpp in the folder C:\doc, change your directory using the following command:

cd c:\doc

This is the compile command:

g++ calculator.cpp -o calculator.exe

The -o switch defines the name of the output file.
Now you can run your program by typing the name of the executable file:

calculator.exe


Monday 17 October 2011

Change the Title Tags in Blogger

  1. Go to Design>edit HTML in your Blogger dashboard.
  2. Search for tag: <title><data:blog.pageTitle/></title>
  3. Replace the tag with the following code:
  4. <b:if cond='data:blog.pageType == &quot;index&quot;'> <title><data:blog.title/></title> <b:else/> <title><data:blog.pageName/> | <data:blog.title/></title> </b:if>
  5. Save your new template.
  6. View your blog. You should see the results in your web browser as shown below.







Wednesday 12 October 2011

C++ in Bloodshed tips & shortcuts

In Bloodshed
To compile: ctrl + F9
To run: ctrl + F10
To compile & run: F9

To get your command prompt screen to stay open..
cin.get();
Click here for More about cin.get ();

system("pause");
Click here for Why you should avoid system("pause")

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

As you are trying to test several small scripts (that related to each other) ,some ppl tend to open new source file (ctrl+N) for every small scripts. Here is something you can try, run and test all the small scripts under one source file and isolate each scripts by using the block comment.


MATLAB SIMULINK tips & shortcut

Connecting blocks:
  1. Right-click on signal and drag to create several connections emerging from same block
  2. Left-click and drag to reroute lines
  3. Shift-left-click to create breakpoint
  4. Left-click on breakpoint to move breakpoint
  5. Alt+Ctrl+left-click to connect 2 blocks
Viewing model:
  1. Hot key r to zoom in
  2. Hot key v to zoom out
  3. Spacebar to see whole model in window

Basic DOS command tricks

Basically these are just simple cmd.exe commands for beginner to get started. Actually most people don't even know what is cmd and as soon as you open it, they're like 'oh no! what's this?? where is the icon, color and everything??' (the GUI stuff). But once you get used to the environment, you will find it useful especially when your computer get stuck with the heavy-beautifully-designed-colourful-GUI at the back =p


1. To open cmd
win+R > cmd


2. Manipulating directory

To change directory. (If you are unsure on the exact spelling of a folder, press TAB and use the up and down arrows to scroll through the folders that finish what you started typing.
cd c:\doc


To go up 1 level
cd ..


To list all of the directories
dir


3. New look for your cmd
Sometimes you may want to impress your girlfriend. How about a different look of cmd from everyone else??

C:\Users\angelKeshet>color y

C:\Users\angelKeshet>color 0a

Check on the list of color listed. The first number represents the background color, and the second character represents text color. Now you can play as Keanu Reeves in the The Matrix =p


4. ping
This is a famous command that allow you to check if the host you pinging is 'alive'. A simple check of your internet connection.
C:\Users\angelKeshet>ping google.ca

5. ipconfig
This command will show your IP address, gateway, DNS in use etc.
C:\Users\angelKeshet>ipconfig

6. Others
Clear screen

cls

To quit cmd
exit





Assignment format

Pls use the given format here for your assignment submission