Jump to content

Help to create NotePad in c++

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
21 replies to this topic

#1
mindblaster

mindblaster

    Newbie

  • Members
  • PipPip
  • 17 posts
this is my program to create notepad in c++. i have some difficulties to count line number to remove character. I have done removing charecter in given number.

help me...
#include<conio.h>

#include<stdio.h>

#include<string.h>

#include<iostream.h>


class NotePad

{

	private:

		int x;

		int y;

		int key;

		int total_chars;

		int total_words;

		int total_sentance;

		int line[100];

	public:

		void clipboard()

		{

			x=1,y=1,total_words=0,total_chars=0;

			int line_index=0;


			while(key!=27)

			{

				gotoxy(1,50);cout<<"Col: "<<x<<" ";

				gotoxy(11,50);cout<<"Rows: "<<y;

				gotoxy(21,50);cout<<"Total Chars: "<<total_chars;

				gotoxy(40,50);cout<<"Total Words: "<<total_words;

				total_chars++;

				gotoxy(x,y);

				key=getch();

				gotoxy(x,y);printf("%c",key);

				if(key==13)

				{

					y++;

					line[y]=x;

					x=1;

				}

				else if(key==32)

				{

					total_words++;

				}

				else if(key==8)

				{

					x=line[y]-1;

					gotoxy(x,y);cout<<" ";

				}

				else

				{

				 x++;

				}

			}

		}

};



void main(void)

{

	clrscr();

	NotePad np;

	np.clipboard();

	getch();

}


Edited by mindblaster, 23 August 2009 - 12:35 AM.

Posted Image
Join SuperDiscountShop.com at Facebook.

#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
Care to be more specific on what exactly the problem is? Your code looks ok, though I'm wondering why you're using conio.h. It's pretty old and definitely not standard.
sudo rm -rf /

#3
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Yeah, conio kind of doesn't work anywhere but Windows, at least use ncurses... that's still updated!
Wow I changed my sig!

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
if(key==13)
{
	y++;
	line[y]=x;
	x=1;
}
else if(key==32)
{
	total_words++;
}
else if(key==8)
{
	x=line[y]-1;
	gotoxy(x,y);cout<<" ";
}

This is bad. No, no, no, no. Not portable, not clear at all. This might run on Linux but will fail on Windows, or vice-versa.
switch(key)
{
case '\n':
	y++;
	line[y]=x;
	x=1;
	break;
case ' ':
	total_words++;
	break;
case '\t':
	x=line[y]-1;
	gotoxy(x,y);
	cout<<" ";
	break;
}

sudo rm -rf /

#5
mindblaster

mindblaster

    Newbie

  • Members
  • PipPip
  • 17 posts

dargueta said:


if(key==13)

{

	y++;

	line[y]=x;

	x=1;

}

else if(key==32)

{

	total_words++;

}

else if(key==8)

{

	x=line[y]-1;

	gotoxy(x,y);cout<<" ";

}


This is bad. No, no, no, no. Not portable, not clear at all. This might run on Linux but will fail on Windows, or vice-versa.

switch(key)

{

case '\n':

	y++;

	line[y]=x;

	x=1;

	break;

case ' ':

	total_words++;

	break;

case '\t':

	x=line[y]-1;

	gotoxy(x,y);

	cout<<" ";

	break;

}


its run on windows

#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
Operative word being might. You can't just rely on the character codes being correct like that. At the very least swap '\t' for 8 and so on.
sudo rm -rf /

#7
outsid3r

outsid3r

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 623 posts
He is probably using turbo C++...

#8
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
I don't know why people still use that. There are plenty of more modern--and free--things out there.
sudo rm -rf /

#9
outsid3r

outsid3r

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 623 posts
I agree, probably it's because of his school, there are plenty of schools that still use turbo C to teach, and there is one thing even more stupid, some schools even use pascal to teach programming!

#10
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
It makes me cry inside...

By the way, standard C++ headers don't have .h extensions. Use iostream, not iostream.h - the latter is older and not really supported anymore except for backwards compatibility. Same goes for the old standard C headers. Just prefix a 'c' and drop the .h extension. Your includes should look like:

#include <iostream>
#include <cstdio>  //stdio.h
#include <cstring> //string.h
#include <conio.h>  //note - not a standard C header, so leave it alone.

sudo rm -rf /

#11
outsid3r

outsid3r

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 623 posts
And there are functions from windows API to deal with dos window like setting colors and cursor positions, he should definitely use windows specific functions, not conio.

#12
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
I actually wrote a wrapper class for that that's relatively easy to use...lemme find that.

EDIT: Ok, I found the class, but I also found a bug in it, so I'm just posting the header for now until I fix it.
#ifndef __WINCONIO_CPP_H__
#define __WINCONIO_CPP_H__

#include <windows.h>
#include <tchar.h>
#include <string>

using std::string;
using std::wstring;

typedef struct
{
    TCHAR ch;
    WORD wAttr;
}ATTRCHAR;

