Get Help with C++ Homework: Solving Problems with User Input

  • Context: C/C++ 
  • Thread starter Thread starter FallArk
  • Start date Start date
  • Tags Tags
    C++ Homework
Click For Summary

Discussion Overview

The discussion centers around a C++ homework assignment that requires participants to create a program for user input of numbers, which continues until the user inputs 0. The program must also handle cases where no data is submitted and must determine the highest and lowest values from the inputs.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their current implementation, which correctly handles user input and checks for the termination condition (input of 0) but is uncertain about how to track the highest and lowest values.
  • Another participant suggests that while infinite entries are unlikely due to physical constraints, they recommend limiting the input size to a manageable number, such as 1024, and asks if the original poster knows how to implement this.
  • Some participants propose an alternative approach that avoids using an array, suggesting the use of two variables to track the maximum and minimum values, updating them based on user input.
  • A later reply confirms that the participant successfully implemented the two-variable approach for tracking maximum and minimum values.

Areas of Agreement / Disagreement

There is no consensus on the best approach to handle the input values, with some participants advocating for the use of an array while others prefer using two variables. The discussion reflects differing opinions on how to manage user input effectively.

Contextual Notes

Participants have not resolved the question of how to handle potential buffer overruns or the implications of limiting input size. There is also a lack of clarity on the assumptions regarding the range of input values.

FallArk
Messages
127
Reaction score
0
I ran into some problems when trying to finish my assignment. The objective of this assignment is to create a block of code that will keep asking the user to input numbers until the input is 0. And if the user input 0 for the first value it will say "No data submitted!". I got all those parts, but the assignment also requires me to let the program output the highest and lowest value of all the input when the user input 0 (the user ended the program).
This is what I have done so far:

Code:
#include <iostream>
using namespace std;
int main() {
    int input = 1;
    cout << "Enter a number: ";
    cin >> input;
    if (input == 0) {
        cout << "No data submitted!";
    }
        else {
            while (input != 0) {
                cout << "Enter a number: ";
                cin >> input;
            }
        }
        return 0;
}

I don't really know how to rank the numbers, I think I should use array, but I'm not sure if the user then can input an infinite amount of values. Thanks in advance!
 
Technology news on Phys.org
I wouldn't worry about an infinite amount of entries. Due to physical constraints the input would have to end (possibly in a buffer overrun) so limit the size of your array to what the machine can handle. Do you know how to do this? If not, I don't think it's unreasonable to limit your buffer size to, say, 1024 numbers having a value between -231 and 231 (for a 32-bit machine).

Having said that, can you now set up your array and write the code for finding the highest and lowest numbers?
 
I don't think I would use an array. I would initialize 2 variables...one for the maximum and one for the minimum and on the first input, set both of these equal to the input number. Then on subsequent inputs, if the input is less than the current minimum, set the minimum to the input value, and it the input is greater than the current maximum, set the maximum to the input value. :)
 
MarkFL said:
I don't think I would use an array. I would initialize 2 variables...one for the maximum and one for the minimum and on the first input, set both of these equal to the input number. Then on subsequent inputs, if the input is less than the current minimum, set the minimum to the input value, and it the input is greater than the current maximum, set the maximum to the input value. :)

That's what I did! I figured it out!
 

Similar threads

Replies
12
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 1 ·
Replies
1
Views
6K