Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In c++ Prompt the user for his/her name. Verify that the name only contains alph

ID: 3575639 • Letter: I

Question

In c++ Prompt the user for his/her name. Verify that the name only contains alphabetic characters.

My code:

#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 < 1 ; 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;
}

return 0;
}

Required output and my output

Required output What's your first name n Your name cannot contain An Your name cannot contain In Your name cannot contain An Your name cannot contain An Your name cannot contain An Your name cannot contain: An Your name cannot contain An Invalid name. In Standard input (I don't know) Required output What's your first name ?An Your name cannot contain: (In Your name cannot contain Your name cannot contain An Your name cannot contain An Your name cannot contain In Invalid name. In Your Program's output What's your first name An Your name cannot contain An Invalid name. Test Case 8 Failed Your Program's output What's your first name? Mn Your name cannot contain (In Invalid name n

Explanation / Answer

Solution for Problem :--

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
char name[100];
int i,l,flag=0;
scanf("%s",&name);
l=strlen(name);
for(i=0;i<l;i++)
{
if(name[i]>= 'a' && name[i] <= 'z' || name[i] >= 'A' && name[i] <= 'Z')
{
continue;
}
else
{
cout << "Your name cannot contain: " << name[i] << endl;
cout << "Invalid name." << endl;
flag=1;
break;
}
}
if(flag!=1)
cout << "Valid name." << endl;
return 0;
}

Output 1:-

jack1
Your name cannot contain: 1
Invalid name.

Output 2:-

jesus
Valid name.

Note :- To read input this code use %s to read string.It reads a sttring until it takes space or enter. If your name contain any spaces replace the scanf("%s",name); with the gets(name); or getline(name);.

Thankyou!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote