• 0

[C++] - name game


Question

hi all. i've written a program in C++ using the exact instruction my teacher has given me, and for some reason, it does not work. could someone tell me what might be wrong? i have the entire program basically finished, i think:

#include<iostream>
#include<string>
#include<ctime>
#include<stdlib.h>
using namespace std;

int WriteFirstLast(string &name)
{
	int first;
	int last;
	int length;
	int comma;
	length=name.length();
	comma=name.find(",");
	last=name.substr(0, comma-1);
	first=name.substr((comma+1), (last-1));
	return first, last, length, comma;
}
int main()
{
	srand((unsigned)time(0));
	int first;
	int last;
	int length;
	int comma;
	string name;
	cout<<"Enter name (last,first): "; cin>>name;

	first=WriteFirstLast(name);
	last=WriteFirstLast(name);

	cout<<"Name: "<<first<<" "<<last;

	system("pause");
	return(0);
}

Link to comment
Share on other sites

11 answers to this question

Recommended Posts

  • 0

Well for one thing you can't return multiple variables in a function like that. Also the substr function returns a string so you probably don't want to be assigning them to ints.

Link to comment
Share on other sites

  • 0
Well for one thing you can't return multiple variables in a function like that. Also the substr function returns a string so you probably don't want to be assigning them to ints.

thanks, that helps a bit. but as far as returning multiple variables, i remember something about putting the "&" in front of the variable name, so it would let you return multiple vars or something...or is that not right?

Link to comment
Share on other sites

  • 0

Rob hit the nail on the head. You can't return multiple values from the function like you're trying to do. What you could however do is return an array instead. You could return a string array with 2 elements, one for each name although I don't know whether you've covered that yet. Seems a bit complicated for what you're trying to do. Infact you could remove the function altogether unless it's been asked for by your teacher.

The & symbol gives you a pointer to the operand which is it's memory location.

edit: And I suppose the other solution, although it's not a great one, is to make first and last global variables. Then WriteFirstLast() function doesn't have to return anything.

Link to comment
Share on other sites

  • 0
Rob hit the nail on the head. You can't return multiple values from the function like you're trying to do. What you could however do is return an array instead. You could return a string array with 2 elements, one for each name although I don't know whether you've covered that yet. Seems a bit complicated for what you're trying to do. Infact you could remove the function altogether unless it's been asked for by your teacher.

The & symbol gives you a pointer to the operand which is it's memory location.

edit: And I suppose the other solution, although it's not a great one, is to make first and last global variables. Then WriteFirstLast() function doesn't have to return anything.

that's a good idea, and yeah i need the function. how exactly do i make them global variables?

Link to comment
Share on other sites

  • 0

Good thing you managed to do it, what approach did you take?

Usually the best ways to do this is returning a structure of passing the values as a reference and editing them.

Global variables is easy but is shunned on larger scale development because all of a sudden you have a lot of global variables that are specific to given functions and there's also the issue of their free access.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.