Enhanced Magic Number

Hello,

I have this Problem

Enhance the Magic Number program by telling us if we missed the number the following: Very Near, Near, Far, Very Far.


What I did is making ranges > or < 20 then it's very near..

and i come up with this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <conio.h>

using namespace std;

void main()
{
	int x = 20, g;
	cout << "Please enter your number:";
	cout <<"\n\n";
	cin >> g;
	cout <<"\n\n";
	if ( (g>x+20) || (g<x-20)) cout << "Very near";
	else if ( (g>x+40) || (g<x-40) ) cout << "Near";
	else if ( (g>x+60) || (g<x-60) ) cout <<"Far";
	else if (g=x) cout <<"Bravo!!!!";
	else cout <<"very far";
	getch();
}


but it's not working for me :( is there anything I could fix ? Please help me and I';ll try to edit the code.

Thank you :D
That won't work. Check if it's very near, near or far by using an alternative solution like
if (g < x + 20 && g > x - 20) //if its between x + 20 and x - 20 .
You aren't checking if it fulfills one - you need both requirements to be fulfilled to be "very near"!

else if (g = x)
Equality. Symbol.

void main()
main() must return an int.

getch();
Useless. Old.
so you don't have to guess the same number every time change x to random.
and use a while loop so user can keep guessing.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    // use time so numbers are random
    srand(time(NULL));

    // assign x to a random number out of 100
    x = getRand();
    ...
        ...
    ...
    return 0;
}

int getRand()
{
    return 1+rand() % 100;
}
Last edited on
Thanks Bluzor - our teacher is teaching us void main() - and using getch(); to keep the output showing so we can use it later.

@ gcampton : we didn't learn the loop lesson yet.

I'll try again :)
Instead of getch(), you can use cin.get();
I made this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <conio.h>

using namespace std;

void main()
{
	int x =10, g;
	cout <<"Please enter your number:";
	cout <<"\n\n";
	cin >> g;
	cout <<"\n\n";
	if (g < x+20 && g > x - 20) cout <<"Very near";
	else if ( g < x + 40 && g > x - 40) cout <<"Near";
	else if (g < x + 60 && g > x - 60) cout <<"Far";
	else if (g==10) cout << "Bravo!!!!";
	else cout << "Very Far";

	getch();
}


It's working Pretty well but when i type "10" it gives me "Very Near" :( it must give me "Bravo!!!"

Thanks.
our teacher is teaching us void main() - and using getch(); to keep the output showing so we can use it later.

WHAT??? Aren't teachers meant to be competent at the subject they teach?
main() must return int. getch() is from an old DOS header (conio.h). It's non-standard, importable, and... old.
Last edited on
It isn't the first time I hear that. There must be some ignorant teachers...
I hope this guy gets his money back; that's absolutely ridiculous...
LOL

so what I must use in the future :) ?
Use int main() and cin.get() instead.
WHAT??? Aren't teachers meant to be competent at the subject they teach?


Perhaps in a Utopian world they are. In the real world, there are a great many incompetent people in all professions. Perhaps even moreso with teaching. After all, there is some amount of truth in "Those who can't do, teach.".
Perhaps in a Utopian world they are. In the real world, there are a great many incompetent people in all professions. Perhaps even moreso with teaching. After all, there is some amount of truth in "Those who can't do, teach.".

Hmmm....
Topic archived. No new replies allowed.