class WinConOut
{
protected:
    static DWORD dwRefCount;
    static HANDLE hStdOut;
    static HANDLE hAccess;
    static bool visible;
    static CONSOLE_SCREEN_BUFFER_INFO csbi;
    static WinConOut * const __inst;
public:
    //////////////CONSTANTS////////////////////////////
    static const int COLOR_FORE_BLACK           = 0x00;
    static const int COLOR_FORE_BLUE            = 0x10;
    static const int COLOR_FORE_GREEN           = 0x20;
    static const int COLOR_FORE_AQUA            = 0x30;
    static const int COLOR_FORE_RED             = 0x40;
    static const int COLOR_FORE_PURPLE          = 0x50;
    static const int COLOR_FORE_YELLOW          = 0x60;
    static const int COLOR_FORE_WHITE           = 0x70;
    static const int COLOR_FORE_GREY            = 0x80;
    static const int COLOR_FORE_GRAY            = 0x80;
    static const int COLOR_FORE_LIGHT_BLUE      = 0x90;
    static const int COLOR_FORE_LIGHT_GREEN     = 0xA0;
    static const int COLOR_FORE_LIGHT_AQUA      = 0xB0;
    static const int COLOR_FORE_LIGHT_RED       = 0xC0;
    static const int COLOR_FORE_LIGHT_PURPLE    = 0xD0;
    static const int COLOR_FORE_LIGHT_YELLOW    = 0xE0;
    static const int COLOR_FORE_BRIGHT_WHITE    = 0xF0;

    static const int COLOR_BACK_BLACK           = 0x00;
    static const int COLOR_BACK_BLUE            = 0x01;
    static const int COLOR_BACK_GREEN           = 0x02;
    static const int COLOR_BACK_AQUA            = 0x03;
    static const int COLOR_BACK_RED             = 0x04;
    static const int COLOR_BACK_PURPLE          = 0x05;
    static const int COLOR_BACK_YELLOW          = 0x06;
    static const int COLOR_BACK_WHITE           = 0x07;
    static const int COLOR_BACK_GREY            = 0x08;
    static const int COLOR_BACK_GRAY            = 0x08;
    static const int COLOR_BACK_LIGHT_BLUE      = 0x09;
    static const int COLOR_BACK_LIGHT_GREEN     = 0x0A;
    static const int COLOR_BACK_LIGHT_AQUA      = 0x0B;
    static const int COLOR_BACK_LIGHT_RED       = 0x0C;
    static const int COLOR_BACK_LIGHT_PURPLE    = 0x0D;
    static const int COLOR_BACK_LIGHT_YELLOW    = 0x0E;
    static const int COLOR_BACK_BRIGHT_WHITE    = 0x0F;

    static const int ATTRIB_DEFAULT             = 0x07;
    static const int ATTRIB_HIGH_INTENSITY      = 0x0F;
    ////////////////////////////////////////////////////
    WinConOut(void);
    ~WinConOut(void);
    
    //hides the console window
    void hide(void);

    //shows the console window
    void show(void);

    //sets the default character attributes, i.e. foreground and background
    //color. note that unlike Linux this does not allow you to do fancier
    //things such as underlining and blinking. that requires loading ANSI.SYS
    //at boot time.
    bool set_attr(int attr);

    //retrieves the current character attributes
    int get_attr(void) const;

    //moves the cursor to the specified location, returns false on failure
    bool move_cursor(int x, int y);

    //returns the current x coordinate of the cursor
    int get_cursor_x(void) const;

    //returns the current y coordinate of the cursor
    int get_cursor_y(void) const;

    //retrieves information about the console
    void get_info(CONSOLE_SCREEN_BUFFER_INFO& buffer) const;

    //gets the width, in characters, of the console
    int get_width(void) const;

    //gets the height, in characters, of the console
    int get_height(void) const;

    //sets the width and height of the console in characters
    bool set_dimensions(int x, int y);

    //printf
    int printf(LPCTSTR format_string,...);

    //printf at a specific coordinate; the cursor does not move from its
    //current position.
    int printf(int x, int y, LPCTSTR format_string,...);

    //printf with attributes - allows you to print to the console using
    //character attributes other than the default currently set.
    int printfa(LPCTSTR format_string, int attribute,...);

    //printf with attributes and coordinates
    int printfa(int x, int y, LPCTSTR format_string, int attribute,...);


    //retrieves the title string of the console
    size_t get_title(LPTSTR buffer,size_t buflen);

    //sets the title string of the console
    bool set_title(LPCTSTR title);


    //clears the entire screen
    bool cls(void);

    //clears a region of the screen and sets the default attribute for that region
    bool clear(int upperx, int uppery, int lowerx, int lowery, int attribute = ATTRIB_DEFAULT);

    //fill the screen with a specific character and attribute
    bool fill(TCHAR fillch, int attribute);

    //fill a region of the screen with a specific character and attribute
    bool fill(int upperx, int uppery, int lowerx, int lowery, TCHAR fillch, int attribute);


    //copies a region of the screen into a buffer that stores both the character and attribute
    bool save_region(int upperx, int uppery, int lowerx, int lowery, ATTRCHAR*& buffer) const;

    //copies a char/attr buffer onto the screen.
    bool load_region(int upperx, int uppery, int lowerx, int lowery, const ATTRCHAR*& buffer);


    //these operators should be used instead of COUT and CERR. Using those will break this.
    WinConOut& operator<<(bool data);
    WinConOut& operator<<(char data);
    WinConOut& operator<<(unsigned char data);
    WinConOut& operator<<(short data);
    WinConOut& operator<<(unsigned short data);
    WinConOut& operator<<(int data);
    WinConOut& operator<<(unsigned int data);
    WinConOut& operator<<(long data);
    WinConOut& operator<<(unsigned long data);
    WinConOut& operator<<(double data);
    WinConOut& operator<<(float data);
    WinConOut& operator<<(long double data);
    WinConOut& operator<<(long long data);
    WinConOut& operator<<(unsigned long long data);
    WinConOut& operator<<(TCHAR data);
    WinConOut& operator<<(LPCTSTR str);
    WinConOut& operator<<(const string& str);
    WinConOut& operator<<(const wstring& str);
};

#endif

Edited by dargueta, 20 August 2009 - 05:04 AM.
Found bug

sudo rm -rf /