Thread: anyone can help?beginner nd help...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    9

    anyone can help?beginner nd help...

    cout<<"Please enter your name:";
    cin>>user.name;
    int i=0;
    char name[i];
    while (name[i])
    {
    if (isalpha(name[i])) cout<<"Valid",name[i];
    else cout<< "Invalid name entry",name[i];
    i++;
    }


    i want the user enter only alphabet and not number... wat code should i use? above code not working

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    What errors are you getting? I can see a few things wrong at the moment.

    Also, please use code tags.

  3. #3
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    That array is of size zero so it wont hold anything because you have 'i' = 0.

    You could do something along these lines:

    Code:
    char name[32];
    int i = 0;
    
    cin.getline(name, sizeof(name));
    
    while(isalpha(name[i]) && i < sizeof(name)) i++;
    
    if(!isalpha(name[i]) && name[i] != '\n'){
    //...invalid
    }
    else{
    
    }
    Last edited by Syscal; 10-01-2010 at 09:27 AM.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    9
    the output become invalid name entry invalid name entry valid no matter wat i typed... wat is code tags?

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Regarding code tags, read the sticky on this forum.

    The first thing I notice about your code is that you are storing the name inside user.name
    I am assuming user is an instance of a class?
    You are never iterating through that name. In fact you are not doing anything with that (apart from storing it).

    Look at Syscal's code for some ideas.

  6. #6
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by silent View Post
    the output become invalid name entry invalid name entry valid no matter wat i typed... wat is code tags?
    code tags allow for the white space in code to be preserved on the forums instead of being aligned on the left margin. to use them, either click on the "#" icon or type [code ] code goes here[/code] w/o the space in [code ]

    it's the difference between:
    if (foo)
    bar();
    else
    baz();

    and:

    Code:
    if (foo) 
        bar();
    else 
        baz();
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    To improve Syscal's code:
    Code:
    std::string name;
    int i = 0;
    
    std::getline(name, cin);
    
    while( isalpha(name[i]) && i < name.length() ) i++;
    
    if(!isalpha(name[i - 1]))
    {
        //...invalid
    }
    else
    {
        // Valid
    }
    Certainly not the best way to do it, but it works.
    And you cannot, and I stress this, create an dynamic array like so:
    char name[i];
    Where i is a non-const variable.
    Furthermore, you have no business user char arrays for string in C++.
    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