Reputation: 1
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;
}
Upvotes: 0
Views: 98
Reputation: 62576
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);
}
}
}
Upvotes: 0
Reputation: 11
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:
As I already said this is a very simple example just to give you an idea and can be improved a lot.
Upvotes: 1
Reputation: 539
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
Upvotes: 0
Reputation: 4207
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.
Upvotes: 0