Pi Project

Mar 4, 2014 at 5:30pm
We were assigned to make a pi project for pi day at school, and I decided to make a a game where it shows you 200 digits of pi, and then you enter as many as you can. For some reason the program won't work right, and I can't find why.

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
#include <iostream>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>
#include <cstdlib>

using namespace std;

int main()
{
    cout << "Welcome to the Pi Game!\n";
    system("Pause");
    system("CLS");
    cout << "In this game, you will input characters of pi until you get one wrong.\nWhen you get one wrong, you lose.\n";
    system("Pause");
    system("CLS");
    cout << "Please input the digits one at a time.\n";
    system("Pause");
    system("CLS");
    cout << "You will have 30 seconds to memorize as many digits of pi as you can, starting with the next click of a button.\n";
    system("Pause");
    system("CLS");
    cout << "3.14159265358979323846264338\n3279502884197169399375105820\n9749445923078164062862089986\n2803482534211706798214808651\n3282306647093844609550582231\n7253594081284811174502841027\n0193852110555964462294895493\n038196";
    _sleep(30000);
    system("CLS");
    cout << "Begin (you do need to put in the decimal.\n";
    int positionInPi;
    char userInput;
    char pi[202];
    for(int x = 0; x <= 201; x++)
    {
            positionInPi = 0;
            userInput = 0;
            char pi[202] {3,'.',1,4,1,5,9,2,6,5,3,5,8,9,7,9,3,2,3,8,4,6,2,6,4,3,3,8,3,2,7,9,5,0,2,8,8,4,1,9,7,1,6,9,3,9,9,3,7,5,1,0,5,8,2,0,9,7,4,9,4,4,5,9,2,3,0,7,8,1,6,4,0,6,2,8,6,2,0,8,9,9,8,6,2,8,0,3,4,8,2,5,3,4,2,1,1,7,0,6,7,9,8,2,1,4,8,0,8,6,5,1,3,2,8,2,3,0,6,6,4,7,0,9,3,8,4,4,6,0,9,5,5,0,5,8,2,2,3,1,7,2,5,3,5,9,4,0,8,1,2,8,4,8,1,1,1,7,4,5,0,2,8,4,1,0,2,7,0,1,9,3,8,5,2,1,1,0,5,5,5,9,6,4,4,6,2,2,9,4,8,9,5,4,9,3,0,3,8,1,9,6};
            cin >> userInput;
            if (pi[positionInPi] != userInput)
            {
                break;
            }
            positionInPi++;
    }
    cout << "Game Over!\n";
    cout << "You knew " << positionInPi+1 << " digits of pi.\n";
    return 0;
}
Mar 4, 2014 at 5:38pm
On line 34, you forgot to put single quotes around each of the digits of pi. Always be aware that '0' != 0. I don't know why you didn't just use a regular string literal like you did on line 23?
Last edited on Mar 4, 2014 at 5:39pm
Mar 5, 2014 at 2:51pm
I tried it first with the single quotes around each digit of pi, and that didn't work, so I took them out to see if it would work.
Last edited on Mar 5, 2014 at 2:52pm
Mar 5, 2014 at 3:10pm
You use the variable "positionInPi" which you always set to 0 - why don't you use the x variable?? It seems like you're doing pre-loop initialization inside of the loop by mistake.

Also, why did you mark this as solved? You won't get help if people think this is solved.
Last edited on Mar 5, 2014 at 3:12pm
Mar 5, 2014 at 5:14pm
@L B
I hadn't realized that I was comparing a char and an int, so I changed them to both be ints. I also set positionInPi to 0 before starting the loop, so it was being set to 0 every time it looped. I fixed it and the program works like a charm. Thanks for your help!
Last edited on Mar 5, 2014 at 5:20pm
Mar 5, 2014 at 7:02pm
Ah its already solved.Anyway heres my version of 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
#include <conio.h>
#include <windows.h>
void _clrscr()
{
    HANDLE conOut=GetStdHandle(STD_OUTPUT_HANDLE);
    COORD Home={0,0};
    FillConsoleOutputCharacter(conOut,(TCHAR)' ',1000,Home,nullptr);
    SetConsoleCursorPosition(conOut,Home);
}
int main()
{
    char pi[]="3."
"141592653589793238462643383279502884197169399375105"
"82097494459230781640628620899862803482534211706798"
"21480865132823066470938446095505822317253594081284"
"8111745028410270193852110555964462294895493038196";
char a,ctr=0;
_cputs("You will have 3 seconds to memorize as many digits of pi as you can, starting with the next click of a button.\n");
_getch();
_cputs(pi);
Sleep(3000);
_clrscr();
while (_kbhit()) _getch();
_cputs("Begin (you do need to put in the decimal.\n");
while(a=_getch()==pi[ctr]&&ctr!=sizeof pi-1) _putch(pi[ctr++]);
_cputs("Game Over!\n");
_cprintf("You knew %d digits of pi",ctr-1);
    return 0;
}
Last edited on Mar 5, 2014 at 7:36pm
Mar 5, 2014 at 7:13pm
@SorinAlex: you do know that string literals concatenate with no operator between them? There's no need to escape the newlines like that on lines 12-16, just make them all separate string literals.
Mar 5, 2014 at 7:34pm
@L B thanks i though that only worked if the strings were on the same line !
Topic archived. No new replies allowed.