Any suggestions or improvements?

Can I use the for loop to replace the do...while loop in this program? Any suggestion to make this program more simple? This program ask the user to enter their option from 1 to 5 (if choice is another number the program will terminate) My code:

int choice;
    char option;
    printf("You want to know what I'm thinking about you?\n");
    printf("1. Add New Contact\n");
    printf("2. Edit Existing Contact\n");
    printf("3. Call Contact\n");
    printf("4. Text Contact\n");
    printf("5. Exit\n");

for (int i = 0; i < 5; i++)
{
    printf("Please choose the above option:\n");
    scanf(" %d", &choice);
    switch (choice)
    {
    case(1) : printf("To add new contact, you need the contact's: First name, Last name and Number");
        break;
    case(2) : printf("To edit, enter new contact's number");
        break;
    case(3) : printf("Who do you wish to call");
        break;
    case(4) : printf("Who do you want to text");
        break;
    case(5) : exit(1);
    default: printf("\n%d is not a valid choice!\n", choice);
        break;

Source code:

do
{
printf("Enter your choice: ");
scanf(" %d", &choice);
switch (choice)
{
case (1): printf("\nTo add you will need the ");
printf("contact's\n");
printf("First name, last name, and number.\n");
break;

case (2): printf("\nGet ready to enter the name of ");
printf("name of the\n");
printf("contact you wish to change.\n");
break;
case (3): printf("\nWhich contact do you ");
printf("wish to call?\n");
break;
case (4): printf("\nWhich contact do you ");
printf("wish to text?\n");
break;
case (5): exit(1); //Exits the program early
default: printf("\n%d is not a valid choice.\n", choice);
printf("Try again.\n");
break;
}
} while ((choice < 1) || (choice > 5));
return 0;

Answers


I don't think for this problem replace the "do...while" loop with the "for" loop is well. In the first code (for loop) if the user choose a correct answer the program make 5 times the same operation. Insted in the "do while" the loop stop just when the answer is correct (chose is = 1...2....3...4..or 5). I think that the second code is right regards


Well, in theory you could, but it wouldn't make any sense. As a rule of thumb:

  • Use for when the number of iterations are known. (Usually considered easiest to read and usually produces the most effective code.)
  • Use while when the number of iterations are unknown.
  • Use do while when the number of iterations are unknown, but you must put the loop condition after the first lap in the loop (for example because you need to evaluate user output).

I'm afraid, no. As the code is currently written, the major behavioral difference, unless the input is 5,

  • The for loop version is guranteed to execute 5 times,
  • The do...while version depends on the input value for each iteration. for any value less than 1 or greater than 5 will terminate the loop. Otherwise, it will continue looping.

Need Your Help

Deploying VBA project that uses ADO objects

excel vba deployment ado

In an Excel VBA project, I need to read and write UTF-8-encoded text files. After reading this and this SO topic, I am considering using ADO stream objects to accomplish this.

displaying html in div or iframe

javascript jquery html iframe

I want to display my html string in this iFrame.