Thread: string

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

    string

    Hi everyone! I am new to c++ programming and I want to ask you something.
    I want to make a program that checks if the user entered a valid name that contains only letters.
    I use the for loop and the strlen for the length of a string.But it doesn't work. Could someone please help me and show me an example ?
    Thanks in advance!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    How about you post your attempt (which doesn't work), then we can explain why it doesn't work, and how to fix it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    26
    Yes, first time I wrote this program :
    Code:
    #include <iostream>
    #include <string>
    #include <cctype>
    using namespace std;
    
    int main()
    { 
        char name[50];
    
        int i = 0;
        while (true) {
            cout << "Please enter your name.\n";
            cin >> name;
    
    
        if (isalpha(name[i])) {
            i = 0;
            i++;
    
            cout << "Nice to meet you.\n";
        break;
        }
        cout << "No, that's wrong.Enter your name again:\n";
        }
    
        return 0;
    
    }
    For a name like Sonny2 it displays the message 'nice to meet you' but this is not a valid name.
    After this, I wrote the following program using the boolean function.I thinks this is better but it keeps displaying the message 'No,that's wrong.Please enter your name ' even when it shouldn't.
    This is it:
    Code:
    #include <iostream>
    #include <cstring>
    #include <cctype>
    using namespace std;
    bool is_name(string);
    int main()
    { 
    char name[50] = {'\0'};
    int i = 0;
    
        do {
        cout << "Please enter your name.\n";
        cin >> name;
        
         if (is_name(name) == true) {
        
       
        cout << "Nice to meet you.\n";
        break;
        }
        
       
       cout << "No, that's wrong.\n";
       
        
        } while (is_name(name) == false);
       
    
    
       
    return 0;
    
    }
    bool is_name(string)
    {
        int i = 0;
        char name[50];
        for (i = 0; i < strlen(name) ;i++ ){
        if(isalpha(name[i])) {
       
        return true;
        }
        }
        return false;
    }
    I 'll appreciate if you tell me why it doesn't work and how I should fix it.
    By the way, how can I check the presence of the @ in a string ? If I'm not wrong it's a function for points (.) I want the user to input his email address.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The first bit of code only checks the first character in the string.

    The second code (in the function is_name()) checks a local uninitialised string. strlen(name) may be anything and the function will return true if it finds any alphabetic character, or false if it gets to the end of the (uninitialised) string without finding any alphabetic characters. Because it is playing with an uninitialised string (not the one read from the user) this function can technically run rampant.
    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.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    This is C++, so you really should look at using std::String here vice char arrays. What did you really expect to happen with your code snippet here:
    Code:
    if (isalpha(name[i])) { //<----is i ever incremented after you check the first char?
            i = 0;
            i++;
    
            cout << "Nice to meet you.\n";
        break;
        }
    Your logic needs to be like:
    Code:
    do
         Get input and save in std::string
         check each char in std::string and test if all are valid
    LOOP while name is invalid
    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.

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    26
    So how can I make my first program to check all the characters in the string ? Using the strlen() function ? But how?
    As for the second, how do I initialise my string?

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    You don't have to use the strlen function, the std::sting has a size() method. So.....
    Code:
    #include <iostream>
    #include <string>
    
    int main(){
    
         std::string Name; //<---This is a string
         // to check all characters of a std::string
         for(int i=0;i<Name.size();i++)
              Name[i]; //<----this iterates through all the char of a string
    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.

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    26
    @ AndrewHunter I'm sorry I asked you that stupid question about the strlen() function. When I posted yesterday I hadn't seen your reply . I don't know why but I hadn't seen it.
    Anyway , I solved the problem and my program works !
    Thank you very much!

  9. #9
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Sky_Daughter View Post
    stupid question
    Questions are never stupid. Unless you ask for full homework code and use Turbo C/C++ .
    Last edited by manasij7479; 08-16-2011 at 06:26 AM.

  10. #10
    Registered User
    Join Date
    Aug 2011
    Posts
    26
    I can assure you that I'm not asking for full homework. I want to learn the C++ program language. Anyway, my program was not a homework because I haven't studied the c++ at school yet

  11. #11
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Sky_Daughter View Post
    I can assure you that I'm not asking for full homework. I want to learn the C++ program language. Anyway, my program was not a homework because I haven't studied the c++ at school yet
    Nice catch !
    That was what I said.
    Anyway, congratulations for trying to stay ahead of the curve. I like to think I do the same and thus have a considerably better idea of programming and Computers in general than most of my age.
    My point is that, never hesitate to ask questions, however stupid may it seem. In the worst case scenario, you'd be redirected to some more research.
    Last edited by manasij7479; 08-16-2011 at 07:07 AM.

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Sky_Daughter View Post
    @ AndrewHunter I'm sorry I asked you that stupid question about the strlen() function. When I posted yesterday I hadn't seen your reply . I don't know why but I hadn't seen it.
    Anyway , I solved the problem and my program works !
    Thank you very much!
    No problem, if you are trying to learn you should post your completed code and ask for advice on approvements. Here is a quick little snippet I just put together that accomplishes the task at question, to show you a possibly different solution:
    Code:
    #include <iostream>
    #include <string>
    
    int main(){
    
    	std::string Name;
    	int i;
    
    	do{
    		std::cout<<"\nEnter a valid name: ";
    		std::cin >>Name;
    		for(i=0;i<Name.size();i++)
    			if(!isalpha(Name[i]))
    				break;
    	}while(i < Name.size());
    
    	std::cout<< "Hello " <<Name<<", nice to meet you.";
    
    	return (0);
    }
    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.

  13. #13
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    A little modification for reading full names and accepting spaces if you want to do that too:
    Code:
    #include <iostream>
    #include <string>
    
    int main(){
    
            std::string Name;
            int i;
    
            do{
                    std::cout<<"\nEnter a valid name: ";
                    std::getline(std::cin,Name);
                    for(i=0;i<Name.size();i++)
                            if(!(isalpha(Name[i])||Name[i]==' '))
                                    break;
            }while(i < Name.size());
    
            std::cout<< "Hello " <<Name<<", nice to meet you.";
    
            return (0);
    }

  14. #14
    Registered User
    Join Date
    Aug 2011
    Posts
    26
    Thank you again for your codes! This is what I wrote yesterday :
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    bool is_name(string);
    
    int main()
    { 
        string name;
        int i;
    
        do {
        cout << "Please enter your name.\n";
        cin >> name;
        
         if (is_name(name) == true) {
        
       
        cout << "Nice to meet you.\n";
        break;
        }
        
       
       cout << "No, that's wrong.\n";
       
        
        } while (is_name(name) == false);
       
        return 0;
    
    }
    
    
    bool is_name(string name)
    {
        int i;
    
        for (i = 0; i < name.length();i++ ){
            if ( !isalpha (name[i])) {
                return false;
            }
        }
        return true;
    }
    And how can I check the presence of the @ and the . in a string ? As I said above, this time I want the user to enter his email address.

  15. #15
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Sky_Daughter View Post
    And how can I check the presence of the @ and the . in a string ? As I said above, this time I want the user to enter his email address.
    You are going to want to look at: How do I seperate a string into tokens?-FAQ After you seperate the string into tokens, you would do your validation on each token.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 22
    Last Post: 07-28-2011, 01:26 PM
  2. Replies: 7
    Last Post: 06-16-2011, 06:21 PM
  3. Replies: 12
    Last Post: 03-07-2011, 01:24 AM
  4. Replies: 5
    Last Post: 05-09-2010, 10:58 AM
  5. Replies: 1
    Last Post: 10-31-2005, 11:36 AM