Thread: Wats wrong!!

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    385

    Wats wrong!!

    Following is a program which first inputs a stream of alphabets by the user and is then supposed to clear the screen partially. But it does not seem to be doing its job. Can u please help me find wats wrong with this.

    Code:
    #include<iostream>
    #include<stdio.h>
    
    int main()
    {
    	char *scr=(char*)0xB8000000;
    	char str[80],c;
    	int i,row=0,col=0;
    	puts("Type the alphabets");
    	while((c=getchar())!=EOF)
    	{
    		char *v;
    		v=scr+row*160+col*2;
    		*v=c;
    		c+=2;
    		if(c==80)
    		{
    			row++;
    			c=0;
    		}
    	}
    	
    	row=row/2; col=col/2;
    
    		while(1)
    		{
    			char *v;
    			v=scr+row*160+col*2;
    			*v=' ';
    			c-=2;
    			if(c==0)
    			{
    				row--;
    				c=80;
    			}
    			if(row==0 && col==0)
    				break;
    		}
               return 0;
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Where did you get that code from?! Have you heard about protected mode?
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Yes I have heard about the protected mode. It is an exercise program that I am trying, based upon the explainations and other codes that I read about VDU memory and printing characters to the screen. 0xB8000000 is the address of the VDU memory and anything that is written at this address (with the attrbutes of the characters at odd addresses) is supposed to be appear on the screen. So the blank space that I am trying to print should clear the screen partially ( row=row/2; col=col/2; ).

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by juice View Post
    Yes I have heard about the protected mode. It is an exercise program that I am trying, based upon the explainations and other codes that I read about VDU memory and printing characters to the screen. 0xB8000000 is the address of the VDU memory and anything that is written at this address (with the attrbutes of the characters at odd addresses) is supposed to be appear on the screen. So the blank space that I am trying to print should clear the screen partially ( row=row/2; col=col/2; ).
    Well, guess what: You can't. Not in user-space. You need the OS's functions to do that. It ( the OS ) has taken measures to ensure that neither you nor anyone else can dumble with any memory you desire!
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Not at all. I've done this in the past, I just can't find wat's wrong..

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    how far in the past? if you did it on DOS or Windows 95/98/ME, you'll find that a lot has changed since then. if you're doing it on some sort of embedded system, or if you're running this in a kernel module or driver, it's possible, but if it's a user-privilege program on any modern operating system, it simply will not work.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This is C, not C++. It would be appreciated if you could distinguish it enough to put it in its proper category next time.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    char str[80],c;
    int i,row=0,col=0;
    puts("Type the alphabets");
    while((c=getchar())!=EOF)
    The getchar function returns an int, not a char. This is important when comparing against EOF.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    Registered User
    Join Date
    Jul 2011
    Posts
    62
    Just a note about the "clear a screen".
    On windows, this can be easily done:

    Code:
    #define NOCOMM //this makes sure you don't load all the fat from the next header
    #include <windows.h>//contains the system function
    /* ...... */
    system("cls");
    /* ...... */
    This code only runs on windows though, so make the that dependant on the platform, the alternative on linux is described in this post:
    Returning to different parts of script. - C++ Forum.
    (note the name of the link is insubstantial).

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Elysia View Post
    This is C, not C++. It would be appreciated if you could distinguish it enough to put it in its proper category next time.
    but he/she included <iostream>

    doesn't that make it C++?

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Shingetsu Kurai View Post
    Just a note about the "clear a screen".
    On windows, this can be easily done:

    Code:
    #define NOCOMM //this makes sure you don't load all the fat from the next header
    #include <windows.h>//contains the system function
    /* ...... */
    system("cls");
    /* ...... */
    This code only runs on windows though, so make the that dependant on the platform, the alternative on linux is described in this post:
    Returning to different parts of script. - C++ Forum.
    (note the name of the link is insubstantial).
    Firstly, system() is declared in the standard C header, <stdlib.h>. It is not necessary to #include <windows.h> to use it, even on windows.

    The problem with system("cls") is that it is completely non-portable. Under windows, it relies on the command interpreter supporting a cls command that clears the screen. That implies a program that runs (under windows) within a console or command window. It is completely inapplicable to windows programs that support a windows GUI.

    Similar comments apply to the "alternative on linux" that you linked to.

    As mentioned in the link provided by AndrewHunter (and by other posters) clearing a screen is not standard C or C++. If you even need to care about clearing a screen, you need to be quite clear what "screen" actually means for your application, and accept that your code may be completely non-portable.

    You also need to accept that a lot of people will provide you with "simple" solutions that don't always work (because they assume you are doing something you are not). If you are using a library that addresses your needs (for example, console I/O functions in the win32 API, curses under linux) you need to accept you are locked into those libraries, and also realise your code may break when the library is updated or the operating system is updated.

    And the older the techniques are that you use (for example, using DOS interrupts or BGI (Borland Graphics Interface) calls under DOS) the more likely your program is to cease working when your operating system is upgraded. That is not because those techniques are bad (in fact, in their day, they were actually quite brilliant) but simply because life moves on. If it is not nailed down in a standard - and preferably a standard that can be applied cross-system - you are vulnerable to obsolescence.
    Last edited by grumpy; 10-01-2011 at 09:52 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Elkvis View Post
    but he/she included <iostream>

    doesn't that make it C++?
    It doesn't change the fact that it's C with a C++ header.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wats wrong!!
    By juice in forum C Programming
    Replies: 6
    Last Post: 09-26-2011, 04:54 AM
  2. Can any1 tell me wats wrong in this c program..
    By Hrisav in forum C Programming
    Replies: 2
    Last Post: 07-09-2009, 05:03 AM
  3. wats the fault...
    By ElemenT.usha in forum C Programming
    Replies: 17
    Last Post: 01-04-2008, 01:52 PM
  4. wats wrong with this code?
    By Marrah_janine in forum C++ Programming
    Replies: 20
    Last Post: 12-10-2006, 08:01 PM
  5. wats happening around me.
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 12-17-2002, 12:59 PM