Functions in a seperate file

Hey guys I am fairly new to programming( I know some python) but I want to get serious about it.

I need some help in two areas in this question

The first is when creating a function
i.e.

string newfunc(int integer)

Can I tell the function to pass any integer value or must the integer I pass it always be called integer(in this case). It seems like it would be much more useful to have the function saved and be able to pass it any variable name as long as it is an integer.

Also if i return something like

return result;

will i only ever be able to access it by the variable name result.


I am here trying to create a function that converts integers to strings and save it to be used later.

I here show the main file that will use the function, then the cpp for the function and the header file.

The program comes back with an error for an undeclared identifier
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
50
51
52
53
54
55
56
57
58
#include <string>
#include <sstream>
#include "inttostr.h"

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	int x = 999;
	int y = 999;
	string result;

	while (x > 99)
	{
		while(y > 99)
		{
		int product = (x*y);
        inttostr(product);
		
		if (result[0] == result[5] && result[1] == result[4] && result[2] == result[3])
		{
			cout<<"The largest palindrome of 2 3-digit numbers is "<<result<<endl;
		}
		else
		{	
			y--;
		}
		}//end while
		x--;
	
	}//end while
	
	system("Pause");
	return 0;
}

//inttostr.cpp Convert integers to strings
#include<string>
#include<sstream>
#include "inttostr.h"

using namespace std;

string inttostr(int inttoconvert)
{
		string result;
		ostringstream convert;
		convert << inttoconvert;
		result = convert.str();
		return result;
}

#ifndef INTTOSTR_H_INCLUDED
#define INTTOSTR_H_INCLUDED
string inttostr(int inttoconvert);{
	return result;
};
#endif // INTTOSTR_INCLUDED 


Am I just way off here?
Variable names only matter within their scope. You may have a function with some parameter name and some returned variable with some specific name, but you do not necessarily have to use those names everywhere.

The error comes from you header file (lines 53 to 58).
1
2
3
4
5
6
7
#ifndef INTTOSTR_H_INCLUDED
#define INTTOSTR_H_INCLUDED
string inttostr(int inttoconvert); //semi-colon makes this a prototype
{              // Now rest of block is missing an identifier
	return result; //No result variable declared; invalid
};     //You don't need a semi-colon here
#endif // INTTOSTR_INCLUDED 


Another mistake you made is defining two functions with the exact same identifier but separate definitions.
A function prototype follows this format:
return_type function_name(parameter_types_and/or_names);

So, for example you should have in inttostr.h:
1
2
3
4
5
6
7
8
#ifndef INTTOSTR_H_INCLUDED
#define INTTOSTR_H_INCLUDED

#include <string> //For string class in function prototype
using std::string;

string inttostr(int); //Note that the name in the prototype is optional
#endif 

Names do not cross function boundaries.
http://en.wikipedia.org/wiki/Parameter_%28computer_programming%29#Parameters_and_arguments

string inttostr(int inttoconvert);{
return result;
};
while(y > 99)
{
int product = (x*y);
inttostr(product);
result=inttostr(product);
Only names defined at global scope are accessible across functions. For example,
1
2
3
4
5
6
7
8
9
10
11
void g(){
    foo=42; //this does not compile
}

int f(){
    int foo;
    return foo; //this compiles but is incorrect
}

g();
f(); //if the program somehow compiled, this call would still not return 42 


Also, your algorithm is incorrect. See:
http://www.cplusplus.com/forum/general/76890/ (someone made the same algorithmic error you made)
http://www.cplusplus.com/forum/beginner/75152/ (an optimal solution is explored)
Last edited on
i'm not gonna try to answer this, because i can see from the question that you didn't try anything to solve your problem.
no offence, but i really suggest you try looking for an answer before posting here.

you should try your expertise, when it fails you can return to the source you learned programming from, and when that fails, try searching google for a solution, when that fails look for a solution between the articles of this site.

only when you fail in all these, i'd be happy to answer anything i'm capable of.

really no offence.
I've never seen a book directly state that I can use any identifier I want to pass through and return. Then store the return in a new identifier.

Maybe I should be smart enough to get the inference, but hey I am just a guy sitting at a computer trying to write some C++ so I can get a ground understanding on a subject I know very little about and have never taken a formal class in.

Thanks everyone else for clearing this up.
Hey, that's a Euler problem right?

How many have you solved so far?
Last edited on
@Deaven
Maybe I should be smart enough to get the inference, but hey I am
just a guy sitting at a computer trying to write some C++ so I can get a
ground understanding on a subject I know very little about and have never
taken a formal class in.

i don't think you really know what i'm talking about, right?
from the question you asked, i can clearly see that you didn't try to read almost anything about this language.
you don't know what a scope is !!!
scope is one of the strong features of C++, and it's really not that complicated, it can be explained in just a few pages -like 5 pages max-, and in my opinion, you don't have to be a genius to learn it, you just have to work a bit.
if you really don't know how to start with this language:
C++ The Complete Reference 4th Ed

http://cfile3.uf.tistory.com/attach/157796424D7B86980ED5F3

this is the book i started with, it's old, almost ANCIENT, but it contains most of the standard C++ 1998.
i've seen lots of guys here advice reading
C++ primer 5th edition
any should be a good start before you start writing any programs.

please don't take it personal, i just want people to read a reference before they start with the language.
Last edited on
Ivor Horton's Visual C++ for Beginners will get you through all the basics in pretty good detail, and it even gets into Windows MFC in the last couple of chapters.
Topic archived. No new replies allowed.