Exercise 1: Write a short program called readata.cpp that defines a character ar
ID: 3569654 • Letter: E
Question
Exercise 1: Write a short program called readata.cpp that defines a character array last which contains 10 characters. Prompt the user to enter their last name using no more than 9 characters. The program should then read the name into last and then output the name back to the screen with an appropriate message. Do not use the getline() or get functions!
Exercise 2: Re-write the program above using the getline() function (and only allowing 9 characters to be input). As before, use the character array last consisting of 10 elements.
Explanation / Answer
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char last[10];
cout<<"Enter your last name : ";
cin>>last;
if(strlen(last)>9)
cout<<"you entered name with more than 9 character. try again";
else
cout<<" You Entered the last name : "<<last;
}
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char last[10];
cout<<"Enter your last name : ";
cin.getline (last,11);
if(strlen(last)>9)
cout<<"you entered name with more than 9 character. try again";
else
cout<<" You Entered the last name : "<<last;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.