Please be patient with me for this is my time posting and I am still in the primitive stages of programming.

I am using C++ VS. Thus far, the code I have written will prompt for a users first name, last name and telephone number(without dashes or parenthesis). The output begins with the users phone number, then display users full name then reverse the users name.

I have 2 problems: 1. My code will reverse the users full name (i.e Doe, Jane) but I need it to reverse those names (i.e eoD enaJ) and I am at a stand still because it's "just not coming to me" as far as what I need to do to make this happen. Can I use an array to do this?

2. I want to keep prompting the user for a phone number in the event they use less than 10 numbers for their phone number or if the accidentally type a letter. I tried to put an if/else statement but no luck

//Declared Variable
	string firstName = "";
	string lastName = "";
	string fullName = "";
	string phoneNum = "";
	char ans;

//Get information from user

do
{
cout<<"Enter your first name: ";
cin>>firstName;

cout<<"Enter your last name: ";
cin>>lastName;

cout<<"Enter 10 digit phone number(without dashes or parenthesis): ";
cin>>phoneNum;

cout<<" \n";
	
//manipulate data to be output

if(phoneNum.length()==10)
{
phoneNum.insert(0,"(");
phoneNum.insert(4,")");
phoneNum.insert(8,"-");
cout<<"Your phone number is: "<<phoneNum<<endl;
}

fullName = firstName + " "+lastName;
cout<<"Your full name is: "<<fullName<<endl;

//reverse users name

spaceLocation== fullName.find(" ",0);
firstName = fullName.substr(0, spaceLocation);
lastName = fullName.substr(spaceLocation + 1);


cout<<"Your reversed name is: "<<lastName<< ", "<<firstName<<endl;

cout<<" \n";

//Prompt user to to try again

cout<<"Would you like to try again? ";
cin>>ans;
ans=toupper(ans);
}  while(ans=='Y');

cout<<"THANK YOU \n"<<endl;

	
   system("pause");
	return 0;
}

Recommended Answers

All 2 Replies

I have a ton of book on programming that I am reading through now so hopefully I can find out how to do this. I will not give up on it until I make it preform the it is suppose to and I fully understand it.

There is a reverse() method in std <algorithm> header file

#include <string>
#include <algorithm>
#include <iostream>

using namespace std;

int main()
{
    string name;
    cout << "Enter your name\n";
    getline(cin,name);
    reverse(name.begin(),name.end());
    cout << name << '\n';
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.