Basic functions program.need help

I am a bit confused on functions and am not sure if the program was set up right. Also If it set up right can someone tell me what is wrong with it?





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
59
#include <iostream>
#include <ctime> // for time functions
#include <cstdlib> //for rand and srand functions
using namespace std;

void random();
void welcome();
void guessing();
void check();

int random(int number,int guess)
{
	int Number = 0;
    int guess = 0;

	srand(time(0));
    number = rand() % 99 + 1;
    cout << number << endl << endl;
}

welcome()
{
	cout << "welcome to the guessing game" << endl;
}
int guessing(int guess) 
{
	cout << "I thinking of a number between 1 and 100. Guess the number";
	cin >> guess:
}
int check(int guess, int number)
{
	while (guess != secretNumber){
	
	if(!(cin >> guess)){
            cout << "Sorry, must be a number" << endl;
            cin.clear();
            cin.ignore(256, '\n');
            continue;
        }
	if(guess < 0 || guess > 100) {
            cout << "Sorry, guess between 1 and 100" << endl;
            cin.clear();
            cin.ignore(256, '\n');
            continue;
    }

	 if (guess > secretNumber) {
            cout << "Too high!\n\n";
        }else if (guess < secretNumber) {
            cout << "Too low!\n\n";
        }else {
            cout << "Congratulations, you got it!\n\n";
        }
	}
}
main() 
{
	cout << "good game!!!" << endl;
}
Since the code won't compile that must be an indication that there is something wrong. Please post your complete compiler error messages, exactly as they appear in your development environment.

welcome has no return type but isn't declared as void. So add void to line 21 before welcome and that'll be fine. Also main always returns 0, so that should be int before main.
At the end of line 28 you are using a full colon to end a statement, that should be a semicolon. Lastly, program execution always starts in main yet the only thing main will do is cout "good game!". If you want your program to do something else you have to call the functions from within main.

That should clear some of the issues you are having up. Try fixing that then if you still have problems post your new code then post any errors and output using the output tags too. :)
here is what i fixed.


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
59
60
61
62
63
64
#include <iostream>
#include <ctime> // for time functions
#include <cstdlib> //for rand and srand functions
using namespace std;

void random();
void welcome();
void guessing();
void check();

int main;
{
	int Number = 0;
    int guess = 0;

	srand(time(0));
    number = rand() % 99 + 1;
    cout << number << endl << endl;
}

void welcome()
{
	cout << "welcome to the guessing game" << endl;
}
int guessing(int guess) 
{
	cout << "I thinking of a number between 1 and 100. Guess the number";
	cin >> guess;
}
int check(int guess, int secretNumber)
{
	while (guess != secretNumber){
	
	if(!(cin >> guess)){
            cout << "Sorry, must be a number" << endl;
            cin.clear();
            cin.ignore(256, '\n');
            continue;
        }
	if(guess < 0 || guess > 100) {
            cout << "Sorry, guess between 1 and 100" << endl;
            cin.clear();
            cin.ignore(256, '\n');
            continue;
    }

	 if (guess > secretNumber) {
            cout << "Too high!\n\n";
        }else if (guess < secretNumber) {
            cout << "Too low!\n\n";
        }else {
            cout << "Congratulations, you got it!\n\n";
        }
	}
}
void random() 
{
	cout << "good game!!!" << endl;

    system("pause");
	return 0;


}


1>------ Build started: Project: secretnumber, Configuration: Debug Win32 ------
1>Compiling...
1>asd.cpp
1>c:\users\baret academic\documents\visual studio 2008\projects\secretnumber\secretnumber\asd.cpp(9) : error C2144: syntax error : 'int' should be preceded by ';'
1>c:\users\baret academic\documents\visual studio 2008\projects\secretnumber\secretnumber\asd.cpp(16) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>dfsg.cpp
1>c:\users\baret academic\documents\visual studio 2008\projects\secretnumber\secretnumber\dfsg.cpp(12) : error C2447: '{' : missing function header (old-style formal list?)
1>c:\users\baret academic\documents\visual studio 2008\projects\secretnumber\secretnumber\dfsg.cpp(61) : error C2562: 'random' : 'void' function returning a value
1> c:\users\baret academic\documents\visual studio 2008\projects\secretnumber\secretnumber\dfsg.cpp(6) : see declaration of 'random'
1>Generating Code...
1>Build log was saved at "file://c:\Users\Baret Academic\Documents\Visual Studio 2008\Projects\secretnumber\secretnumber\Debug\BuildLog.htm"
1>secretnumber - 3 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
had an older item in the same project so here is the real output

1>------ Build started: Project: secretnumber, Configuration: Debug Win32 ------
1>Compiling...
1>dfsg.cpp
1>c:\users\baret academic\documents\visual studio 2008\projects\secretnumber\secretnumber\dfsg.cpp(12) : error C2447: '{' : missing function header (old-style formal list?)
1>c:\users\baret academic\documents\visual studio 2008\projects\secretnumber\secretnumber\dfsg.cpp(61) : error C2562: 'random' : 'void' function returning a value
1> c:\users\baret academic\documents\visual studio 2008\projects\secretnumber\secretnumber\dfsg.cpp(6) : see declaration of 'random'
1>Build log was saved at "file://c:\Users\Baret Academic\Documents\Visual Studio 2008\Projects\secretnumber\secretnumber\Debug\BuildLog.htm"
1>secretnumber - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
You need to really look at your error messages and recall what Raezzor said. if you are declaring a return type then you need to return that type back. to fix the "random" function I would suggest the same thing you did for the "welcome" function, take a look at your error messages first:

1
2
 'random' : 'void' function returning a value
1> c:\users\baret academic\documents\visual studio 2008\projects\secretnumber\secretnumber\dfsg.cpp(6) : see declaration of 'random'


you should also change your line 11, main is not an integer declaration. You had it right the first time. int main().

and fix line 14 int Number = 0;. remember that variable names are case sensitive.

The good news is, you're almost done with the error messages, if you get past these fixes I think you'll be running the program.
I hope this helps

Last edited on
My advise, stop trying to write the whole program and write one section at a time. When something breaks, it's easier to figure out based on what you added last.

The first thing to do is write something that will compile.
add something, test it
add something else, test it

Here is some working code for you to start, with examples of functions and global variable.

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
#include <iostream>
#include <ctime> // for time functions
#include <cstdlib> //for rand and srand functions
using namespace std;
int guess=0;

void welcome()
{
	cout << "welcome to the guessing game" << endl;
}

int guessing() 
{
	cout << "I thinking of a number between 1 and 100. Guess the number: ";
	cin >> guess;
	if ((guess>0)&&(guess<=100))
	{
    cout << "Good number lets see..." << endl;
    }
    else
    {
	cout << "Between 1 and 100. Try again: ";
	cin >> guess;
    }
}

main() 
{
welcome();
guessing();
cout << "your guess was  " << guess << endl;
cout << "good game!!!" << endl;
return 0;	
}
Last edited on
On top of what everyone else said, if you want your number to be between 1 and 100, your number limiting statement should be

 
number = rand() % 100 +1;


The reason is that you can't get a remainder of the number you're dividing by.

if rand() = 199
199 % 100 = 99 + 1 = 100

1
2
int number, max = 100, min = 1;
number = rand() % ((max+1)-min) +min;


That will ensure you stay in the limits you want.
here is what i have got so far
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
59
60
61
62
63
64
65
#include <iostream>
#include <ctime> // for time functions
#include <cstdlib> //for rand and srand functions
using namespace std;

void random();
void welcome();
void guessing();
void check();

int main()
{
	int secretNumber = 0;
    int guess = 0;
	

	int number, max = 100, min = 1;
    number = rand() % ((max+1)-min) +min;
  
}

void welcome()
{
	cout << "welcome to the guessing game" << endl;
}
int guessing(int guess) 
{
	cout << "I thinking of a number between 1 and 100. Guess the number";
	cin >> guess;
}
int check(int guess, int secretNumber)
{
	while (guess != secretNumber){
	
	if(!(cin >> guess)){
            cout << "Sorry, must be a number" << endl;
            cin.clear();
            cin.ignore(256, '\n');
            continue;
        }
	if(guess < 0 || guess > 100) {
            cout << "Sorry, guess between 1 and 100" << endl;
            cin.clear();
            cin.ignore(256, '\n');
            continue;
    }

	 if (guess > secretNumber) {
            cout << "Too high!\n\n";
        }else if (guess < secretNumber) {
            cout << "Too low!\n\n";
        }else {
            cout << "Congratulations, you got it!\n\n";
        }
	}
}
void random() 
{
	cout << "good game!!!" << endl;

    system("pause");
	return 0;


}


now its saying this
" error C2562: 'random' : 'void' function returning a value
'see declaration of 'random'
can someone explain this to me?
can someone help please???
someone help please!!!
It's pretty clear from the error message what is wrong. And I can already tell you that program is not going to do anything near what you want. I suggest reading up on basic functions, prototyping, declaration, usage, etc. Because even after we've tried to help a few times in this thread there's still many of the same errors there. In fact, the only function that matches it's return type with what is written in the block of the function is welcome. Every other function there, including main, is wrong (with regard to what it's declared to return, and what it actually returns...)
Last edited on
This will help you out

http://www.cplusplus.com/doc/tutorial/functions/

Read carefully, if you declare a function of type 'int' it must return 'int'. If your function is of type 'void' you can't return anything... You can't even use 'return'.
majorursa, sorry for all the confusion you must be having.

here are some hints:
when you have a function that doesn't need to return anything to the caller, you use void in the function declaration. as we see here:
1
2
3
4
void welcome()  //<<--- see the void? that means you return nothing.
{
    cout << "welcome to the guessing game" << endl;
}


when the function does return an integer, you put that in the function declaration and add a return call in the function. as in this example:
1
2
3
4
5
6
int guessing(int guess)   //<------this returns an int
{
    cout << "I thinking of a number between 1 and 100. Guess the number";
    cin >> guess;
    return guess;  ///<------you put int on the function line, so return the guess variable
}


one last piece of advise, the main function must always be like this:
1
2
3
4
5
int main()   //<-----see the int here? that means return an int.
{
 /* your code in here */
return 0;   //<-----main needs to return an integer.
}


Ok, now thats out of the way, onto the other points. your code is scattered all over the place. you need to think about where you are getting inputs and where you are prompting the user for a number. you have it scattered in the check function and the guessing function. i think you're trying to do too many things. you need ti make it simple.

i would to this in my main function instead of using your check function. just to try things out if this works:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    welcome();
    guess = 0;

    while (guess != number)
    {
        guess=guessing();

        if (guess > number)
        {
            cout << "Too high!\n\n";
        }
        else if (guess < number)
        {
            cout << "Too low!\n\n";
        }
        else
        {
            cout << "Congratulations, you got it!\n\n";
            random();
        }

    }


AND one more note: you have secretNumber and number variables. these should be the same. make it secretNumber.

I hope this helps,
Last edited on
Topic archived. No new replies allowed.