Last function I need help on. void read(char* destination, int maxChars) Read a
ID: 3541743 • Letter: L
Question
Last function I need help on.
void read(char* destination, int maxChars)
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
#include <iostream>
#include <string>
using namespace std;
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] = '';
}
int main()
{
char des[]="My name is Anindhya Sankhla";
read(des,12);
cout<<des<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.