php hit counter

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
declared one varibale char s1[10] but when i am inputing the string
VU Help • House Banner VU Help Virtual University Students - Assignments, Solved Papers, GDB & Exam Help Student Help Forum • Collaborative • Free Support
#1
i declared one varibale char s1[10] but when i am inputing the string, it is accepting more than 9 characters. why it is like this? i have put different number in the bracket but there when i am puting the sting, there is no setriction. how the other characters are accepting and where are they going for save?



Thank you so much
Reply
#2
You are facing this problem becuase you are trying to overflow the buffer. When we print the array value using cin statement, cin reads a character array until the user presses the enter key. When enter key is pressed, cin takes the whole input (i.e. string) and stores it into the array name. Remember that if we put a string larger than the size of the array in absence of a null character in it, then it is not possible to determine where a string is terminated in the memory. This can cause severe logical error. So, one should be careful while declaring a character array. The size of array should be one more than the number of characters you want to store. To handle such issues, always use loop to print the character string stored in array. Below is the code which will help you understand the problem.
Code:
#include <iostream.h> #include<conio.h> main() { char s1[10]; cout<<"Enter your name:\t"; cin>>s1; for ( int i = 0 ; i < 10; i ++ ) cout<<s1[i]; getch(); system("pause"); }

Here in the above code, for loop is used to print the character array. Here the purpose of the for is to print the value of the array upto its upper limit and it will discard the remaining character which are stored beyond the character array limit.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why we write main rgc,char**argv in C++ 2012 Vuhelper 0 3,058 01-04-2012, 10:36 PM
Last Post: Vuhelper
  ASCII to char conversion code in C++ 2011 Vuhelper 0 1,771 11-05-2011, 05:01 PM
Last Post: Vuhelper
  Creating a Random String in c++ 2011 Vuhelper 0 1,342 06-14-2011, 09:14 PM
Last Post: Vuhelper
  Pass string value into function as an array in c++ Vuhelper 0 998 06-11-2011, 06:42 PM
Last Post: Vuhelper
  Generating substrings of a string in C++ 2011 Xpert 0 2,001 05-12-2011, 09:18 PM
Last Post: Xpert
  Memory pool & char array questions in C++ for 2011 Xpert 0 2,289 05-12-2011, 09:16 PM
Last Post: Xpert
  why we have declared these two TreeNode pointers left saneha 1 2,207 05-02-2011, 05:15 PM
Last Post: Vuhelper
  static member function can be declared explicitly? saneha 1 5,005 05-02-2011, 06:56 AM
Last Post: Vuhelper
  what this function do ? void student ::setName (char *aName); saneha 1 2,164 05-02-2011, 06:35 AM
Last Post: Vuhelper
  const. pointer or pointer to const. char saneha 1 2,773 04-29-2011, 08:47 AM
Last Post: Vuhelper

Forum Jump:


Users browsing this thread: 1 Guest(s)
VU Help • House Banner VU Help Virtual University Students - Assignments, Solved Papers, GDB & Exam Help Student Help Forum • Collaborative • Free Support