Can someone please help

I am working on a program where i prompt the user for his/her name and i have to verify that the name only contains alphabetic characters.

here is my coding. It works to an extent i just can't get it to work the way it is supposed to.

#include <iostream>
#include <cstring>
using namespace std;

const int SIZE = 50;

int main()
{

char name[SIZE];

cout << "What's your first name?" << endl;
cin.getline(name, SIZE);

for (int x = 0; x < strlen(name); x++)
{
if (name[x] >= 65 && name[x] <= 90 || name[x] >=97 && name[x] <=122)
{
cout << "Valid name." << endl;
}
else
{
cout << "Your name cannot contain: " << name[x] << endl;
cout << "Invalid name." << endl;
}
}

when someone types in a name with only alphabetic characters it outputs valid name for how long the name is. For Ex: bob is three characters so it would output valid name three times. also when you put in a name with a number or character at the end it outputs valid name for the correct characters but also outputs the invalid character and invalid name which its supposed to do. the valid name should not be showing up though.

closed account (Dy7SLyTq)
http://ideone.com/XTQtlw
You are outputting "Valid name" each iteration of the loop. Instead try making a boolean variable and init it to true, then in the for loop, if the condition ever fails (not a letter) you set it to false and break. Now you can use your boolean in an if statement and another for loop (i=0; i < strlen(name); ++i) to output the name x number of times :)
Last edited on
Topic archived. No new replies allowed.