you're reading...

CS1021C Collection of Simple C/C++ Programs

... beware: C++ code entered here ...

The Size in Bytes of C++ Standard Data Types

#include <iostream>
using namespace std;

int main() {
    cout << "\nCalculating the Size of Data Types in C++ using Sizeof \n";
    cout <<"\nChar: " << sizeof(char);
    cout <<"\nShort: " << sizeof(short);
    cout<<"\nInt: " << sizeof(int);
    cout<<"\nLong: " << sizeof(long);
    cout<<"\nFloat: " << sizeof(float);
    cout<< "\nDouble: " <<  sizeof(double);
    cout  << "\nLong Double " << sizeof(long double);

    double amtd; int amtint;
    cout  << "\nSize of double variable amtd " << sizeof(amtd);
    cout  << "\nSize of int variable amtint " << sizeof(amtint);

    double distance = 92.96E6; //Thanks Google
    cout << "\n Distance from Earth to the Sun is: " << distance << " miles.";
    float milesToMeters = 1609.34;
    cout << "\n Distance from Earth to the Sun is: " << distance * milesToMeters << 
                      " meters.";

return 0;

A Filter

This program uses getchar() and putchar() to read a char from standard input (stdin) and writes a character to the standard output (stdout).

#include <stdio.h>
#include <ctype.h>
int main(void) {
int c;
while ((c = getchar()) != EOF)
return 0;
FREDs-MacBook-Air:~ fred$ g++ filter.cpp -o filter
FREDs-MacBook-Air:~ fred$ echo "HELLO THeRE X3" | ./filter
hello there x3

How to encrypt a message in C++ using a Caesar cipher

The Caesar cipher is one of the earliest known and simplest ciphers. It is a type of substitution cipher in which each letter in the plaintext is ‘shifted’ a certain number of places down the alphabet.

#include <iostream>
#include <string>
using namespace std;
int main() {
 string plain;
 cout << "Enter a message(lower case): "<< endl;
 getline(cin, plain);
 int k;
 cout << "Enter integer encryption key: ";
 cin >> k;
 string cipher = "";
 for(int i=0; i<plain.length(); i++){
   char c = plain[i];
   if ((c >= 97) && (c <= 122))
     cipher +=(((c-97)+ k) % 26) + 97;
 cout << "Encrypted message is: " << cipher <<endl;


No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: