10 Replies - 291 Views - Last Post: 06 December 2017 - 09:45 PM Rate Topic: -----

#1 davidg1996  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-November 17

Can someone help me figure this out please

Posted 05 December 2017 - 09:59 PM

I'm trying to print out the full name to print out in the format in my second declaration from my third declaration but it just prints out empty space what can i do to print it out any suggestion will be helpful thank you.
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
void wholeName(string firstName, string middleName, string lastName);
void output(string lastName, string firstName, string middleName);
void formatName(string lastName, string firstName, string middleName, string fullName);
int main()
{
	string firstName, lastName, middleName, fullName;
	wholeName(firstName, lastName, middleName);
	output(lastName, firstName, middleName);
	formatName(lastName, firstName, middleName, fullName);
	system("pause");
	return 0;
}
void wholeName(string firstName, string middleName, string lastName)
{
	cout << "Enter you first, middle, and last name please.\n";
	cin >> firstName >> middleName >> lastName;
}
void output(string lastName, string firstName, string middleName)
{
	cout << "Your name now reads in this format for your resume.\n";
	cout << "Your last name, than your first name, lastly your middle initial.\n";
	cout << lastName << " " << firstName << " " << middleName<< endl;
}
void formatName(string lastName, string firstName, string middleName, string fullName)
{
	cout << "This is how your name will read on your resume now.\n";
	fullName = lastName + " " + firstName +" " + middleName.substr(0,1) + ".\n";
	cout << fullName;
}


the include cstring was because i can get extra credit using char instead

This post has been edited by Skydiver: 05 December 2017 - 10:04 PM
Reason for edit:: Put code in code tags. Learn to do this yourself.


Is This A Good Question/Topic? 0
  • +

Replies To: Can someone help me figure this out please

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5955
  • View blog
  • Posts: 20,409
  • Joined: 05-May 12

Re: Can someone help me figure this out please

Posted 05 December 2017 - 10:09 PM

You don't really need to include <cstring> to be able to use C style strings.

Anyway, the main reason that you are not getting in output is because C++ is pass by value by default. You'll need to pass your strings by reference if you want your input function to fill in the string and have the values returned back to main() so that main() can pass along those string values to the other functions.

If you had use vanilla C strings, you would have not had this problem, but instead run into other problems of potential buffer overflows. In general, when writing C++ code, use C++ strings instead of C strings.
Was This Post Helpful? 1
  • +
  • -

#3 davidg1996  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-November 17

Re: Can someone help me figure this out please

Posted 06 December 2017 - 03:41 PM

Thank you, i was able to get it to work
Was This Post Helpful? 0
  • +
  • -

#4 davidg1996  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-November 17

Re: Can someone help me figure this out please

Posted 06 December 2017 - 04:23 PM

sorry last question im trying to make it to where i can input a different name with my void exit(char answer) definition but it won't let me input anything or go back to the beginning can anyone explain, please and thank you
#include<iostream>
#include<string>
using namespace std;
void wholeName(string& firstName, string& middleName, string& lastName);
void output(string& lastName, string& firstName, string& middleName);
void formatName(string& lastName, string& firstName, string& middleName, string& fullName);
void exit(char answer);
int main()
{
	string firstName, lastName, middleName, fullName;
	char answer = 'y';
	wholeName(firstName, lastName, middleName);
	output(lastName, firstName, middleName);
	formatName(lastName, firstName, middleName, fullName);
	exit(answer);
		system("pause");
		return 0;
	}
	void wholeName(string& firstName, string& middleName, string& lastName)
	{
		cout << "Enter you first, middle, and last name please.\n";
		cin >> firstName >> middleName >> lastName;
	}
	void output(string& lastName, string& firstName, string& middleName)
	{
		cout << "Your name now reads in this format for your resume.\n";
		cout << "Your last name, than your first name, lastly your middle name.\n";
		cout << lastName << " " << firstName << " " << middleName << endl;
	}
	void formatName(string& lastName, string& firstName, string& middleName, string& fullName)
	{
		cout << "This is how your name will read on your resume now.\n";
		fullName = lastName + " " + firstName + " " + middleName.substr(0, 1) + ".\n";
		cout << fullName;
	}

	void exit(char answer)
	{
		do {
			cout << "Do you want put a different name?\n";
			cout << "Press y for yes or n for no.\n";
			cin >> answer;
		} while (answer == 'y' || answer == 'Y');
		return;
		if (answer != 'y' || answer != 'Y') {
			cout << "End program.\n";
		}
	}


This post has been edited by Skydiver: 06 December 2017 - 04:27 PM
Reason for edit:: Put code in code tags. Please learn how to do this yourself.

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5955
  • View blog
  • Posts: 20,409
  • Joined: 05-May 12

Re: Can someone help me figure this out please

Posted 06 December 2017 - 04:32 PM

In the future, please post your code in code tags as it demonstrates in the watermark for the text field where you type in your post.

Anyway there are two problems: The first problem is that return on line 44 causes the computer to just return out of your exit() function. The second problem is that you are not doing anything to loop within your main() to repeat the process of asking for a name and printing them out various ways.
Was This Post Helpful? 1
  • +
  • -

#6 davidg1996  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-November 17

Re: Can someone help me figure this out please

Posted 06 December 2017 - 04:47 PM

ok will and do you think not having a declaration to exit or repeat loop and instead have it in the main function?
Was This Post Helpful? 0
  • +
  • -

#7 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 1410
  • View blog
  • Posts: 4,486
  • Joined: 20-March 10

Re: Can someone help me figure this out please

Posted 06 December 2017 - 06:16 PM

Use a do ..... while ...... loop dependent on char answer if answer == 'y' then loop.
Was This Post Helpful? 1
  • +
  • -

#8 davidg1996  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-November 17

Re: Can someone help me figure this out please

Posted 06 December 2017 - 07:02 PM

I'm using the do while loop but for some reason, it won't let me put another name in, it just keeps saying "do you want to put in another name unless I type n where it ends my program which is in an if statement
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5955
  • View blog
  • Posts: 20,409
  • Joined: 05-May 12

Re: Can someone help me figure this out please

Posted 06 December 2017 - 08:03 PM

Show your updated code. Is there a new do-while loop in your main()? Or are you still taking about the do-while loop in your exit() function?
Was This Post Helpful? 1
  • +
  • -

#10 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2019
  • View blog
  • Posts: 5,417
  • Joined: 27-December 05

Re: Can someone help me figure this out please

Posted 06 December 2017 - 08:04 PM

That's because the only thing in your do-while loop is
    cout << "Do you want put a different name?\n";
    cout << "Press y for yes or n for no.\n";
    cin >> answer;

so those 3 lines just keep running over and over until you enter something other than 'y' or 'Y' (and then control returns immediately to main).

Your do-while loop should ALSO contain the "working code" that you want repeated. And it shouldn't be in a separate function. Get rid of the exit function and put the do-while loop in main.

Something like this
int main()
{
    /// some stuff ...
    do
    {
         // some useful stuff ...
         // some useful stuff ...
         // etc ...

         // now that work is done.  Is there some more to do:

         cout << "Do you want put a different name?\n";
         cout << "Press y for yes or n for no.\n";
         cin >> answer;
    }  while (answer == 'y' || answer == 'Y');

    // maybe some more stuff ...
    return 0;
}

Was This Post Helpful? 1
  • +
  • -

#11 davidg1996  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-November 17

Re: Can someone help me figure this out please

Posted 06 December 2017 - 09:45 PM

Ok i was able to get my program to work thank you so much
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1