If I could be both told the meaning of the #includes, as well as what is wrong w
ID: 3546141 • Letter: I
Question
If I could be both told the meaning of the #includes, as well as what is wrong with my code, I'd be most appreciative. It simply won't return anything, I'm unsure of my mistake.
The point of the program is to use ONLY C-Strings, and ask for First, Middle, and Last names of the given user. After this, to output it in First, Last M. (Middle initial) format. It is also essential that, if no middle name is given, that it outputs a response with first, last.
Thank you for your time, and assistance.
#include <cstdlib>
#include <iostream>
#include <cctype>
using namespace std;
int main( )
{
char firstN[21], middleN[21], lastN[21], names[61]; //Holders of name strings C-String
int i = 0, j = 0; //Holders for important places
int no_last_name = 0; //To check for last name.
cout << "Homework Ten: Solution to Project 4 (Using C-Strings) "
<< "Please enter your full name: ";
cin.getline(names, 61);
while( names[i] != ' ')
{
firstN[i] = names[i];
i++;
j++;
return j;
}
while(names[j] != ' ' &&names[j] != '' &&names[j] != '.')
{
middleN[i] = names[j];
i++;
j++;
return j;
}
while(names[j]!= '' &&names[j] != '.')
{
lastN[i] = names[j];
i++;
j++;
if(lastN[i] <= 0)
{
no_last_name = 1;
return no_last_name;
}
}
if(no_last_name = 1)
{
cout << "Formatted name: " << middleN << ", " << firstN;
}
else
{
cout << "Formatted name: " << lastN << ", " << firstN << " "
<< middleN << '.';
}
system("pause");
return 0;
}
Explanation / Answer
you have to initiate the variable "i" to 0 ,before each loop..that is the reason it is not providing you with any output due to index being out of bound.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.