No Output? Can someone proof read this please?

This is a discussion on No Output? Can someone proof read this please? within the C++ Programming forums, part of the General Programming Boards category; Hey there... been a while since I have posted here : ) I was just wondering if someone could skim ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    24

    No Output? Can someone proof read this please?

    Hey there... been a while since I have posted here : ) I was just wondering if someone could skim over my code for me. I apparently have a problem somewhere that I just can't seem to find. I have stared at the code for hours trying to find my mistake but can't seem to find it. This program is for a class assignment, if you didn't notice, my teacher likes it to look pretty. The comments basically tell you what the program is supposed to do. My problems is, I get no syntax or link errors, but when I execute, I get no output whatsoever. "Press any key to continue"
    I am sure it is some minor problem I missing. I appreciate any and all help I can get!
    Thank you in advance

    Code:
    //File:   areDistinct4.cpp
    
    
    /////////////////////////////////////////////////
    // Include files
    /////////////////////////////////////////////////
    
    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    /////////////////////////////////////////////////
    // Prototypes
    /////////////////////////////////////////////////
    
    void display (int a[], int n);
    void populate (int a[], int &n);
    void testDeleteAtPos (void);
    bool deleteAtPos (int a[], int &n, int p);
    
    /////////////////////////////////////////////////
    // Constants
    /////////////////////////////////////////////////
    
    const int MAX_VALUE = 50;
    const int MAX_COUNT = 10;
    const int TEST_CASES = 20;
    
    /////////////////////////////////////////////////
    // Main Examples
    /////////////////////////////////////////////////
    
    void main(void)
      {
      srand(time(NULL));
    
      void testDeleteAtPos (void);
      }
    
    /////////////////////////////////////////////////
    // bool deleteAtPos ( int a[], int &n, int p)
    /////////////////////////////////////////////////
    
    /*
    Deletes the value at position p from the array a[], if
    successful then n--, and returns true. If not successful
    then returns false.
    
    Examples:
    a[] = {}, n=0, p=12 => a[] = {}, n=0, false
    a[]={21},n=1, p=0 => a[]={},n=1, true
    a[]={21, 22},n=2, p=0 => a[]={22},n=2, true
    a[]={21, 22, 21},n=3, p=1  => a[]={21, 21}, n=2, true
    
    Algorithm
    if n = 0 then return false
    if p> n - 1 then return false
    if p < 0 then return false
    if p == n - 1 then
    	n--, return true
    for i = p to n - 2	Step 1
    	a[i] = i+1]
    	end for
    	n--, return true
    */
    
    bool deleteAtPos ( int a[], int &n, int p)
      {
      if (n == 0)
        return false;
    
      if (p > (n - 1))
        return false;
    
      if (p < 0)
        return false;
      
      if (p == (n - 1))
        {
        n--;
        return true;
        }
    
      for (int i = p; i <= n - 2; i++)
        a[i] = a [i+1];
        
        n--;
        
        return true;
      }
    
    /////////////////////////////////////////////////
    //void test DeleteAtPos (void)
    /////////////////////////////////////////////////
    
    /*
    Algorithm:
    create an array a[], n, result, p
    do the following 10 times
    fill a[] with random values
    put a random value in p
    display [], p
    result = deleteAtPos (a, n, p)
    display result, a[]
    */
    
    void testDeleteAtPos (void)
      {
      cout << "***************\n";
      cout << "testDeleteAtPos\n";
      cout << "***************\n";
    
      int a[MAX_COUNT];
      int n, p;
      bool result;
    
      for (int c = 1; c <= TEST_CASES; c++)
        {
        populate (a, n);
        
        p = -2 + rand()%(MAX_COUNT +3);
    
        display (a, n);
    
        cout << "p = " << p << endl;
        
        result = deleteAtPos (a, n, p);
    
        cout << "result = " << result << endl;
    
        display (a,n);
    
        cout <<"---------------------------\n";
        }
      }
    
    /////////////////////////////////////////////////
    // void populate(int a[], int &n)
    /////////////////////////////////////////////////
    
    void populate(int a[], int &n)
      {
      n = rand()%(MAX_COUNT+1); //n=0 to 10
    
      for (int i = 0; i <= n - 1; i++)
        a[i] = rand()%(MAX_VALUE+1);
    
      }
    
    /////////////////////////////////////////////////
    // void display(int a[], int n)
    /////////////////////////////////////////////////
    
    void display(int a[], int n)
      {
      cout << "a[" << n << "] = ";
      for (int i = 0; i <= n - 1; i++)
        cout << a[i] << ' ';
    
      cout << endl;
      }
    
    /////////////////////////////////////////////////
    /////////////////////////////////////////////////
    Thanks again!!

    GrlNewB

  2. #2
    btq
    btq is offline
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    your problem is here:
    Code:
    void main(void)
      {
      srand(time(NULL));
    
      void testDeleteAtPos (void); // <---- here
      }
    void testDeleteAtPos (void) doesn't call the function.
    Since it doesn't return a value or take any parameters
    just exchange that line with:
    testDeleteAtPos();

    this will call the function testDeleteAtPos() and hopefully do what you
    want it to do.

    /btq
    ...viewlexx - julie lexx

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    One other, noteworthy item: main returns an int (according to both C99 and C++98 standards), not void.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Registered User slaveofthenet's Avatar
    Join Date
    Apr 2003
    Posts
    80
    Yes, do what btq said. Also, you're using deprecated header, try this:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    You'll also want to use int main() instead of void main(), here's why.
    Detailed understanding of language features - even of all features of a language - cannot compensate for lack of an overall view of the language and the fundamental techniques for using it. - Bjarne Stroustrup

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    24
    lol... thank you all!! I knew it was going to be something stupid... I must have got things mixed around when I was copying my code from the original program.
    It worked great! Thanks a bunch!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Connecting input iterator to output iterator
    By QuestionC in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2007, 03:18 AM
  2. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  3. Replies: 1
    Last Post: 12-31-2001, 05:04 PM
  4. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 04:28 PM
  5. To all who read last post formatted output
    By spliff in forum C Programming
    Replies: 8
    Last Post: 08-21-2001, 04:37 AM

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