please answer my query

This is a discussion on please answer my query within the C++ Programming forums, part of the General Programming Boards category; please check the following code. arr should the 5 character array that we enter but in output it is appended ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Location
    bhopal
    Posts
    1

    Exclamation please answer my query

    please check the following code. arr should the 5 character array that we enter but in output it is appended by some extra characters may be they are garbage. Please tell me why it is happening and how to modify the code?
    Code:
    #include<iostream.h>
    #include<conio.h>
    void main()
    {
    	clrscr();
    	char arr[5];
    	for(int i=0;i<5;i++)
    	{
    		arr[i]=getch();
    		cout<<"*";
    	}
    	cout<<endl<<arr;
    	getch();
    }

  2. #2
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    i think getch(), use (cin.get() c++ code) returns when it sees '\n' on next loop..
    after getch()..add cin.get() or getch() again and that should consume the extra crap..
    You ended that sentence with a preposition...Bastard!

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The garbage is because your string is not null terminated. C and C++ store strings in char arrays with a 0 at the end to tell the output call (cout, printf() etc.) where to stop.

    In your case, the simple way to fix the problem is to make your array 1 bigger than your string and initialize it to all 0s...
    Code:
    char arr[6] = {0};

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    The solution is to write portable, standard C++ code:
    Code:
    #include <iostream>
    
    int main()
    {
    	std::string string;
    	std::getline(string, std::cin);
    	std::cout << "*****\n" << string;
    }
    Get rid of iostream.h: it's non-standard.
    Get rid of conion.h and getch: they're non-portable.
    Use C++ facilities instead of C facilities.
    Use int main instead of void main! void main is not standard!
    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.

  5. #5
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    dang! of course null terminated..reminds me why I'm kinda glad I am not studying C :O
    You ended that sentence with a preposition...Bastard!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dns query
    By bulldog in forum C Programming
    Replies: 6
    Last Post: 02-24-2004, 09:44 AM
  2. SQL Query
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 12-24-2003, 08:06 AM
  3. movement rendering query....pls answer promptly
    By technoXavage in forum Game Programming
    Replies: 3
    Last Post: 11-30-2003, 05:50 AM
  4. Msg Box query
    By face_master in forum Windows Programming
    Replies: 8
    Last Post: 12-06-2002, 01:51 PM
  5. Query!
    By VegasSte in forum C Programming
    Replies: 12
    Last Post: 10-15-2002, 02:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21