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

  • Thread starter Thread starter FallArk
  • Start date Start date
  • Tags Tags
    C++ Homework
AI Thread Summary
The discussion revolves around a programming assignment that requires creating a code block to continuously prompt the user for numbers until a 0 is entered. The program must also handle the case where 0 is the first input, displaying "No data submitted!" and, upon termination, output the highest and lowest values of the entered numbers. The initial code provided successfully prompts for input but lacks the functionality to track the maximum and minimum values.Participants suggest using an array to store inputs but also highlight potential limitations regarding input size. An alternative approach is recommended, which involves initializing two variables to track the maximum and minimum values without using an array. The solution involves setting these variables equal to the first input and updating them based on subsequent inputs. The original poster confirms that they successfully implemented this solution.
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!
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Back
Top