Like Tree1Likes
  • 1 Post By ZuK

Print out all input values

This is a discussion on Print out all input values within the C++ Programming forums, part of the General Programming Boards category; I have a problem with how to print out all the numbers that the user enters the code looks like ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    91

    Question Print out all input values

    I have a problem with how to print out all the numbers that the user enters the code looks like this so far:

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        int max[2] = {-1000}; 
        int lowest[2] = {1000}; 
        int input[5];
        int size = 4;
    
        for (int i = 1; i < size; i++)
        {
            cout << "Input " << i << ": ";
            cin >> input[0];
            if (input[0] > max[0])
            {
                max[0] = input[0];
            }
            if (input[0] < lowest[0])
            {
                lowest[0] = input[0];
            }
        }
        cout << "Max is: " << max[0] << '\n';
        cout << "Lowest is: " << lowest[0] << '\n';
        cout << "All numbers is: " << input[0] << '\n';
    return 0;
    }
    I want the program to print out all the numbers that the user has entered after the program have said the higest and lowest number is ?. But I do not know how to do this I thought it could be something like this:

    Code:
    cout << input[0];
    //or
    cout << input[i];
    /*Because i is the number of how many enters of numbers the user can do*/
    But that was totally wrong tried to do another "for loop" in the first for loop but that was meaningless because it can't change anything in the first "for loop". Someone who's know what to do.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,313
    Try introducing another "for loop" AFTER the statements that print out max and lowest.

    Remember that statements are executed sequentially, not all at once, and not in some random order. Their order in source code matters. The statement that prints out max is immediately before (above) the one that prints out the lowest, which is why max is printed before the lowest rather than lowest being printed before the max.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    91
    grumpy: Tried to do what you said but I didn't succeed. The code in the end I wrote was this:

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        int max[2] = {-1000};
        int lowest[2] = {1000};
        int input[5];
        int size = 4;
        for (int i = 1; i < size; i++)
        {
            cout << "Input " << i << ": ";
            cin >> input[0];
            if (input[0] > max[0])
            {
                max[0] = input[0];
            }
            if (input[0] < lowest[0])
            {
                lowest[0] = input[0];
            }
        }
        cout << "Max is: " << max[0] << '\n';
        cout << "Lowest is: " << lowest[0] << '\n';
    
          return 0;
    }
    I tried to put in some code like this at the end but no one of these worked:

    Code:
        for (int j = 1; j < size; j++)
    
         //Here is the code that I wrote in the end
        {
            cout << j << input[j];
        }
        //or
        for (int j = 1; j < size; j++)
        {
            cout << j << input[size];
        }
        //or
        for (int j = 1; j < size; j++)
        {
            cout << j << input[0];
        }
        //or without the "j" before the "input[]"

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Your code never stores the input values ( you always read into input[0]) -> there is no way to print them after the input loop.
    BTW why is max and lowest declared as an array ?

    Kurt
    stahta01 likes this.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    91
    Seriously I don't know why max and lowest is declared as arrays. However it doesn't matter if they are declared as arrays or not.

    And yes that's true I always get the input value of input[0] but if I had made a code like this:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int max[2] = {-1000};
        int lowest[2] = {1000};
        int input[5];
        int size = 4;
       
        for (int i = 1; i < size; i++)
        {
            cout << "Input " << i << ": ";
            cin >> input[0];
    
            if (input[0] > max[0])
            {
    
                max[0] = input[0];
            }
            
            if (input[0] < lowest[0])
            {
                lowest[0] = input[0];
            }
        }
        cout << "Max is: " << max[0] << '\n';
        cout << "Lowest is: " << lowest[0] << '\n';
    
        for (int i = 0; i < size; i++)
        {
            i = input[0];
        }
        cout << input[i];
          return 0;
    }
    

    The program would say:
    "error: name lookup of 'j' changed for ISO 'for' scoping"
    I really needs help to get this right.


  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
        for (int i = 0; i < size; i++)  // i is declared here
        {
            i = input[0];
        }
        cout << input[i];   // access to i is not allowed outside the for loop
    only some old MS compilers allow that
    Kurt

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,313
    Quote Originally Posted by ZuK View Post
    only some old MS compilers allow that
    Not true. It was permitted in older versions of C++, too. The construct was written out of the (then) draft C++ standard in 1994 or 1995.
    Right 98% of the time, and don't care about the other 3%.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,635
    In other words, if you must use "i" outside the loop, then move the declaration of i outside the loop.
    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. How to determine and print smallest and largest values
    By Anna Lane in forum C Programming
    Replies: 4
    Last Post: 09-28-2010, 07:05 AM
  2. print input parameter values
    By George2 in forum Windows Programming
    Replies: 0
    Last Post: 06-30-2008, 03:33 AM
  3. print values in a bintree
    By mengqing in forum C++ Programming
    Replies: 4
    Last Post: 05-13-2008, 03:07 AM
  4. forgotten how to print values :(
    By darfader in forum C Programming
    Replies: 3
    Last Post: 08-12-2003, 09:47 AM
  5. print values of function to file
    By threahdead in forum C Programming
    Replies: 3
    Last Post: 10-14-2002, 05:17 PM

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