functions giving me headache

I am supposed to do a main program with two function named tulTerv and tulHei. First one to ask name(nimi) and second to print 'nimi' and if it's AustinPowers return value has to be 0 otherwise -1. first function not suppose to return anything(so void?) I am using string(not really sure if it's necessary otherwise) because after completing this program I am trying to do exstra program to count which letter is used the most in nimi. I am new to programming
so little help or push to right direction would be nice, thank you.



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
#include <iostream>
#include <string>


using namespace std;

void tulTerv(string nimi)
{
	
string nimi;
	
cin >> nimi;

}
	
int tulHei()
{	
	string nimi;
	cout <<"heippa"<< nimi;
	{
	if (nimi== "Austin Powers")
	return 0;
	else 
	return -1;
	}
	

}


int main() {

cout <<"nimesi"<<"?"<<tulTerv(nimi);

tulHei();
cin.get();

}
Last edited on
If you put a closing [/code] at the end of your code, the code will format and line numbers will show up.

line 9 nimi; <- this is just the name of a string variable. It doesn't do anything by itself.

line 32 cout <<"nimesi"<<"?"<<tulTerv(string nimi); < - when you call a function, don't include the type (string), just the name of the parameter.


Edit: Also - nimi is not declared in your main function. and you're declaring a new variable nimi in your tulHei function.

Edit again: If the first function tulTerv has to be void because of the assignment, nimi could be sent by reference so that what they input there gets stored in the nimi variable back in main.

If they're going to be entering names with spaces in between, like "Austin Powers", look into using getline because cin will stop at the blank space and only read "Austin".

Last edited on
@wildblue I edited code a little. umm at the form it is now, will cin >> nimi; save it's input for the function tulHei to use?

thanks anyway .
Last edited on
Right now you're calling the tulTerv function from main with parameter nimi. Nimi hasn't been declared as a variable in main, so you should be getting a compiler error there.

Then in tulTerv, the function declaration has a local variable nimi - this is a copy of the value in nimi in the main function, but not the same variable. You then declare another local variable nimi in the body of the function. This is probably also going to give you a compiler error.

If you want the tulTerv function to return something, it can't be a void function. If you want to keep tulTerv void but update nimi back in main, you need to pass the variable by reference void tulTerv(string & nimi) <- the & is a reference operator.
Last edited on
@wildblue thanks! this helped me to get a sharper picture of the whole thing.
Topic archived. No new replies allowed.