C++ Console Homework Project

Howdy.
My C++ teacher asked me to write this console project two weeks ago, I'm still trying but I couldn't write the program and I have just one week left. So I need your help with this, any help will be appreciated.

http://s11.postimg.org/ia1b59cvn/Ads_z.png

As you can see at the image above, my teacher asks for a digit extracting program. Sounds easy, right? Yeah, he asked for the same assignment before and I wrote that program. But this time he has strange conditions for the program so I need more experienced coders's help.

My teacher said:
-It needs to work up to 19 digits.
-You need to use 10 semicolons maximum.
-You need to use 3 variables maximum.
-You need to use these ONLY: iostream.h, conio.h, if, getch.
-You MUST NOT use these: for, while, do-while, goto, switch, until, repeat-until, try-catch etc.
Or I'll get an F. Ain't that a bitch?


I've made a digit extraction homework just like the image above only with "if" command. I have used 14 semicolons in total and it supported up to 5 digits. My teacher said he wants 10 semicolons max and 19 digit support or I'll get an F. I tried a lot but couldn't make an application with less semicolons.

Some of you will say this is not the right way to teach C++, and If I want to learn it I need to write this myself. Yeah, you are right and this is not the best way to teach C++. I am aware of it and I want to learn java actually but this is my exam project and If I don't complete it, I will get an F and I won't get any java courses. So I need your help. I know this program is very stupid but I have to finish it.

Notes:
-My teacher said that he used if within an if and wrote it with 7 semicolons in total.
-my classmate said that he found a command and wrote this program with just one variable and 7 semicolons but he doesn't tell me what it is, lol. Screw him.

I hope you guys can help me with that. I'm looking forward to it. Thanks!
Last edited on
This is not a homework site. We won't do your homework for you. However we are always willing to help solve problems you encountered, correct mistakes you made in your code and answer your questions.

We didn't see your attempts to solve this problem yourself and so we cannot correct mistakes you didn't made and answer questions you didn't ask. To get help you should do something yourself and get real problems with something. If your problem is "I don't understand a thing", then you should go back to basics and study again. As it is impossible to find derivative of function without knowledge in arithmetic, you cannot do more complex tasks in programming without clear understanding of basics
Hints:

This is a homework involving recursion.

You are not asked to input a number; you are asked to input using getch() -- a character. Hence, you are already a step ahead when trying to split the number.

Hope this helps.
I gave you a base that you could fix, completely without conio.h.
He only said your allowed to use conio.h not that you must use it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
int8_t counter = 0, secondary_counter = 0, num[19];
void display() {
	if (secondary_counter < counter) {
		std::cout << num[secondary_counter++] << "*" << pow(10, counter - secondary_counter - 1) << " + ";
		display();
	}
}
void get_input() {
	std::cin >> num[counter++];
	std::cout << num[counter - 1];
	if (std::cin.peek() != 10)
		get_input();
}
int main() {
	{get_input(), std::cout << ' ' << '=' << ' ', display(), system("pause");}
	//He did say no semicolons only :)
}

If you need explanations on some parts of the code ask me.
Last edited on
@AbstractionAnon

Here's the ones I tried:
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
#include <iostream>

size_t digits(size_t num, size_t pow = 1000000000, char sep = '=')
{
	if (!pow)
		return 0;

	if (pow > num)
		return digits(num, pow / 10, sep);

	if (num / pow) {
		std::cout << " " << sep << " " << num / pow << "*" << pow;
		sep = '+';
	}

	digits(num % pow, pow / 10, sep);
}

int main()
{
	size_t num;

	std::cout << "Input number: ";
	if (!(std::cin >> num))
		return !!(std::cout << "Bad number" << std::endl);

	std::cout << num;
	return digits(num);
}


But it gives strange result when I input a number below 0.

@rabster:
Thanks a lot! But it gives an error:
In function 'void display()': 5:90: error: 'pow' was not declared in this scope
Ah okay here, fix this :P 6 semicolons so far and no conio.h.
Also got it down to 2 variables.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cmath>
int8_t counter = 0, num[19];
void zeros(int val) {
    if (num[val]) {
        std::cout << "0", zeros(val + 1);
    }
}
void display() {
	if (num[counter]) {
		std::cout << num[counter++] << "*1", zeros(counter), std::cout << " + ", display();
	}
}
void get_input() {
	std::cin >> num[counter++],	std::cout << num[counter - 1];
	if (std::cin.peek() != 10)
		get_input();
}
int main() {
	{get_input(), std::cout << ' ' << '=' << ' ', counter = 0, display(), system("pause");}
	//He did say no semicolons only :)
}


Oops remove the cmath.
Last edited on
He wants it dirty? Here it is...


use implicit if ('?' - ':') to avoid semicolons. Something like:

1
2
3
4
5
return (!pow) 
    ? 0 
    : (pow > num)
    ? digits(num, pow / 10, sep)
...


By this it may also be possible to make a more complex recursive things, where you only use one subfunction callig itself with a outer parameter:
 
par==0?( (!pow) ? 0 : ... ? (...) etc ):(par==1)

