Want to input a name with comma?

Hello everyone?

I want to know if someone can help me to understand how to input a name with comma and how to check if the user input the name with one comma or multiple( can I check it with ispunc function?/)

Thank you much for your help.

you can use the cin.getline() to get everything they type in, but im not sure how you would check for multiple commas
1
2
3
4
5
6
7
8
9
10
string name;
bool comma = false;
getline(cin,name);
for(int i=0; i<name.length(); i++)
{
     if(name[i] == ',')
       comma = true;
}
if(comma == true)
   cout<<"Name with comma";


please atleast try to code something yourself.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  
do{
        cout<<" What is your name??"<<endl;     
        gets(name);
        fflush(stdin);
        letters=strlen(name);
        valid = true;
            for (int i = 0; i < letters; i++){
                if (isdigit(name[i])){          
                    valid = false;
                    cout << "Sorry!! There should be no digit in the name ";
                    break;    
                }
            
            }
            
            count = 0;  
            for (int i = 0; i < letters; i++){
                if(name[i]==','){            
                    count++;
                }
            }    
            if (count == 0){     
                valid = false;
                cout<<" Please inter a name with a comma!!"<<endl;  
                }
                
            if (count > 1 ){
                valid = false; 
                cout<<"Name should conatain only one comma!!"<<endl;
                }
               
}while(!valid);





I've done this man!!

thanks for your help though!
Now I'm trying to get rid of the ' , ' where ever the user inputs it and return the name without any!!! I'm stuck in that part

Here is what I came up with, but doesn't work! :(

1
2
3
4
5
6
7
8
9
 for (int i=0; i < letters; i++){
                   if (name[i]==','){
                       name[i]='/0';
                   }                 
                   name[i]=tolower(name[i]);           
               }
               name[0]= toupper(name[0]);              
               cout<<" this is name"<<name;   
              



(forget about the rest of it) I tried to null it, but it will null everything!! :(

1
2
if (name[i]==','){
 name[i]='';

try this.

If it doesn't work, create another string, assign the first string to it and when a comma occurs use continue statement.
ghadf,
You'll run into problems with terminating strings like that. using c-style terminators, you'd lose the second half of the string if you didnt assign it to another variable.

why not use the string functions? here is an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
using namespace std;

int main()
{
    string name;
    string first;
    string last;
    int i=0;
    name = "Smith,John";

    i=name.find(',',0);
    if (i>0)  //name has a comma
    {
        last=name.substr(0,i);
        first=name.substr(i+1,name.length());
    }
    else
    {
        i = name.find(' ',0);
        first=name.substr(0,i);
        last=name.substr(i,name.length());
    }

    cout<<" this is name "<<name << endl;;
    cout<<  first << endl;
    cout<< last << endl;

    name = "Yao Ming";

    i=name.find(',',0);
    if (i>0)  //name has a comma
    {
        last=name.substr(0,i);
        first=name.substr(i+1,name.length());
    }
    else
    {
        i = name.find(' ',0);
        first=name.substr(0,i);
        last=name.substr(i,name.length());
    }

    cout<<" this is name "<<name << endl;;
    cout<<  first << endl;
    cout<< last << endl;
    return 0;

}

Topic archived. No new replies allowed.