Problem with Functions

Hey, as you can see I am a beginner at c++ and although I think I am improving I am still finding functions quite hard to get my head around. I am writing this program to test typing speed but am stuck at the beginning.

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
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <ctype.h>

using namespace std;

int players ()
{
	cout << "\nTyping Speed Test\n\n"
	<< "See who is a faster typer out of your friends!\n"
	<< "Would you like to start a one or two player game?\n"
	<< "1) One Player\n2) Two Player\n\n";
	
	int choice = 0;
	while (choice != 1 && choice != 2) 
	{
		cout << "Enter either 1 or 2: ";
		cin >> choice;
	}
	
	return (choice);
}

string text ()
{
	int choice = players ();
	string numberPlayers;
	if (choice == 1)
	{
		numberPlayers = "single player";
	}
	
	else 
	{
		numberPlayers = "two player";
	}
	
	cout << "\nEnter the text that you will be typing in this "
	<< numberPlayers << " game:\n\n";

}

int main() 
{
	text ();
	return (0);
}


This is the output of the program.


Typing Speed Test

See who is a faster typer out of your friends!
Would you like to start a one or two player game?
1) One Player
2) Two Player

Enter either 1 or 2: 1

Enter the text that you will be typing in this single player game:

[/s]Run Command: line 1: 7551 Segmentation fault: 11 ./"$2" "${@:3}"


I'm not too sure what the error is at the end and how to solve it. Any help would be much appreciated, thanks in advance!
Last edited on
you need a return(0); line in main and you need the other curly brackets, how did that even compile.
Hey, sorry for not making it clear. I seem to have located the problem to the two functions so I did not post the rest of the main function which has a return(0) and an other curly bracket. I will upload the rest now.
Last edited on
your text function says it is returning a string, but it is not. Try changing it to a void function.
Last edited on
Thats solved it! Thank you very much for your help :)
Topic archived. No new replies allowed.