Thread: highest int

  1. #1
    Registered User
    Join Date
    Jul 2010

    highest int

    I've been trying to make this code work for hours, but it's really messing my head up...
    Its objective is simple, the user chooses how many numbers he wants to input, then the program output the highest.

    #include <iostream>
    #include <iomanip>
    using namespace std;
    int returnHighest(int*,int);
    int main()
        int inputX;
        cout<<"How many numbers do you wanna input? ";
        int listOfNumbers[inputX];
        int n = 1;
        while(n <= inputX)
            int inputY;
            cout<<"Input N. "<<n<<": ";
            listOfNumbers[n] = inputY;
        cout<<"The highest number is: "<<returnHighest(listOfNumbers,inputX);
    int returnHighest(int * list,int arraySize)
    int returnValue;
    //the following FOR loop will
    //search for the highest int
    //stored in "list" int array
    int num = 0;
    for(int x = 0 ; x <= arraySize; x++)
        if(list[x] > num)
            num = list[x];
    returnValue = num;
    return returnValue;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Your first loop starts at 1, and finishes 1 place past the end of the array.

    Your second loop starts at 0, and finishes 1 place past the end of the array.

    Try a
    for ( i = 0 ; i < n ; i++ )
    kind of loop in both places.
    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.

  3. #3
    Join Date
    Oct 2007
    Inside my computer
    Aside from that Salem mentioned:

    >>int listOfNumbers[inputX];
    This is not ever going to work. Ever.
    But that doesn't mean it can't be done easily. You use a vector:

    #include <vector>
    std::vector<int> ListOfNumbers(InputX);

    And, of course, the vector keeps track of its size, so you can do:


    Or even integrate that into the loop:

    for(int x = 0 ; x < list.size(); x++)

    And btw, 0 is not guaranteed to be the lowest possible number. Consider what happens if the user enters negative numbers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM