Respected All :)

This is a discussion on Respected All :) within the C++ Programming forums, part of the General Programming Boards category; Hi, Hope you all are great there?? Well apologies for the last posts, also thanks Malc for telling the rule. ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    6

    Respected All :)

    Hi,

    Hope you all are great there??

    Well apologies for the last posts, also thanks Malc for telling the rule.

    I am new to the forum, also this is the first time I had joined any forum and I have joined it because I like the rule of it that it doesn't make the other user copy the stuff but help them where they get stuck.

    Despite of teaching English it is a very good forum and if everyone have problem reading my Post, kindly don't.

    Anyways, I am new to programming and I hope this forum will help me where i get stuck

    so here is my code as i said once the assignment due date is over i will post it:

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <ctype.h>
    
    int main ()
    {
      int i, n;
      int *pointerData;
      clrscr();
       
     
       cout<<"Enter the size of the array: ";
       cin>>i;
       
      if(!isalpha(i))
      { 
      pointerData = (int*) calloc (i,sizeof(int));
      
      if (pointerData==NULL)
    
        {
          cout<<"Unable to allocate memory for array."<<endl;
          return 0;
        }
    
      for (n = 0; n < i; n++)
      {
        cout<<"Enter number: ";
        cin>>pointerData[ n ];
      }
      cout<<"The elements of the array are:"<<endl;
    
      for (n = 0; n < i; n++)
          cout<<pointerData[ n ]<<" ";
    
        pointerData =(int*) realloc(pointerData, 2 * i * sizeof(int));
    
       if (pointerData==NULL)
    
        {
          cout<<"Unable to re-allocate memory for array."<<endl;
          return 0;
        }
    
        cout<<""<<endl<<"Enter the elements in the array after reallocation:"<<endl;
    
      for (n = i; n < i*2; n++)
      {
        cout<<"Enter number: ";
        cin>>pointerData[ n ];
      }
    
       cout<<endl;
    
      for (n = 0; n < i*2; n++)
          cout<<pointerData[ n ]<<" ";
    
      free (pointerData);
     }
      getch();
      return 0;
    }
    The only problem i get is when the user enter an alphabet, the programme is not running properly instead it get stucked in the loop.

    I hope anybody can help me out in this regard.

    Apologies in advance for English grammar.

    Regards

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The only problem i get is when the user enter an alphabet
    The program only accepts numbers as input because you store them in variables of type int. Inputting anything else makes cin fail and it won't be able to accept any more input as long as you don't ignore the unread characters and clear the error state.

    Code:
    if(!isalpha(i))
    I think this check indicates some kind of misunderstanding. You asked how many integers to allocate. What does isalpha have to do with that?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    6

    I want to Restrict the user from entering alphabets

    Hello,

    I want to Restrict the user from entering alphabets

    Regards

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There are two solutions here:
    • Preventing bad data from being entered.
      This is quite difficult even at trivial levels because you need to get on with low-level I/O interaction to accept/refuse individual characters of the input, which general also means doing any formatting/editing as well [even more complicated levels involve also understanding WHERE what parts can be entered, so for example knowing that you can only have ONE decimal dot in a floating point number, and '-' can only appear at the beginning or after the 'e' sign for exponent]
    • Refusing (and retrying) bad input.
      Pretty easy to do. Read data as a string, try to make it into whatever format it needs to be, and if it's not "ok". Repeat until actually correct data has been entered.


    --
    Mats
    Last edited by matsp; 06-02-2009 at 06:36 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

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