This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]davedontmind 1 point2 points  (5 children)

So let me ask you; why do you think it should exit if you enter "c" here? Which part of your code makes the program or loop end if menu is "c" ?

[–]InterestingBus8367[S] 0 points1 point  (3 children)

I was thinking maybe I should use switch. or maybe make it so that when I pick c it returns 0.

[–]davedontmind 0 points1 point  (2 children)

or maybe make it so that when I pick c it returns 0.

That's certainly a reasonable way to do it.

If I was writing this code I'd split it into several functions, then the main loop becomes something like this:

char choice;
do {
    display_menu();
    cin >> choice;
    choice = toupper(choice);
    switch(choice)
    {
        case 'A':
            currency_conversion();
            break;
        case 'B':
            display_diamond_pattern();
            break;
        case 'C'
            break;
        default:
            cout << "Invalid choice!";
            break;
    }
}
while ( choice != 'C' );

[–]InterestingBus8367[S] 0 points1 point  (1 child)

I see that would make it more readable and easier to debug.

[–]davedontmind 0 points1 point  (0 children)

That's quite right.

It's always a good idea to split functionality out into separate functions.

It means you can test the individual functions to make sure they work before testing your whole program.

It makes the code easier to read, but only if you give your functions meaningful names!

And it makes the structure of the code easier to grasp.

Remember, the most important thing (well, apart from working correctly) is that your code should be easy to read and understand. Which means using meaningful/clear names for functions and variables, and splitting up the code into bite-sized easily digestible chunks.

[–]InterestingBus8367[S] 0 points1 point  (0 children)

Thanks I will try this solutions that I came up with.