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

In summary: Thanks for your help!In summary, the programmer was trying to create a program that would keep asking the user to input numbers until the input was 0. But the programmer ran into a problem because the assignment also required the program to output the highest and lowest value of all the input when the user input 0. The programmer solved the problem by initializing two variables and setting them to the input number on the first input.
  • #1
FallArk
127
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
  • #2
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?
 
  • #3
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. :)
 
  • #4
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!
 

1. What is C++?

C++ is a high-level, general-purpose programming language that was developed by Bjarne Stroustrup in 1983. It is an extension of the C programming language and is one of the most widely used programming languages in the world.

2. Why do I need help with my C++ homework?

C++ can be a complex and challenging language to learn, especially for beginners. Seeking help with your C++ homework can provide you with a better understanding of the language and improve your programming skills.

3. How can I find reliable help with my C++ homework?

You can find reliable help with your C++ homework by asking your classmates or teacher for assistance, joining online forums and communities, or hiring a professional tutor or programmer.

4. What are some common challenges with C++ homework?

Some common challenges with C++ homework include understanding the syntax and structure of the language, troubleshooting errors, and implementing complex algorithms and data structures.

5. How can I improve my understanding of C++ for future assignments?

To improve your understanding of C++, you can practice regularly, read and study code written by experienced programmers, and keep up with the latest updates and developments in the language.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
4
Replies
118
Views
6K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
Replies
14
Views
31K
  • Programming and Computer Science
Replies
2
Views
876
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
6
Views
8K
Back
Top