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");
    case(2) : printf("To edit, enter new contact's number");
    case(3) : printf("Who do you wish to call");
    case(4) : printf("Who do you want to text");
    case(5) : exit(1);
    default: printf("\n%d is not a valid choice!\n", choice);

Source code:

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

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


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).

Need Your Help

Changing between user defined classes at runtime in C++

c++ pointers casting user-defined-types

I have two classes with one extending the other. They both have a method called doSomething() that perform something different. I want to be able to have one pointer that I can switch from class A to

Visual Studio source window and Bootcamp keyboard mapping

visual-studio keyboard-shortcuts macbook remap bootcamp

So I recently bought myself a MacBook Air and I just installed Visual Studio 2012 in bootcamp. The problem now is that the key } is missing from the keyboard layout for some reason. The { key is ma...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.