I'm trying to make an template which is my class assignment.
I used a switch statement in the do while loop while the condition is if enter variable not equal to 16 you should terminate the program
I'vve already used break statment in every case but it isn't working
#include <iostream>
using namespace std;
int main()
{
int op;
cout << "Please enter choice between 1 to 15 For Different Operations "
<< endl;
cout << "1. Create List(create a new list(presumably empty))" << endl;
cout << "2. Insertion" << endl;
cout << "3. Deletion" << endl;
cout << "4. Update(replace the element)" << endl;
cout << "5. Start" << endl;
cout << "6. Next" << endl;
cout << "7. Back" << endl;
cout << "8. Tail" << endl;
cout << "9. Find" << endl;
cout << "10. Copy" << endl;
cout << "11. Get(display current index and element)" << endl;
cout << "12. Size / length" << endl;
cout << "13. Display list" << endl;
cout << "14. De - allocate list" << endl;
cout << "15. Exit" << endl;
cin >> op;
do {
switch (op) {
case 1:
cout << "Creating the List" << endl;
break;
case 2:
cout << "inserting... " << endl;
break;
case 3:
cout << "Deleting... " << endl;
break;
case 4:
cout << "updating... " << endl;
break;
case 5:
cout << "starting... " << endl;
break;
case 6:
cout << "next... " << endl;
break;
case 7:
cout << "back... " << endl;
break;
case 8:
cout << "Tail... " << endl;
break;
case 9:
cout << "Find... " << endl;
break;
case 10:
cout << "Copying... " << endl;
break;
case 11:
cout << "Getting... " << endl;
break;
case 12:
cout << "Size... " << endl;
break;
case 13:
cout << "Display... " << endl;
break;
case 14:
cout << "Deallocate... " << endl;
break;
case 15:
terminate;
break;
default:
cout << "Please enter the correct choice " << endl;
break;
}
} while (op != 16);
return 0;
}
i expect the the cout output in every choice user made and then stop for taking input
from your original code
cin >> op;
do {
switch (op)
so you read from cin outside the loop and never read again, reordering so you read at every loop pass will ask you again the desired operation
do {
cin >> op;
switch (op)
Additionally note that break does not break the loop it just breaks from the switch. Without the break the instructions from the next case will be executed as well in the switch.
If you want to break from the loop under some conditions you need to add a break outside of the switch statement, you can use a variable to "remember" if you should leave the loop.
However since you have an exit condition in your typed instructions why not make the loop exit
} while (op != 15);
which seems to make sense because of the
cout << "15. Exit" << endl;
line.
The code below contains a table I created numbering from 1 to 10 in reverse order. (This is the code without breaks, later changed.)
#include <iostream>
using namespace std;
int main()
{
switch(1) {
case 1: cout << "1. blue" << endl;
case 2: cout << "2. orange.." << endl;
case 3: cout << "3. yellow.." << endl;
case 4: cout << "4. green.." << endl;
case 5: cout << "5. purple.." << endl;
case 6: cout << "6. red.." << endl;
case 7: cout << "7. teal.." << endl;
case 8: cout << "8. aqua.." << endl;
case 9: cout << "9. white.." << endl;
case 10: cout << "10. gray.." << endl;
}
}
I am trying to make it so that each time the loop runs, the program will output the message corresponding to the next lower number, one by one. (Code with breaks)
#include <iostream>
using namespace std;
int main() {
for(int i = 10; i>0; --i){
switch(i) {
case 1: cout << "1. blue" << endl;
break;
case 2: cout << "2. orange.." << endl;
break;
case 3: cout << "3. yellow.." << endl;
break;
case 4: cout << "4. green.." << endl;
break;
case 5: cout << "5. purple.." << endl;
break;
case 6: cout << "6. red.." << endl;
break;
case 7: cout << "7. teal.." << endl;
break;
case 8: cout << "8. aqua.." << endl;
break;
case 9: cout << "9. white.." << endl;
break;
case 10: cout << "10. gray.." << endl;
break;
}
}
}
I am having trouble looping down the list, and listing each number/color one by one.
At the end of listing each item, I would like the program to ask the user if they would like to Stop, or Restart the loop.
You need to create a variable to hold the variable that will be passed into your switch statement. Your switch statement needs to start from 10 and go down because you want to print case 10 first.
#include <iostream>
using namespace std;
int main() {
for(int i = 10; i>0; --i){
switch(i) {
case 1: cout << "1. blue" << endl;
break;
case 2: cout << "2. orange.." << endl;
break;
case 3: cout << "3. yellow.." << endl;
break;
case 4: cout << "4. green.." << endl;
break;
case 5: cout << "5. purple.." << endl;
break;
case 6: cout << "6. red.." << endl;
break;
case 7: cout << "7. teal.." << endl;
break;
case 8: cout << "8. aqua.." << endl;
break;
case 9: cout << "9. white.." << endl;
break;
case 10: cout << "10. gray.." << endl;
break;
}
}
}
It was a bit confusing how you were explaining before, I think i got it now.
As said on the comments switch works but probably isn't the best way, I will show you how I would do it, if you really want it done with switch just say in the comments and I will update my answer.
I think what you really want is to print one option and after ask if the user wants to stop or start from the beginning, if not just continue (Just comment if it isn't this).
#include <iostream>
#include <array>
using namespace std;
int main()
{
array<string, 10> colors {"1. blue", "2. orange..", "3. yellow..", "4. green..", "5. purple..", "6. red..", "7. teal..", "8. aqua..", "9. white..", "10. gray.."};
for(int i = colors.size() - 1; i >= 0; --i)
{
cout << colors[i] << endl;
cout << endl << "Would you like to stop[s/S] or restart[r/R]?: ";
char input;
if (cin >> input)
{
if (tolower(input) == 's')
break;
else if (tolower(input) == 'r')
i = colors.size(); // To restart should be one more than the last index, it's going to be decreased at the end of loop
}
cout << endl;
}
}
So basically the code has two parts, first the printing which I changed the switch statement to and array.
array<string, 10> colors {"1. blue", "2. orange..", "3. yellow..", "4. green..", "5. purple..", "6. red..", "7. teal..", "8. aqua..", "9. white..", "10. gray.."};
for(int i = colors.size() - 1; i >= 0; --i)
{
cout << colors[i] << endl;
}
So this way you could even add more to the array and the code would still work, just starts at the end of the array and prints one by one.
The second part is were it asks the user if he wants to stop or restart.
char input;
if (cin >> input)
{
if (tolower(input) == 's')
break;
else if (tolower(input) == 'r')
i = colors.size(); // To restart should be one more than the last index, it's going to be decreased at the end of loop
}
If the user types s just breaks out of the loop, if he types r resets the i variable to the last index of the array (although the array is indexed at 0, you can't put size() - 1 because at the end of the loop the variable i is decreased).
As requested here is the switch version
#include <iostream>
using namespace std;
int main()
{
while (true)
{
for(int i = 10; i>0; --i)
{
switch(i)
{
case 1: cout << "1. blue" << endl;
break;
case 2: cout << "2. orange.." << endl;
break;
case 3: cout << "3. yellow.." << endl;
break;
case 4: cout << "4. green.." << endl;
break;
case 5: cout << "5. purple.." << endl;
break;
case 6: cout << "6. red.." << endl;
break;
case 7: cout << "7. teal.." << endl;
break;
case 8: cout << "8. aqua.." << endl;
break;
case 9: cout << "9. white.." << endl;
break;
case 10: cout << "10. gray.." << endl;
break;
}
}
cout << endl << "Would you like to stop[s/S] or restart[r/R]?: ";
char input;
if (cin >> input && tolower(input) != 'r')
break;
cout << endl;
}
}
I want to suggest a different approach to solve your problem.
Think of what you are trying to do and use one or more function calls to do that from main instead of having the code inside main. You core functionality is to print a color given an integer value.
From main, that would look like:
void printColor(int i);
int main()
{
for (int i = 10; i>0; --i)
{
printColor(i);
}
}
In the implementation of printColor, you would use:
std::string getColor(int i);
void printColor(int i)
{
std::cout << i << ". " << getColor(i) << std::endl;
}
You can implement getColor as:
std::string getColor(int i)
{
switch (i)
{
case 1:
return "blue";
case 2:
return "orange";
...
}
// If value is different than the known values in the switch statement,
// we have an unknown color
return "Unknown";
}
For repeating the core function in main, you can use a while loop.
Here's everything put together.
#include <iostream>
#include <string>
void printColor(int i);
std::string getColor(int i);
bool getRepeat();
int main()
{
bool repeat = true;
while ( repeat )
{
for (int i = 10; i>0; --i)
{
printColor(i);
}
repeat = getRepeat();
}
}
void printColor(int i)
{
std::cout << i << ". " << getColor(i) << std::endl;
}
std::string getColor(int i)
{
switch (i)
{
case 1:
return "blue";
case 2:
return "orange";
case 3:
return "yellow";
case 4:
return "green";
case 5:
return "purple";
case 6:
return "red";
case 7:
return "teal";
case 8:
return "aqua";
case 9:
return "white";
case 10:
return "gray";
}
// If value is different than the known values in the switch statement,
// we have an unknown color
return "Unknown";
}
bool getRepeat()
{
std::cout << "Stop or Continue (S/C)? ";
char c;
std::cin >> c;
return (c == 'c' || c == 'C');
}
cin is really difficult for me to understand with c++.
I want to choose an item on the menu but I want it to be clean, however I am running into issues with cin.fail.
I realize cin.fail checks the datatype so I should be clear when it comes to other data types.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int number;// define an integer variable called number
float cost;// a floating point variable called cost,
char beverage;// and a character variable called beverage
bool validBeverage;
cout << fixed << showpoint << setprecision(2);
do
{
cout << endl << endl;
cout << "Hot Beverage Menu" << endl << endl;
cout << "A: Coffee $1.00" << endl;
cout << "B: Tea $ .75" << endl;
cout << "C: Hot Chocolate $1.25" << endl;
cout << "D: Cappuccino $2.50" << endl <<endl << endl;
cout << "Enter the beverage A,B,C, or D you desire" << endl;
cout << "Enter E to exit the program" << endl << endl;
do
{
cin >> beverage;// Fill in the code to read in beverage
if (cin.fail)
{
cout << "You have entered an invalid value." << endl
<< "Please enter the letter corresponding to the menu." << endl;
}
} while (cin.fail);
switch(beverage)
{
case 'a': validBeverage = true;
case 'A': validBeverage = true;
case 'b': validBeverage = true;
case 'B': validBeverage = true;
case 'c': validBeverage = true;
case 'C': validBeverage = true;
case 'd': validBeverage = true;
case 'D': validBeverage = true;
break;
default: validBeverage = false;
}// end switch beverage
if (validBeverage == true)
{
do
{
cout << "How many cups would you like?" << endl;
// Fill in the code to read in number
cin >> number;
if (cin.fail() || number <= 0)
{
cout << "You have entered an invalid value." << endl <<
"Please enter an integer greater than 0." << endl;`enter code here`
}
} while (cin.fail() || number <= 0);
}
switch (beverage)// Fill in the code to begin a switch
statement that is controlled by beverage
{
case 'a': (float)cost = number * 1.0;
cout << "The total cost is $ " << cost << endl;
break;
case 'A': (float)cost = number * 1.0;
cout << "The total cost is $ " << cost << endl;
break;
case 'b': (float)cost = number * 0.75;// Fill in the
code to give the case for tea ( $0.75 a cup)
cout << "The total cost is $ " << cost << endl;
break;
case 'B': (float) cost = number * 0.75;// Fill in the
code to give the case for hot chocolate ($1.25 a cup)
cout << "The total cost is $ " << cost << endl;
break;
case 'c': (float)cost = number * 1.25;
cout << "The total cost is $ " << cost << endl;
break;
case 'C': (float)cost = number * 1.25;
cout << "The total cost is $ " << cost << endl;
break;
case 'd': (float)cost = number * 2.50;// Fill in the
code to give the case for cappuccino ($2.50 a cup)
cout << "The total cost is $ " << cost << endl;
break;
case 'D': (float)cost = number * 2.50;// Fill in the
code to give the case for cappuccino ($2.50 a cup)
cout << "The total cost is $ " << cost << endl;
break;
case 'e':
cout << "Please come again." << endl;
break;
case 'E':
cout << " Please come again." << endl;
break;
default:cout << "Invalid Selection."; // Fill in the
code to write a message indicating an invalid selection.
cout << " Try again please" << endl;
}
}while (beverage != 'e' || beverage != 'E'); // Fill in the code to
finish the do-while statement with the condition that beverage does not
equal E or e.
// Fill in the appropriate return statement
return 0;
}
Try using: if(!cin) which checks for data type on input.
I have an assignment where I have to have a menu which is somewhat working, it can't crash or exit if I type in the wrong input and it can't have an endless loop. Although my menu doesn't exits or crashes it goes in to an endless loop where if I type in anything but an integer. Here is my code.
void mainMenu()
{
int option;
cout << "\t\t\t***** Project: Algorithms *****\n\n\n";
cout << "Enter your selection.\n\n";
cout << "1\tSearches.\n";
cout << "2\tCalculations and negations.\n";
cout << "3\tCopying.\n";
cout << "4\tExit the program.\n";
cout << "Please enter the menu next to each option.\n> " << flush;
cin >> option;
switch (option)
{
case 1: cout << "Yes!";
system("cls");
searchMenu();
break;
case 2: cout << "Yes!";
system("cls");
Calc_NegateMenu();
break;
case 3: cout << "Yes!";
system("cls");
copyMenu();
break;
case 4: cout << "Yes!";
exit(0);
break;
default: cout << "ERROR! Invalid input!";
system("cls");
mainMenu();
break;
}
}
The other menus.
void searchMenu()
{
int option;
cout << "\t\t\t***** Search *****\n\n\n";
cout << "Enter your selection.\n\n";
cout << "1\tSearch for a element with find.\n";
cout << "2\tSearch for an element with binary search.\n";
cout << "3\tSearch for pair elements.\n";
cout << "4\tBack to the main menu.\n";
cout << "Please enter the menu next to each option.\n> " << flush;
cin >> option;
switch (option)
{
case 1: cout << "Yes!";
system("cls");
searchMenu();
break;
case 2: cout << "Yes!";
system("cls");
Calc_NegateMenu();
break;
case 3: cout << "Yes!";
system("cls");
copyMenu();
break;
case 4: cout << "Yes!";
system("cls");
copyMenu();
break;
default: cout << "ERROR! Invalid input!";
system("cls");
mainMenu();
break;
}
}
void Calc_NegateMenu()
{
int option;
cout << "\t\t\t***** Calculate or Negate *****\n\n\n";
cout << "Enter your selection.\n\n";
cout << "1\tCalculate the total sum of all elements in the vector.\n";
cout << "2\tNegate all elements in the vector.\n";
cout << "3\tBack to the main menu.\n";
cout << "Please enter the menu next to each option.\n> " << flush;
cin >> option;
switch (option)
{
case 1: cout << "Yes!";
system("cls");
searchMenu();
break;
case 2: cout << "Yes!";
system("cls");
Calc_NegateMenu();
break;
case 3: cout << "Yes!";
system("cls");
mainMenu();
break;
default: cout << "ERROR! Invalid input!";
system("cls");
mainMenu();
break;
}
}
void copyMenu()
{
int option;
cout << "\t\t\t***** Copy *****\n\n\n";
cout << "Enter your selection.\n\n";
cout << "1\tCopy to list.\n";
cout << "2\tCopy to file.\n";
cout << "3\tBack to the main menu.\n";
cout << "Please enter the menu next to each option.\n> " << flush;
cin >> option;
switch (option)
{
case 1: cout << "Yes!";
system("cls");
searchMenu();
break;
case 2: cout << "Yes!";
system("cls");
Calc_NegateMenu();
break;
case 3: cout << "Yes!";
system("cls");
mainMenu();
break;
default: cout << "ERROR! Invalid input!";
system("cls");
mainMenu();
break;
}
}
When cin to an integer doesn't find one it sets an error flag and does not read from the input until the flag is cleared.
See this answer or this one.
Search on "cin infinite loop" and read the cin documentation.
I have always used if else statements over switch statements, but I decided I wanted to try a switch out. I did the basic Coke Machine program with a switch and I cannot for the life of me figure out why it does not work how it should. When I use a number 1-5 for my input it continues to give the the switch default error message instead of the case cout statements (such as "You chose Coke"). Obviously something must be wrong that I am not seeing/
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Beverage List" << endl;
cout << "Coke = 1" << endl;
cout << "Dr. Pepper = 2" << endl;
cout << "Water = 3" << endl;
cout << "Sprite = 4" << endl;
cout << "Lemonade = 5" << endl << endl << endl;
cout << "Enter a number to choose a beverage: ";
cin >> number;
switch (number)
{
case '1':
cout << "You chose Coke";
break;
case '2':
cout << "You chose Dr. Pepper";
break;
case '3':
cout << "You chose Water";
break;
case '4':
cout << "You chose Sprite";
break;
case '5':
cout << "You chose Lemonade";
break;
default:
cout << "Error: Choice was not valid. Here is your money back.";
}
cout << "\n";
system("pause");
return 0;
}
The character '1' is not the same as the number 1.
Change
case '1':
to
case 1:
It looks like your case statements are comparing chars, not integers:
case '5':
Try this instead:
case 5: