Creating a loop in C++ (still learning)

while loop in c
nested for loop in c
for loop c programming practice
do while loop in c
for loop in c++
for loop example
for loop syntax
if statement in c

new here, trying to figure out how to repeat my program. I need to understand how to insert a loop, i think a "do while" loop will work for this, but I am unsure because I have tried a few places of insertion and cannot get it to work right.

So my program is a telephone program, I am sure everyone here has done this in school, I am learning to do this and this is the part that I am confused on. My code is below.

I just need to make it possible for the user to keep entering phone numbers, over and over again.

I feel like I should be inserting a "do" before line14 "for (counter = 0... Then insert the "while" portion at line 94 between the brackets. For some reason, that doesn't work for me and I am now stumped.

NOTE This is an assignment for school, so please explain to me rather than just show me. Thanks for everyones help.

#include <iostream> 

using namespace std; 


int main() {

    int counter;

    char phoneNumber;

    cout << "\nEnter a phone number in letters only." << endl;

    for (counter = 0; counter < 7; counter++)
    {
        cin >> phoneNumber;

        if (counter == 3)
            cout << "-";

        if (phoneNumber >= 'A' && phoneNumber <= 'Z'
            || phoneNumber >= 'a' && phoneNumber <= 'z')
            switch (phoneNumber)
            {
                case 'A':
                case 'a':
                case 'B':
                case 'b':
                case 'C':
                case 'c':
                    cout << 2; // keypad starts with 2 for letters ABC, abc
                    break;
                case 'D':
                case 'd':
                case 'E':
                case 'e':
                case 'F':
                case 'f':
                    cout << 3; //for letter DEF, def
                    break;
                case 'G':
                case 'g':
                case 'H':
                case 'h':
                case 'I':
                case 'i':
                    cout << 4; //for letters GHI, ghi
                    break;
                case 'J':
                case 'j':
                case 'K':
                case 'k':
                case 'L':
                case 'l':
                    cout << 5; //for letter JKL, jkl
                    break;
                case 'M':
                case 'm':
                case 'N':
                case 'n':
                case 'O':
                case 'o':
                    cout << 6; //for letters MNO, mno
                    break;
                case 'P':
                case 'p':
                case 'Q':
                case 'q':
                case 'R':
                case 'r':
                case 'S':
                case 's':
                    cout << 7; //for letters PQRS, pqrs
                    break;
                case 'T':
                case 't':
                case 'U':
                case 'u':
                case 'V':
                case 'v':
                    cout << 8; //for letters TUV, tuv
                    break;
                case 'W':
                case 'w':
                case 'X':
                case 'x':
                case 'Y':
                case 'y':
                case 'Z':
                case 'z':
                    cout << 9; //for letters WXYZ, wxyz
                    break;
            }

    }
    return 0;
}

As already said by pb772 an infinite loop of type

do { //Stuff you'd like to do } while(1);

would be fine, especially since it's a school assignment, but not ideal as always stated by pb772. I've seen advises to cycle a finite number of times and then exit but I would instead do something like a special character like '#' or '!' that will trigger a condition to exit the loop. I would see this like an exit/escape character. In the end is up to you, if you want you can do anything and what I'm proposing is just an idea to inspire you. For example another idea would be, if you'd like to go deeper, wait for another input to define what action to perform, you trigger the "command console" with '!' and then type 'q' to exit or also read the characters into a string at first so you could do complex "commands" like "!q".

Here's the simple version:

bool loop_condition = true;
do
{
   if(input == '!')
   {
      loop_condition = false;
   }
   else
   {
      //Stuff you'd like to do if the read character is not !
   }while(loop_condition == true);

Just to provide context here is what's happening:

  1. I declare a variable named loop_condition
  2. Inside the loop I check if the typed character is !
  3. If so set the variable loop_condition to false with subsequent exit from the loop
  4. Else just execute your code and loop

As I already said this is a very simple example just to give you an idea and can be improved a lot.

for loop in C, for loop in C. The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. Next, the condition is evaluated. After the body of the 'for' loop executes, the flow of control jumps back up to the increment statement. The condition is now evaluated again. A loop statement allows us to execute a statement or group of statements multiple times and following is the general from of a loop statement in most of the programming languages − C++ programming language provides the following type of loops to handle looping requirements.

I suggest wrapping the for (counter=0... loop with a while (!cin.eof()) { block. This will allow the user to continue to enter in characters, until an EOF character (e.g. ctrl-D).

You may find you want to output a newline after every 7th character, to make the display look nice.

C - for loop in C programming with example, For loop in C programming with example: A loop is used in a programming to execute set of statements repeatedly until a given condition returns false. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times. Syntax. The syntax of a for loop in C programming language is − for ( init; condition; increment ) { statement(s); } Here is the flow of control in a 'for' loop − The init step is executed first, and

do {
    //your code here;
} while (1);

This will repeatly infinitely which is not a good practice.

int number_of_phones = 10; // total number of phones you want
int i = 0;
do {
    //your code here;
    i=i+1;
} while (i<number_of_phones);

This will make it run 10 times for example

Loops in C and C++, is a sequence of instructions that is repeated until a certain condition is reached. An operation is done, such as getting an item of data and changing it, and then some condition is checked such as whether a counter has reached a prescribed number. Beware the endless loop! When a C program enters an endless loop, it either spews output over and over without end or it sits there tight and does nothing. Well, it’s doing what you ordered it to do, which is to sit and spin forever. Sometimes, this setup is done on purpose, but mostly it …

You can have whatever condition you want in a for loop, including nothing at all, which is treated as true.

for(;;) {
    // code
}

is the same as

while (true) {
    // code
}

is the same as

do {
    // code
} while (true)

It sounds like you mixed up the placement of braces when you tried do { ... } while (true). You may want to move your big switch into a function, so that it's more obvious what scope a partiular } ends.

#include <iostream> 

int phone_key(char key)
{
    switch (key)
    {
    case 'A':
    case 'a':
    case 'B':
    case 'b':
    case 'C':
    case 'c':
        return 2;
    case 'D':
    case 'd':
    case 'E':
    case 'e':
    case 'F':
    case 'f':
        return 3; 
    case 'G':
    case 'g':
    case 'H':
    case 'h':
    case 'I':
    case 'i':
        return 4; 
    case 'J':
    case 'j':
    case 'K':
    case 'k':
    case 'L':
    case 'l':
        return 5; 
    case 'M':
    case 'm':
    case 'N':
    case 'n':
    case 'O':
    case 'o':
        return 6; 
    case 'P':
    case 'p':
    case 'Q':
    case 'q':
    case 'R':
    case 'r':
    case 'S':
    case 's':
        return 7;
    case 'T':
    case 't':
    case 'U':
    case 'u':
    case 'V':
    case 'v':
        return 8; 
    case 'W':
    case 'w':
    case 'X':
    case 'x':
    case 'Y':
    case 'y':
    case 'Z':
    case 'z':
        return 9; 
    }

    return 0;
}

int main() {
    for (;;)
    {
        std::cout << "\nEnter a phone number in letters only." << std::endl;

        for (int counter = 0; counter < 7; counter++)
        {
            char phoneNumber;
            cin >> phoneNumber;

            if (counter == 3)
                std::cout << "-";
            std::cout << phone_key(phoneNumber);
        }
    }
}

Visual Basic/Loops, In Loop, the statement needs to be written only once and the loop will be executed 10 times as shown below. In computer programming, a loop is a sequence of  If you are executing a loop and hit a continue statement, the loop will stop its current iteration, update itself (in the case of for loops) and begin to execute again from the top. Essentially, the continue statement is saying "this iteration of the loop is done, let's continue with the loop without executing whatever code comes after me."

C for loop, A "For" Loop is used to repeat a specific block of code (statements) a known number of times. The for-loop statement is a very specialized while  The nested loops should be adequately indented to make code readable. In some versions of 'C,' the nesting is limited up to 15 loops, but some provide more. The nested loops are mostly used in array applications which we will see in further tutorials.

How to Construct a Basic FOR Loop in the C Language, The C language gives you many ways to create loops in your code, but the most common is the for loop. A for loop has three parts: The setup. The exit condition  A loop statement allows us to execute a statement or group of statements multiple times. Given below is the general form of a loop statement in most of the programming languages − C programming language provides the following types of loops to handle looping requirements.

How to Create Endless Loops in C Programming, When a C program enters an endless loop, it either spews output over and over without end or it sits there tight and does nothing. Well, it's doing what you ordered  A loop statement allows us to execute a statement or a group of statements multiple times and following is the general from of a loop statement in most of the programming languages − C# provides following types of loop to handle looping requirements.

Comments
  • Converting phoneNumber to lower or upper case before the if and the switch will reduce a lot of the work you have to do testing both cases. There is also a library function to tell if a character is a letter. This is much safer because nothing guarantees that the character set will be organized in any sane fashion and there are no surprises inserted between 'a' and 'z'.
  • Some things I would do differently: Read into a string stream. Convert to lower/upper case to avoid comparing against both character sets. Iterate the length of the actual user input, not assuming phone numbers are a certain length. Consider reading github.com/googlei18n/libphonenumber/blob/master/FALSEHOODS.md as well to understand edge cases. For example, what if I used Japanese or Arabic letters, how would you map those?
  • stackoverflow.com/questions/5431941/…
  • This does solve my problem but presents another one. It carries any extra numbers entered as you start over and it should disregard them and just start over completely. Thanks for your help and explanation!