Click here to Skip to main content
15,904,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to write a code that ask users to enter their email.
condition is that email should include "@" sign. so if he/she write their email address with no "@" sign the program will ask them to enter it again(loop)

What I have tried:

How I can do this using array and loop?(flag or while.. etc.)

in c + + language.
thank you
Posted
Updated 15-Nov-18 21:17pm
Comments
KarstenK 16-Nov-18 3:14am    
you should also test for some characters ("abc@abc.abc") and the "."

A loop? Pretty simple:
#include <iostream>

using namespace std;
bool ContainsChar(char c, const char *str)
{
    while (*str)
    {
        if (*str++ == c) return true;
    }
    return false;
}
int main()
{
    cout<<ContainsChar('@', "Hello World");
    cout<<ContainsChar('@', "Hello@World");

    return 0;
}
 
Share this answer
 
C++
#include <iostream>
#include <fstream>
using namespace std;

bool is_input_ok( const string & input )
{
  return (input.find('@') != string::npos);
}


int main()
{
  string email_address;
  do
  {
    cout << "please enter the email address:\n";
    cin >> email_address;
  } while ( ! is_input_ok( email_address) );
}
 
Share this answer
 
Comments
Member 14056553 16-Nov-18 3:11am    
thank you. but this in not working on my program. maybe c language?
CPallini 16-Nov-18 3:40am    
'Is not working' is a bit vague. Details?
Member 14056553 16-Nov-18 3:12am    
im using borland c++

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900