Called Function anomaly

Hey Chaps,

I'm trying to get back into C++ and am finally adjusting to non-object-orientated code again... but I'm having an issue I can't find an answer too online or through comparison with similar code.

My goal is a simple branching decision and name declaration (which I'm aware can be made more modular to better suit being re-used for other values - I'll work on that whilst I await a response).

The problem: When I compile the script, what I get responds correctly though I have to respond twice to "What is your name?" before it will continue on to the next line. Anybody able to highlight what I've overlooked?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>

std::string nameDeclare() /// ===** TARGET FUNCTION I CALL LATER IN SCRIPT **== 
{
	std::string namechange("");

	std::cout << "What is your name?" << std::endl;
	std::cin >> namechange;

	return namechange;
}

int main()
{
	std::string playername("");
	std::string x("");
	
	std::cout << "Initialising..." << std::endl;
	
	nameDeclare(); /// ===** SCRIPT SEEMS TO REPEAT THIS LINE ....or..... THE FUNCTION ABOVE "nameDeclare()"

	std::cout << ("You said..." + nameDeclare()) << std::endl;
	std::cout << "Is that right?"; 
	std::cin >> x;
	
	if (x == ("No","no"))
	{
		nameDeclare();
	}
	else
	{
		std::cout << "Excellent!";
	}
		return 0;
}
/* For some reason the script repeats the question of "what is your name?" consistently saving the response string 2nd time around and repeating it back correctly.*/ 
Last edited on
1
2
3
	nameDeclare(); /// ===** SCRIPT SEEMS TO REPEAT THIS LINE ....or..... THE FUNCTION ABOVE "nameDeclare()"

	std::cout << ("You said..." + nameDeclare()) << std::endl;
notice how you call the function a second time?


Also, you have a variable for the payers name, why not assign the returned value from nameDeclare to it?

e.g. change line 21 to playername = nameDeclare(); and then line 23 to std::cout << "You said..." << playername << std::endl;

Also if (x == ("No","no")) does not do what you want it to. You should do if(x == "no" || x == "No") though we run into the issue of just calling the function but not doing anything else.


I would suggest doing a loop instead of the if statement.
1
2
3
4
5
do
{
    //get the players name..
    //ask if the name is correct ..
} while( x == "No" || x == "no");

I structured the decision like you suggested originally but for some reason it wasn't working so I assumed the syntax wasn't right and the way posted worked -shrugs-.

Fantastic! I'm just trying to get my head around return values and knew my method was incredibly bloated, I understood what you've done - much appreciated!!

I originally suspected my cout with + nameDeclare might be causing it to run a 2nd time though because it was printing the string correctly 2nd time around I dismissed it. I'll correct it to your suggested layout and better lay out the called function itself.

Thanks Giblit.
Topic archived. No new replies allowed.