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
Click For Summary
SUMMARY

The discussion focuses on solving a C++ homework assignment that requires creating a program to continuously accept user input until the user enters 0. If 0 is the first input, the program outputs "No data submitted!". The solution involves initializing two variables to track the highest and lowest values entered by the user. The final implementation avoids using an array, instead utilizing conditional statements to update the maximum and minimum values based on user input.

PREREQUISITES
  • Basic understanding of C++ syntax and structure
  • Knowledge of control flow statements (if, while)
  • Familiarity with variable initialization and data types
  • Concept of user input handling in C++
NEXT STEPS
  • Learn about C++ arrays and their limitations in handling user input
  • Explore C++ functions for modular programming
  • Study error handling techniques for user input in C++
  • Investigate algorithms for finding maximum and minimum values in datasets
USEFUL FOR

C++ students, beginner programmers, and anyone looking to improve their skills in handling user input and data processing in C++.

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!
 
We have many threads on AI, which are mostly AI/LLM, e.g,. ChatGPT, Claude, etc. It is important to draw a distinction between AI/LLM and AI/ML/DL, where ML - Machine Learning and DL = Deep Learning. AI is a broad technology; the AI/ML/DL is being developed to handle large data sets, and even seemingly disparate datasets to rapidly evaluated the data and determine the quantitative relationships in order to understand what those relationships (about the variaboles) mean. At the Harvard &...

Similar threads

Replies
12
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 14 ·
Replies
14
Views
34K
  • · Replies 118 ·
4
Replies
118
Views
10K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 39 ·
2
Replies
39
Views
5K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 6 ·
Replies
6
Views
12K
Replies
12
Views
2K