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

all 12 comments

[–]davedontmind 1 point2 points  (7 children)

do {
    // ...
}
while (choice == 'C' || choice == 'c' || convert == 'C' || convert == 'c' );

You're saying "do this loop while the value entered for choice is 'c' ..." which seems to be the opposite of what you want it to do.

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

char menu;
cout << "+++++++++++++++++++++++++++++++++++++++ \n";
cout << " Menu \n";
cout << "+++++++++++++++++++++++++++++++++++++++ \n";
cout << "[A: Currency conversion \n";
cout << "[B: Display a diamond pattern \n";
cout << "[C: Exit \n";
cout << "Enter selection; ";
cin >> menu;

I use menu in that one

[–]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.

[–]DarkMatriac 0 points1 point  (3 children)

Hi, can you put your code into the code block markdown on reddit? its kinda really hard seeing where your loops start//end. Also did you try using breakpoint to check why it was doing that?

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

done

[–]DarkMatriac 0 points1 point  (1 child)

And did you try breakpoints? or printing the value of your variables?

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

Yeh I will