So this was an assignment I had to do for my Intro to Comp Science I, and the main objective was to write the functions that are seperated by each comment saying which it is.
My question is more about the void running() funciton I wrote. I wanted to have it execute all the functions above to make it a simple progrem runnings each. Using a case/switch and a while loop to do this. With cases 1,2,3 ONLY, it works as I expect. But the moment I add in the 4,5,6th cases it just cycles through them instead of returning back to the start of the while loop.
Can anyone help me understand what is going wrong?
/* For some reason the while loops case switches have an issues where they are cycling
thru the cases and not returning to the base while loops. IT was ok until I added cases 4,5,6*/
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
usingnamespace std;
//global space
/* Function for the first problem. Wholesale markup
*/
double retailPrice(int whole,double markup){
cout << markup << endl;
if(markup == 100){
whole = whole*2;
cout << "The retail price should be " << whole << " dollars. " << endl;}
else{
markup = markup / 100;
markup = 1 - markup;
markup = 1 + markup;
whole = whole * markup;
cout << "The retail price should be " << whole << " dollars. " << endl;}
}
/* Coin toss simulation. Question 2
*/
int cointoss(){
int coin;
srand (time(NULL));
int flip = (rand()%2)+ 1 ;
if(flip == 1){
cout << "You got heads."<<endl;}
else{
cout << "You got Tails. "<<endl;}
}
/* 3rd question. Check if a number is odd or even
*/
int evenCheck(int number){
if( number % 2 == 0){
cout << "That number is even.\n";}
else{
cout << "That number is odd.\n";}
}
/* 4th Question determine if a letter is a consonant or vowel.
*/
bool isVowel(char letter) {
if (letter == 'a' || letter == 'e' || letter == 'i' || letter =='o' || letter == 'u') {
returntrue;
}
returnfalse;
}
int countVowels(string str) {
int count = 0;
int pos = 0;
while (pos < str.length()) {
if (isVowel(str.at(pos))) {
count=count;
}
else{
++count;}
++pos;
}
cout << "There are " << count << "consonants."<<endl;}
/* 5th question
Grades */
int gradeChecker(int grade){
if (grade == 100) {
cout << "Above Average" << endl;
}
if (grade >= 90 && grade <=99) {
cout << "A" << endl;
}
if (grade >= 80 && grade <=89) {
cout << "B" << endl;
}
if (grade >= 70 && grade <=79) {
cout << "C" << endl;
}
if (grade >= 60 && grade <=69) {
cout << "D" << endl;
}
if (grade <= 59){
cout << "You failed" << endl;
}
}
void running(){
int run = 1;
int choice;
while(run == 1){
cout << "Which program would you like to run?\n" << "1: Markup Program\n" << "2: Coin Toss Simulator\n"<<"3: Is it even?\n"<<"4: Consonant Counter\n"<<"5:Grade check\n";
cin >> choice;
switch(choice){
int m;
int w;
case 1: cout << "What is the wholesale price? "<< endl;
cin >> w;
cout << "Markup %? "<< endl;
cin >> m;
retailPrice(w,m);
case 2: cout << "Flip the coin?\n"<<"1:yes | 2:no"<<endl;
int toss;
cin >> toss;
if(toss == 1){
cointoss();
}
case 3: cout << "Type a number."<<endl;
int userN;
cin >> userN;
evenCheck(userN);
case 4:{ cout <<"Type a word."<<endl; // not sure why the program
string word;// continues from case 4 to 5 on it's own
cin>>word;//followed by closing itself out even, meaning case 6 is being hit
countVowels(word);}
case 5: cout << "What was your grade(i.e 88)" << endl;
int input;
cin >> input;
gradeChecker(input);
case 6: run = 0;
}
}
}
int main()
{
running();
}
switches fall through until they see a break.
so
switch(x)
case 1:
blah;
case 2:
blah;
case 3:
blah;
break;
will do 1, 2 and 3 if you enter 1, 2 and 3 if you enter 2, and 3 if you enter 3. If you put a break on the 1 and 2 cases, it would only do the one you input.
general tips...
- this feature is the only time I use switches. You can't write the fall-through as efficiently with if statements for some logic. This is rare, in my experience. They are very limited (only integers, only exact match conditions) compared to if statements and this feature is the only thing it can do that if statements cannot.
- switches should always have a default: branch.
So each case needs to have a break added to it basically which would cause it to cycle back to the beginning of the While loop.
And yes, usually I don't use Case/Switches, but ti seemed like it was very appropriate for the task I was trying to do; At least for as far as my knowledge goes.
case 6: would expect 6 to be entered, but by the looks of it I think you're intending it to be the default statement that must be run when the user clicks on any other number.
For that you must use default://write code below this . This case will be run when none of the other cases are matched.
I assumed this because there wasn't an option 6 in your menu and you've put run = 0 which would stop your while loop ;p. So even if that menu were to have an option 6, how about instead telling the user to click on any other key to exit? Makes life simple eh?
So each case needs to have a break added to it basically which would cause it to cycle back to the beginning of the While loop.
yes, for your situation, this is what you need.
but you also need to understand when to not use the break, to exploit the power of switches and their unique ability.