or
1
2
3
int xxx(int par, int var) {
   return par==0?( var=var*2):(par==1?xxx(2,var):(par==2?(var=var*5) etc.
}


Finally you can do it all in one function without loop and 2 to 3 semicolons. Just make a huge ?-:- consrtuction with:
 
std::cout << (in=="0" ? "1*0": in=="1" ? "1*1" : in=="2" ? ....);

Or write a program to create this construction - where you can use a loop...


F.
Last edited on
And one more:
A string-array with all solutions.

Enter 8468 and a string at index 8468 will be given out with "8648 = 8*1000 + ...".

...but don't forget to insert "teachers are ..." for an input of number 23 ;)
Last edited on
artganseforth:

I don't get it, can you write the complete program and explain? Thanks.
@artganseforth:

I made this but I don't know how to use that things you said. Can you turn this program to work like that, the dirty way?

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

int main()
{
	size_t num;

	std::cout << "Input number: ";
	if (!(std::cin >> num) || (num > 99999))
		return !!(std::cout << "Bad number" << std::endl);

	std::cout << num;

	size_t div, plus;
	if (plus = div = num / 10000)
		std::cout << " = " << div << "*10000";
	 
	if (div = (num = num % 10000) / 1000)
		std::cout << (plus ? " + " : " = ") << div << "*1000";

	if (plus |= div, div = (num = num % 1000) / 100)
		std::cout << (plus ? " + " : " = ") << div << "*100";

	if (plus |= div, div = (num = num % 100) / 10)
		std::cout << (plus ? " + " : " = ") << div << "*10";

	if (plus |= div, div = num % 10)
		std::cout << (plus ? " + " : " = ") << div << "*1";
}
Okay, i'll try to help, but i'm good in coding other things. Console and c-basics is 20 years ago for me.

Array solution is easyer and might look like this:
1
2
3
4
5
6
std::cout << ( {
"1 x 1",
"2 x 1",
"3 x 1",
...
}[std::cin] );

(use eg. javascript an a browser to generate the full code ;) )

the other one - puh...
F...ing console things but your lines 14 to 21 might look likee this:
1
2
3
4
5
6
7
8
9
10
11
	/*if*/ (plus = div = num / 10000) ? /*then*/
		(std::cout << " = " << div << "*10000")
	/*else*/ : 
	/*if*/ (div = (num = num % 10000) / 1000) ? /*then*/
		(std::cout << (plus ? " + " : " = ") << div << "*1000")
	/*else*/ :
	/*if*/ (plus |= div, div = (num = num % 1000) / 100) ? /*then*/
		(std::cout << (plus ? " + " : " = ") << div << "*100") 
	/*else*/ :
...



ps. In fact i neve rrealy used << for myself
Last edited on
Sikkirigi why are you using the bitwise operator |= in your code, lines 20,23,26? I removed and it does the exact same thing, I don't believe it too be necessary. Also your ternary operator statements inside your if statements always evaluate to true I don't see the point of having them when you could just replace them with +.
Alright guys, this has got to be the most disgusting, vile, horrid, code I have ever written in my life, and I got this problem with 1 semicolon and 2 variables. I'll be right back once I have the 0 semicolon 1 variable solution LOL.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cmath>
int8_t counter = 0, num[19];
bool zeros(int val) {
    if (num[val]) {
        if((std::cout << "0", zeros(val + 1))){}
    }
}
bool display() {
	if (num[counter]) {
		if(std::cout << " + " << num[counter++] << "*1", zeros(counter), display()) {}
	}
}
bool get_input() {
	if (std::cin >> num[counter++], std::cout << num[counter - 1]) {}
	if (std::cin.peek() != 10 && get_input()) {}
}
int main() {
	if(get_input(), counter = 1, std::cout << " = " << num[0] << "*1", zeros(counter), display(), system("pause")){}
	//He did say no semicolons only :)
}
Last edited on
Here it is. The 0 semicolon 1 variable solution LOL.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
bool zeros(int8_t num[21]) {
	if (num[num[19] + 1] != 0) {
		if ((num[19] += 1, std::cout << '0', zeros(num))) {}
	}
	if (num[19] = num[20]) {}
}
bool display(int8_t num[21]) {
	if (num[num[20]] != 0) {
		if (std::cout << " + " << num[num[20]++] << "*1", zeros(num), display(num)) {}
	}
}
bool get_input(int8_t num[21] = { 0 }) {
	if (std::cin >> num[num[19]++], std::cout << num[num[19] - 1]) {}
	if (std::cin.peek() != 10 && get_input(num)) {}
	else { if (num[20] == 1) { if (num[19] = 0, std::cout << " = " << num[0] << "*1", zeros(num)) {} } }
	if (display(num)) {}
}
int main() {
	if (int8_t x[21] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }) {
		if (get_input(x), system("pause")) {}
	}
	//He did say no semicolons only :)
}
Last edited on
@rabster:

It should say "bad number" when you enter a letter. And can you do it shorter and more understandable way with using semicolons? Because I need to explain the codes to the teacher and it will be harder with this way. :P 3 variables, 10 semicolons maximum will be enough. Thanks
Topic archived. No new replies allowed.