Thread: Program not working

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

    Program not working

    Hi Guys!

    Im new to c++ but have made a few programs. I am currently making a memory testing game. it was all working well untill i started using an array to generate random numbers. if the code is too dificult to understand i'll anotate it but other than that can anyone help me?

    here is the code:

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <dos.h>
    #include <windows.h>
    #include <stdlib.h>
    #include <conio.h>
    
    using namespace std;
    
    int main()
    
    {
    int numberarray[20];
    int numberarrayplace;
    cout<<"Welcome to Dave's memory lab! \n";
    cout<<"Here we will carry out a test: \n";
    cout<<"Remember the numbers!";
    cout<<"You will be given 20 numbers from 0 to 100 and you have 30 seconds to memorize them \n";
    cout<<"Then you will be tested... \n";
    Sleep(1000);
    cout<<"3 \n";
    Sleep(1000);
    cout<<"2 \n";
    Sleep(1000);
    cout<<"1 \n";
    Sleep(1000);
    system("cls");
    
    numberarray = 1;
    for ( 1 == 1 ) {
    numberarray[numberarrayplace] = rand(100);
    cout<< numberarray[numberarrayplace] << " ";
    }
    cout<<"Here are your numbers \n";
    Sleep(30000);
    cout<<"Times up! \n";
    }
    it's not finnished yet but im getting these errors:

    C:\Dev-Cpp\Projects\Fun Projects\Memory Chalenge\main.cpp In function `int main()':
    29 C:\Dev-Cpp\Projects\Fun Projects\Memory Chalenge\main.cpp incompatible types in assignment of `int' to `int[20]'
    30 C:\Dev-Cpp\Projects\Fun Projects\Memory Chalenge\main.cpp expected `;' before ')' token
    35 C:\Dev-Cpp\Projects\Fun Projects\Memory Chalenge\main.cpp expected `)' before ';' token
    C:\Dev-Cpp\Projects\Fun Projects\Memory Chalenge\Makefile.win [Build Error] [main.o] Error 1

    i apreciate your help.

    Gillypie

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    for ( 1 == 1 ) {
    numberarray[numberarrayplace] = rand(100);
    for loops have the form
    Code:
    for(initialization; condition; increment)
    All three sections can be empty, but you at least need those two semicolons.

    In addition, rand() takes no parameters. If you want a random number between 0 and 99 inclusive, use
    Code:
    rand() &#37; 100
    Code:
    #include <dos.h>
    #include <windows.h>
    #include <stdlib.h>
    #include <conio.h>
    dos.h and conio.h are non-standard, and you're using nothing from them, so why include them? (Sleep() is from windows.h, however. It's also non-standard.)

    stdlib.h is the same as cstdlib if you have using namespace std. Only include one of them. I suggest cstdlib.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    ok so whats the syntax for adding the colons to the for loop.

    sorry to sound stupid.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You want the loop to execute 20 times, to print 20 numbers, right?

    This is the normal syntax of a for loop for that sort of thing.
    Code:
    for(int x = 0; x < 20; x ++)
    I think that maybe you should read some tutorials about for loops. http://www.cprogramming.com/tutorial/lesson3.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    thnx but ive decided to use a do while loop for the purposes. youve helped me alot though i understand loops properly now. thnx!


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Replies: 5
    Last Post: 02-02-2003, 10:56 AM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. Simple Program not working right (in C)
    By DruzeTito in forum C Programming
    Replies: 5
    Last Post: 06-01-2002, 10:14 PM
  5. Program ive been working on called ChatMate
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-23-2002, 09:05 PM