Need the following function to read a users name into the destination Function:
ID: 3541740 • Letter: N
Question
Need the following function to read a users name into the destination
Function:
void read(char* destination, int maxChars)
Purpose:
Read a string of characters entered by the user at the keyboard with a length no longer than maxChars (including the null character at the end of the C string) into an address specified by destination The user should be able to type a space or tab as part of the string.
The getline() method of the istream class in the header file <iostream> can be used to read a string that contains spaces or tabs into an array of char.
Explanation / Answer
void read(char* destination, int maxChars)
{
int i = 0;
char temp[10000];
cin.getline(temp,1000);
while(i<maxChars)
{
destination[i] = temp[i];
i++;
}
destination[i] = '';
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.