What's wrong with my program? (Basic C++ Do While)

I'm currently doing an assignment for C++ (Beginners Class) and can't seem to get my program to successfully repeat itself upon the asking of the user. I know that seems hard to explain but you'll understand when you see the code what I'm trying to do.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<iostream>
using namespace std;

int main()
{
    int number{};
    int sum = 0, smallest = 0, largest = 0, count = 0;
    char choice;

    do 
    {
        cout << "Enter 10 numbers seperated by 'space' or 'enter'.\n";

        for (count = 0; count < 10; count++)
        {
            cin >> number;


            sum = sum + number;

            if (number > largest)
            {
                largest = number;
            }

            if (number <= smallest)
            {
                smallest = number;
            }

        }
        cout << "The sum of all your numbers is " << sum << endl;
        cout << "Your largest number is:  " << largest << endl;
        cout << "Your smallest number is:  " << smallest << endl;

        while (choice != 'Y'());
        { 
            cout << "Sorry, that is not a valid choice." << endl;
            cout << "Please try again." << endl << endl << endl;
        } 
           cout << "Would you like to do it again? Y/N ";
           cin >> choice;

    } while (choice == 'Y');
   


This is what I have so far, but when I enter the program the message "Would you like to do it again?" doesn't even display and there's no option to say yes or no. The program simply ends after displaying the Sum, Largest, and Smallest.
Last edited on
Please edit your post and format your code with code tags:

[code] 

   { your code here }

[/code]


'Y'()
'Y' is a character literal, not a function. Remove the (). Your code doesn't compile as-is.

The syntax for a while loop is:
1
2
3
4
while (condition)
{
    commands;
}

Notice that there is no semi-colon at the end of the while line. It's like an if-statement in this way.

The syntax for a do-while loop is:
1
2
3
4
5
do
{
    commands;

} while (condition);

Notice here that there is a semi-colon at the end of the do-while loop.
Last edited on
Hello Struglling84,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Another way of looking at your code:
1
2
3
4
5
6
7
8
9
while (choice != 'Y'())
    ;
{
    cout << "Sorry, that is not a valid choice." << endl;
    cout << "Please try again." << endl << endl << endl;
}

cout << "Would you like to do it again? Y/N ";
cin >> choice;

It really makes no difference to the compiler where the ";", line 2, is the while loop becomes an endless loop because "choice" never changes. Even if you remove the () and the ";" "choice" contains a garbage value that may never be equal to 'Y', so it will always be true.

After that the while loop makes no sense because you are checking for a value of "choice" before you give it a value 'Y' or otherwise.

Consider this:
1
2
3
4
5
6
7
8
    cout << "The sum of all your numbers is " << sum << endl;
    cout << "Your largest number is: " << largest << endl;
    cout << "Your smallest number is: " << smallest << endl;

    cout << "Would you like to do it again? Y/N: ";
    std::cin >> choice;

} while (std::toupper(choice) != 'N');

I believe that Ganado meant the ";" at the end of the while loop inside the do/while loop. The ";" at the end of the do/while loop is needed.

The code runs, but you do have a problem with smallest = 0. Unless you enter less than zero it will always show zero as the smallest even if zero is not entered. Try smallest = INT_MAX. You start with a very high number and adjust as the do/while loop runs.

Andy
Thank you both Andy and Ganado so much for your help!

Sorry about the formatting in my question this was my first post for this website.

Anyway, the issue was indeed initially with my code but also my the program I used to run it. Visual Studio 2019 is buggy and it prevented me from running updated code and just kept running an old version despite me making any changes. This is solved simply by opening up a new program.

I hope you both have a blessed week.
Visual Studio 2019 is buggy and it prevented me from running updated code and just kept running an old version despite me making any changes.


I doubt that is an issue with VS2019. I use it extensively and IMO it's the best IDE

If code is changed within VS main code window then it is saved before the solution is built. So the generated .exe always reflects the current code.
Hello Struglling84,


I have had the same problem with VS2015 and VS2017. It came down to how the file was opened.

If you open the file through the Solution Explorer every thing is fine. It will automatically save the file before the compile.

If you use "Ctrl + O" to open the file it is more like a copy and you have to manually save any changed before the compile.

I have found it best to go through the Solution Explorer to open a file.

Andy
Build builds the solution using files as specified by the Solution Explorer. If files are opened outside of Solution Explorer they are not used as part of the solution build. This isn't a 'bug' it's as by design. VS is a large complex IDE (even the Community Version) and like Word, Excel etc etc you have to take the time and trouble to learn how to use it.
Topic archived. No new replies allowed.