Page 1 of 1

USEFUL C++ TUTORIAL!!!

Posted: Fri May 07, 2010 2:53 am
by wrighty1986
What we are going to code today is quite something. Basically, You will ask the user how many arrays you want create. And you will ask values that the user wants through his input(integer values only!(for now)). And we are going to take the values the user inputed and organize it and save it into a text file called "Datas.txt".

This can be very helpful for beginners. I hope you guys learn something from it.
Code: Select all
//Necessary knowledge... Dynamic Memory Allocation
//Really cool Program!. No joke!

//Declaring our Headers
#include <iostream> // input and output functions
#include <fstream> // input and output file functions
#include "windows.h" // Sleep Function

using namespace std;

int main()
{
    //Title Code for the program
    system("title Array Creator, Manager, and Storer");
    //Declaring necessary integers
    int i,n;
    /*This is a pointer because it is necessary to Allocate the Memory
    (I, myself don't completely know why)*/
    int * p;
    //Asking the how many arrays does the User Wants
    cout << "How many Arrays:";
    //Getting input
    cin >> i;
    /* P is pointing to the address of 'i' which is dynamically Allocated
    using the keyword 'new' it is int because We are only recieving integer values
    plus, you will get an error if you make it new 'char' */
    p = new int[i];

    //If user enters 0, there won't be 0 arrays! Duh!
    if(p == 0)
    {
        //Message
        cout << "Arrays Cannot be Created...";
    }

    /*This loop is going to run as many times as the value
    the user inputed*/
    for(int n=0; n<i; n++)
    {
        //Message
        cout << "Enter your Array[" << n << "]:";
        //Input
        cin >> p[n];
    }
        //My Favorite part. 
        /*ofstream is a data type inside <fstream.h> which stands for
        'output file stream'. We are going to name it dataP*/
        ofstream dataP;
        //dataP.open creates the file if not existing with the name
        dataP.open("Datas.txt");
        /*This is the easy part. To actually input something to the file
        you simply do like a cout statement. dataP << Message;*/
        dataP << "~Array-Datas~\n\n";
        /*This for loop is to enter the values we got from the previous for loop
        to the File*/
        for(int n=0; n<i; n++)
        {
            //Organizes very neatly!
            dataP << "Array[" << n << "]: " << p[n] << "\n";
        }
        dataP.close(); // this is important, This saves the data.
        //This is just so extra Junk to spice up the program to look official! Lame!
        system("CLS"); // clears Screen
        cout << "Sending Datas from Arrays to Datas.txt" << endl; // Message
        Sleep(1500); // Sleep. The value is in Milliseconds. which means 1500 is 1.5 seconds
        cout << "Check the Program Directory for datas"; // Message
        cout << endl;
        system("PAUSE>NUL"); 
        return 0;
} 

Re: USEFUL C++ TUTORIAL!!!

Posted: Sat May 15, 2010 12:18 am
by zachman61
wow i like C++ but never new anything lol ii like it

Re: USEFUL C++ TUTORIAL!!!

Posted: Sat May 15, 2010 8:04 am
by wrighty1986
lol c++ is harder then vb i like vb better my self .

Re: USEFUL C++ TUTORIAL!!!

Posted: Sat May 15, 2010 10:09 am
by mikethedj4
I agree C++ is very confusing to me, but I would so love to learn it along with C#, I know a little Java and Ruby. (I know Java more than any programming language)