CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2016
    Posts
    2

    write a C++ program that repeatedly prompts a user to input a positive integer value.

    write a C++ program that repeatedly prompts a user to input a positive integer value. At any point, if the user enters 0 or a negative integer value, then the program should print the minimum of all of the positive integer values entered by the user, and then terminate execution.For example, if a user enters the values 4 13 8 11 2 66 5 0, then the program should report that the smallest of the positive values entered is 2.

    Excuse me, I am new for stackoverflow. Here is the code I wrote so far. However, it keeps printing the last 0 or negative number I inserted, not the minimum number. Thank You!

    `# include <iostream>`
    `using namespace std;`
    `int main(){`
    `int x, y;`
    `cout<<"enter number";`
    `cin>>x;`
    `while(x!=0){`
    `cout<<"enter another number";`
    `cin>>y;`
    `if (y<x)`
    `x=y;`
    `if (y>x)`
    `x=x;`
    `if (y<=0)`
    `x=x;`
    `}`
    `cout<<"the minimum number is"<<x;`
    return 0;
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: write a C++ program that repeatedly prompts a user to input a positive integer va

    Please use code tags when posting code and why is each line delimited by `?

    Code:
    #include <iostream>
    using namespace std;
    
    int main() 
    {
        int x, y;
    
        cout << "enter number";
        cin >> x;
    
        while (x != 0) {
            cout << "enter another number";
            cin >> y;
    
            if (y < x)
                x = y;
    
            if (y > x)
                x = x;
    
            if (y <= 0)
               x = x;
        }
    
        cout << "the minimum number is" << x;
    
        return 0;
    }
    If y > x or y <= 0 then you are setting x equal to x which doesn't change anything so these lines can be omitted.

    To terminate the loop, you are checking x for 0. You are then displaying that the minimum value is x - which is 0! So this always displays 0!

    PS This is codeguru, not stackoverflow! If you are going to x-post, at least change the name in the post !
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jul 2016
    Posts
    2

    Re: write a C++ program that repeatedly prompts a user to input a positive integer va

    Excuse me for the mistake I did. The important thing is I did the code and it works!

    # include <iostream>
    using namespace std;
    int main(){
    int number;
    cout <<"enter number"<<endl;
    cin>> number;
    int min=number;
    while(number>0){
    if (number<min){
    min=number;
    }
    cout<<"enter another number"<<endl;
    cin>>number;
    }
    cout<<"the minimum number is"<<endl<<min;
    return 0;

    }

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: write a C++ program that repeatedly prompts a user to input a positive integer va

    Please use code tags when posting code
    From my post #2, so why post code again without using them? Go Advanced, select the formatted code and click '#'.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured