Hello, first time posting here. This code works fine until I try to add a do-while loop to it. I get an infinite loop when anything is entered with the do-while loop in place, and "quit" is strangely the only input that doesn't cause an infinite loop, but nonetheless, it does not induce a logout. I even tried wrapping it around the acrostic function call in int main(). I tried looking this up in other threads and found some people use variables as sentinels. What is the correct placement of the do-while?
Also, a == "quit" then that loop will still output invalid letter. I don't know if that's intended or not, but I suggest a while loop, so that if a does equal quit, then you won't trigger the default statement.
Thanks for your quick responses. I will review switches, but my goal here is to create a specific input that will exit the function in any fashion. Zhuge, are you saying it it is not possible to use a in the do-while loop conditions because it is not part of an equation? How can I make a term like "quit" exit the if else loop?
// do while sentinel
#include <iostream>
#include <string>
usingnamespace std;
int n;
string a;
void acrostic ()
{
while (a!="quit")
{
switch (a) // Switch quantity not an integer
{
case"a":
cout << "Apple" << endl;
break;
case"b":
cout << "Banana" << endl;
break;
case"z":
cout << "Zebra" << endl;
break;
default:
cout << "Invalid letter!" << endl;
cin >> a;
}
};
int main ()
{ // A function-definition is not allowed here before '{' token...WHAT FUNCTION-DEFINITION??
for (int n=1; n>0; n++) {
cout << "Please enter letter # " << n << " of your name" << endl;
cin >> a;
acrostic();
}
return 0;
} // Expected '}' at the end of input...even when I append an extra brace
Sorry, still giving me trouble :S If it makes you feel better, this isn't for a class, I'm just trying to teach myself!
I'm really lost here. Why are you using an std::string for just a single char? Also why do you not just get the entire name at once and go letter by letter. Run into a unknown letter? Skip it.
Also. All of your couts, if statements, and switches can be simplified by using a std::map.
Hey thanks, I have only read up to the "Pointers" section in the tutorial and wanted to test my knowledge on input, control structures and functions. I guess I'll just keep reading as I'm obviously missing a lot of information.
I'm sorry. I wrote that when I should have been asleep so it comes of rather harsh.
What I recommend for a better loop is a while loop that breaks when the user enters "quit"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
while (true) // this begins an infinite loop. We can control it by using break statements or returns.
{
cin >> a;
if (a == "quit" || a == "")
break; // This break should not be inside {}.
// break is a control statement that gets you out of the current block
// (a block is defined by {}) it encounters.
// We want to leave the while which is the current block
// Now make a second internal loop.
for (unsigned i = 0; i < a.length(); ++i)
{
switch( a[i] ) // You can now use a in the switch as chars
{
case'a':
// insert cases here.
}
}
//You can move this for loop and all of its contents to the function as you had before.
}
Just to clarify, you can only use a switch statement on an integral type1--not a string. A char is works in switches but can only contain one character.
Your options are: stick with the if/else if/else statements, use the map-based dispatching approach (mentioned above), or a hybrid like the if and switch (also above).
1 Or any type that may be implicitly converted to an integral type.
Well it isn't pretty, but it works! I think all I was looking for was the exit function, instead of specifying some condition that would have to be met via input. All of your input on this question is appreciated 1000-fold!