programmin woes

This is a discussion on programmin woes within the C++ Programming forums, part of the General Programming Boards category; hi guys. i've been making a simple (or so i thought) memory test game on c++ to help me learn. ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    26

    Question programmin woes

    hi guys.

    i've been making a simple (or so i thought) memory test game on c++ to help me learn. ive encountered a number of problems that were eventualy solvable and ive been at this one for hours:



    Code:
    #include <cstdlib>
    #include <iostream>
    #include <windows.h>
    #include <stdlib.h>
    
    
    using namespace std;
    
    int main()
    
    {
    int numberarray[20];
    int numberarrayplace;
    int numberrecal;
    int numberrecalamount;
    int exitcheckloop;
    cout<<"Welcome to Dave's memory lab! \n";
    cout<<"Here we will carry out a test: \n";
    cout<<"Remember the numbers! \n";;
    cout<<"you have 30 seconds to memorize them \n";
    cout<<"Then you will be tested \n";
    cout<<"Press enter to continue... \n";
    cin.get();
    Sleep(1000);
    cout<<"3 \n";
    Sleep(1000);
    cout<<"2 \n";
    Sleep(1000);
    cout<<"1 \n";
    Sleep(1000);
    system("cls");
    
    cout<<"Here are your numbers \n";
    numberarrayplace = 1;
    do {
       numberarray[numberarrayplace] = rand() % 99 + 1;
       cout<< numberarray[numberarrayplace] << " \n";
       numberarrayplace = numberarrayplace + 1;
       } while ( numberarrayplace < 21 );
    Sleep(30000);
    system("cls");
    cout<<"Times up! \n";
    cout<<"Now you must type the numbers back in!  \n";
    
            
         numberarrayplace = 0;
         numberrecalamount = 1;
         
         do {
        
         cin>> numberrecal;
         exitcheckloop = 0;
         do {
                
    
         if ( numberarrayplace > 20 ) {
                   
         cout<<"Incorrect! \n";
         numberrecalamount = numberrecalamount + 1;
         numberarrayplace = 0;
         exitcheckloop = 1;
         }
         
         else if ( numberarray[numberarrayplace] = numberrecal) {
         cout<<"Correct! \n";
         numberarrayplace = 0; 
         numberrecalamount = numberrecalamount + 1;
         exitcheckloop = 1;
         }
         
         else {
         numberarrayplace = numberarrayplace + 1;
         
             }
             } while ( exitcheckloop = 0 );
         
         } while ( numberrecalamount < 21 );
    
    }
    in the phase of the user typing back the numbers no matter what you write it comes up as correct. im probably just being stupid but i cant work out how to make it come up as incorrect when the user puts an incorrect value.

    can anyone help me !?!?!

    thanks

    gillypie

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,336
    Well some half-decent indentation and vertical white-space (aka a blank line) would help you figure out the program flow (and help us in the process).
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <windows.h>
    // #include <stdlib.h> you already included cstdlib
    
    
    using namespace std;
    
    int main()
    {
        int numberarray[20];
        int numberarrayplace;
        int numberrecal;
        int numberrecalamount;
        int exitcheckloop;
        cout << "Welcome to Dave's memory lab! \n";
        cout << "Here we will carry out a test: \n";
        cout << "Remember the numbers! \n";;
        cout << "you have 30 seconds to memorize them \n";
        cout << "Then you will be tested \n";
        cout << "Press enter to continue... \n";
        cin.get();
        Sleep(1000);
        cout << "3 \n";
        Sleep(1000);
        cout << "2 \n";
        Sleep(1000);
        cout << "1 \n";
        Sleep(1000);
        system("cls");
    
        cout << "Here are your numbers \n";
        numberarrayplace = 1;
        do {
            numberarray[numberarrayplace] = rand() % 99 + 1;
            cout << numberarray[numberarrayplace] << " \n";
            numberarrayplace = numberarrayplace + 1;
        } while (numberarrayplace < 21);
    
        Sleep(30000);
        system("cls");
        cout << "Times up! \n";
        cout << "Now you must type the numbers back in!  \n";
    
        numberarrayplace = 0;
        numberrecalamount = 1;
    
        do {
            cin >> numberrecal;
            exitcheckloop = 0;
            do {
                if (numberarrayplace > 20) {
                    cout << "Incorrect! \n";
                    numberrecalamount = numberrecalamount + 1;
                    numberarrayplace = 0;
                    exitcheckloop = 1;
                }
                else if (numberarray[numberarrayplace] = numberrecal) {
                    cout << "Correct! \n";
                    numberarrayplace = 0;
                    numberrecalamount = numberrecalamount + 1;
                    exitcheckloop = 1;
                }
                else {
                    numberarrayplace = numberarrayplace + 1;
    
                }
            } while (exitcheckloop = 0);
        } while (numberrecalamount < 21);
    }
    Step 1 would be to replace an = with an ==
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    so when do i change the = to == ?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,336
    > else if (numberarray[numberarrayplace] = numberrecal)
    What's this look like to you - assignment or comparison?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    yeh i thought so but it didnt fix the problem

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    horrah there was another bit to do as well and it works.

    here u go.
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <windows.h>
    
    
    
    using namespace std;
    
    int main()
    {
        int numberarray[20];
        int numberarrayplace;
        int numberrecal;
        int numberrecalamount;
        int exitcheckloop;
        cout << "Welcome to Dave's memory lab! \n";
        cout << "Here we will carry out a test: \n";
        cout << "Remember the numbers! \n";;
        cout << "you have 30 seconds to memorize them \n";
        cout << "Then you will be tested \n";
        cout << "Press enter to continue... \n";
        cin.get();
        Sleep(1000);
        cout << "3 \n";
        Sleep(1000);
        cout << "2 \n";
        Sleep(1000);
        cout << "1 \n";
        Sleep(1000);
        system("cls");
    
        cout << "Here are your numbers \n";
        numberarrayplace = 1;
        do {
            numberarray[numberarrayplace] = rand() &#37; 99 + 1;
            cout << numberarray[numberarrayplace] << " \n";
            numberarrayplace = numberarrayplace + 1;
        } while (numberarrayplace < 21);
    
        Sleep(30000);
        system("cls");
        cout << "Times up! \n";
        cout << "Now you must type the numbers back in!  \n";
    
        numberarrayplace = 0;
        numberrecalamount = 1;
    
        do {
            cin >> numberrecal;
            exitcheckloop = 0;
            do {
                if (numberarrayplace > 20) {
                    cout << "Incorrect! \n";
                    numberrecalamount = numberrecalamount + 1;
                    numberarrayplace = 0;
                    exitcheckloop = 1;
                }
                else if (numberarray[numberarrayplace] == numberrecal) {
                    cout << "Correct! \n";
                    numberarrayplace = 0;
                    numberrecalamount = numberrecalamount + 1;
                    exitcheckloop = 1;
                }
                else {
                    numberarrayplace = numberarrayplace + 1;
    
                }
            } while (exitcheckloop == 0);
        } while (numberrecalamount < 21);
    }

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    You code is full of buffer overruns.
    Arrays start at zero and go up to n-1, where n is the array size, NOT 1 to n
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    oh guys one more thing.

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <windows.h>
    
    
    
    using namespace std;
    
    int main()
    {
        int numberarray[20];
        int numberarrayplace;
        int numberrecal;
        int numberrecalamount;
        int exitcheckloop;
        int memorycount;
        int memoryscore;
        int blah;
        cout << "Welcome to Dave's memory lab! \n";
        cout << "Here we will carry out a test: \n";
        cout << "Remember the numbers! \n";;
        cout << "you have 30 seconds to memorize them \n";
        cout << "Then you will be tested \n";
        cout << "Press enter to continue... \n";
        cin.get();
        Sleep(1000);
        cout << "3 \n";
        Sleep(1000);
        cout << "2 \n";
        Sleep(1000);
        cout << "1 \n";
        Sleep(1000);
        system("cls");
    
        cout << "Here are your numbers \n";
        numberarrayplace = 1;
        do {
            numberarray[numberarrayplace] = rand() &#37; 99 + 1;
            cout << numberarray[numberarrayplace] << " \n";
            numberarrayplace = numberarrayplace + 1;
        } while (numberarrayplace < 21);
    
        Sleep(3000);
        system("cls");
        cout << "Times up! \n";
        cout << "Now you must type the numbers back in!  \n";
    
        numberarrayplace = 0;
        numberrecalamount = 1;
    
        do {
            cin >> numberrecal;
            exitcheckloop = 0;
            do {
                if (numberarrayplace > 20) {
                    cout << "Incorrect! \n";
                    numberrecalamount = numberrecalamount + 1;
                    numberarrayplace = 0;
                    exitcheckloop = 1;
                }
                else if (numberarray[numberarrayplace] == numberrecal) {
                    memorycount = memorycount + 1;
                    cout << "Correct! \n";
                    numberarrayplace = 0;
                    numberrecalamount = numberrecalamount + 1;               
                    exitcheckloop = 1;
                     }
                else {
                    numberarrayplace = numberarrayplace + 1;
    
                }
            } while (exitcheckloop == 0);
        } while (numberrecalamount < 21);
    
    cout << "Ok let's see how you did... \n";
    Sleep(2000);
    memoryscore = 100 * (memorycount / 20);
    cout << "You guessed " << memorycount << " awnsers";
    cout << "Your memory score is " << memoryscore;
    cin >> blah;
    }
    for some reason it gives a far fetched result like 90818573 or something. do u know why?

  9. #9
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by gillypie View Post
    for some reason it gives a far fetched result like 90818573 or something. do u know why?
    It might be a good idea to initialize memorycount and get rid of the array overflows as well.
    Kurt

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    what are array overflows?

  11. #11
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by gillypie View Post
    what are array overflows?
    e.g. to write to numberarray[20] when the array has only 20 slots.
    Kurt

  12. #12
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    oh i see so i only need [19] because 0 counts

  13. #13
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    k i fixed that but the problem remains

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,336
    It's nice that you copy/pasted my nicely formatted version, but how come all the lines you've added make it look like a mess once more?

    Focus on every single detail, and getting it right.

    Oh, and post your latest code since you seem to have made some changes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <windows.h>
    
    
    
    using namespace std;
    
    int main()
    {
        int numberarray[19];
        int numberarrayplace;
        int numberrecal;
        int numberrecalamount;
        int exitcheckloop;
        int memorycount;
        int memoryscore;
        int blah;
        cout << "Welcome to Dave's memory lab! \n";
        cout << "Here we will carry out a test: \n";
        cout << "Remember the numbers! \n";;
        cout << "you have 30 seconds to memorize them \n";
        cout << "Then you will be tested \n";
        cout << "Press enter to continue... \n";
        cin.get();
        Sleep(1000);
        cout << "3 \n";
        Sleep(1000);
        cout << "2 \n";
        Sleep(1000);
        cout << "1 \n";
        Sleep(1000);
        system("cls");
    
        cout << "Here are your numbers \n";
        numberarrayplace = 1;
        do {
            numberarray[numberarrayplace] = rand() &#37; 99 + 1;
            cout << numberarray[numberarrayplace] << " \n";
            numberarrayplace = numberarrayplace + 1;
        } while (numberarrayplace < 21);
    
        Sleep(3000);
        system("cls");
        cout << "Times up! \n";
        cout << "Now you must type the numbers back in!  \n";
    
        numberarrayplace = 0;
        numberrecalamount = 1;
        memorycount = 0;
    
        do {
            cin >> numberrecal;
            exitcheckloop = 0;
            do {
                if (numberarrayplace > 19) {
                    cout << "Incorrect! \n";
                    numberrecalamount = numberrecalamount + 1;
                    numberarrayplace = 0;
                    exitcheckloop = 1;
                }
                else if (numberarray[numberarrayplace] == numberrecal) {
                    memorycount = memorycount + 1;
                    cout << "Correct! \n";
                    numberarrayplace = 0;
                    numberrecalamount = numberrecalamount + 1;               
                    exitcheckloop = 1;
                     }
                else {
                    numberarrayplace = numberarrayplace + 1;
    
                }
            } while (exitcheckloop == 0);
        } while (numberrecalamount < 20);
    
    cout << "Ok let's see how you did... \n";
    Sleep(2000);
    memorycount = 20;
    memoryscore = 100 * (memorycount / 20);
    cout << "Your memory score is " << memoryscore;
    cin >> blah;
    
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. struct constructor woes
    By Mr_Jack in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2004, 02:36 PM
  2. NASA Woes!
    By DISGUISED in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 01-22-2004, 08:59 PM
  3. Function calling woes
    By RedZippo in forum C Programming
    Replies: 6
    Last Post: 01-08-2004, 11:39 PM
  4. Dll woes
    By Abiotic in forum Windows Programming
    Replies: 3
    Last Post: 11-09-2003, 10:32 AM
  5. Programmin with the MFC
    By omeydhomey in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2003, 07:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21