name arranger c++

here is my code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
string fullName;
cout << "Enter your full name (first middle last): ";
getline(cin, fullName);

if ( (fullName[0] == 'Q') && (fullName.length() == 1))
{
cout << "Program Quitting";
}


int space = fullName.find(" ");
int fullLength = fullName.length();


string firstName = fullName.substr(0, space);
int firstLength = firstName.length();


string second = fullName.substr(space + 1, fullLength - firstLength);
int secondLength = second.length();

int secondSpace = second.find(" ");


string middleName = second.substr(0, secondSpace);
int middleLength = middleName.length();


string lastName = second.substr(secondSpace + 1, secondLength - middleLength);

string finalName = lastName + ", " + firstName + " " + middleName.substr(0, 1) + ".";
cout << finalName<< endl;



system("pause");
return 0;

}


my teachers feedback:
you were to use string copy and concatenation commands and the name was to be stored in "an array of char data type". You did not use either of these techniques.

Also missing the pre-test while loop that was required.
The user should have had separate prompts for each part of the name
Your program has a decision structure testing for Q, but you have no prompt to tell user when to enter it.
Even if you tell it to quit, you displayed the Q as the full name.
First point I'm not an expert but I think when your teacher said the name was to be stored in "an array of char data type" I think he wanted you to use this:

char finalName[80]; //The 80 is a random number that I putted. You may increase it because some names are bigger than that.

Instead, you used this:

string finalName;

_________________________________________________________________________________

The Q being displayed as Full Name if you want to quit is a simple problem to resolve. Simply change the code to this:


1
2
3
4
5
if ( (fullName[0] == 'Q') && (fullName.length() == 1))
{
cout << "Program Quitting";
return 0;
}



PS: Still working on the other part :)

PSS: Please edit your first post and put the code inside code tags. Just select the code and click the <> symbol in the Format box on the left of the post.
Last edited on
I would like to ask you this..If you have a char array of 80 or greater how will the length of it ever be one without using a string of dynamic size( std::string )
char *c_string = new char[n];

So the teacher wants you to use C-strings... Did you ask him/her why you weren't allowed to use std::string?
OK. Than correct me if I'm wrong but I've been reading a little bit and I think I know how to fix it.

First when declaring a char array you have to do it like so:

1
2
3
...
char something[n] = "\0"; //this part I'm not certain because the post where I read so wasn't very self explainotory;
...


and because you can't use the .length function with a char array the code would be changed to so:

1
2
3
4
5
6
7
8
9
10
#include<cstring>
...
...
if( (fullName[0] == 'Q') && (strlen(nameOfTheArray) == 1))
{
cout << "Program Quitting";
return 0;
}
...
...


Note: If what did here was wrong please tell me what was it explain me how is the right way to do it cause I'm still pretty much a newbie in programming. I use what I know to try to help others
Last edited on
Topic archived. No new replies allowed.