do { } while (a!="quit"); not working

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// do while sentinel
#include <iostream>
#include <string>

using namespace std;


int n;
string a;


void acrostic ()

{do {
	if (a=="a")
		cout << "Apple" << endl;
	else if (a=="b")
		cout << "Banana" << endl;
	else if (a=="c")
		cout << "Cherry" << endl;
	else if (a=="d")
		cout << "Did" << endl;
	else if (a=="e")
		cout << "Every" << endl;
	else if (a=="f")
		cout << "Fat" << endl;
	else if (a=="g")
		cout << "Glowing" << endl;
	else if (a=="h")
		cout << "House" << endl;
	else if (a=="i")
		cout << "Igloo" << endl;
	else if (a=="j")
		cout << "Jumper" << endl;
	else if (a=="k")
		cout << "Keys" << endl;
	else if (a=="l")
		cout << "Lift" << endl;
	else if (a=="m")
		cout << "Mouse" << endl;
	else if (a=="n")
		cout << "Night" << endl;
	else if (a=="o")
		cout << "Over" << endl;
	else if (a=="p")
		cout << "Pet" << endl;
	else if (a=="q")
		cout << "Quarry" << endl;
	else if (a=="r")
		cout << "Red" << endl;
	else if (a=="s")
		cout << "Sound" << endl;
	else if (a=="t")
		cout << "Train" << endl;
	else if (a=="u")
		cout << "Umbrella" << endl;
	else if (a=="v")
		cout << "Vroom" << endl;
	else if (a=="w")
		cout << "Water" << endl;
	else if (a=="x")
		cout << "Xylophone" << endl;
	else if (a=="y")
		cout << "Yellow" << endl;
	else if (a=="z")
		cout << "Zebra" << endl;
	else
		cout << "Invalid letter!" << endl;
} while (a!="quit");
	}
int main ()
{
	for (int n=1; n>0; n++) {
		cout << "Please enter letter # " << n << " of your name" << endl;
		cin >> a; 
		acrostic();
	}
	
	return 0;
}


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?

Thanks!
You are using a loop that doesn't change the value of a, so naturally it either loops once or forever.
Now would be a great time to learn about switches http://www.cplusplus.com/doc/tutorial/control/#switch

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.
Last edited on
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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// do while sentinel
#include <iostream>
#include <string>

using namespace std;


int n;
string a;


void acrostic ()
{
do 
{
	switch (a)
	{
	    case "a":
            cout << "Apple" << endl;
            break;
        case "b:"
		cout << "Banana" << endl;
		break;
	/*
        the rest
    */
	case "z":
                cout << "Zebra" << endl;
                break;
         case "quit":   //This prevents the default from triggering, so quit won't say "invalid letter!"
                break;   //the while loop would not need this                
        default:
		cout << "Invalid letter!" << endl;
		cin >> a; // This is vital, otherwise it will keep looping forever
    }
}while (a!="quit");

void acrostic2 ()
{
while (a!="quit") 
{
	switch (a)
	{
	    case "a":
            cout << "Apple" << endl;
            break;
        case "b:"
		cout << "Banana" << endl;
		break;
	/*
        the rest
    */
		case "z":
            cout << "Zebra" << endl;
            break;
        default:
		cout << "Invalid letter!" << endl;
		cin >> a; // This is vital, otherwise it will keep looping forever
    }
};


The second one, if the user enters quit, will simply skip the entire while statement. But the most important part is having the cin >> a;
This gives me three errors:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// do while sentinel
#include <iostream>
#include <string>

using namespace 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!
Last edited on
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <map>
#include <string>
#include <iostream>

void acrostic(const std::string &name)
{
  static std::map <char, std::string> conversion;
  //std::map <char, std::string>::iterator it;
  //std::pair <std::map <char, std::string>::iterator, bool> err_catch;
  if ( conversion.empty() )
  {
    // err_catch =

    conversion.insert( std::pair <char, std::string> ('a', "apple") );
    conversion.insert( std::pair <char, std::string> ('b', "bunny") );
    // More here
    conversion.insert( std::pair <char, std::string> ('z', "zebra") );

    // if (err_catch.second == false)
    // Do some error handling.
  }

  for (int i = 0; i < name.length(); ++i)
    if ( isalpha(name[i]) )
      std::cout << conversion[ name[i] ] << " ";
}

int main()
{
  std::string name;
  std::cout << "Enter your name.\n >"; std::cin >> name;

  acrostic(name);

  return 0;
}

Last edited on
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.
}
Last edited on
conversion.insert( std::pair <char, std::string> ('a', "apple") );
While correct, it should rather be:
conversion['a']="apple";
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.
Last edited on
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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// do while sentinel
#include <iostream>

using namespace std;


int n;
char a;

void exit (int exitcode);

void acrostic ()
{
	if (a=='a')
		cout << "Apple" << endl;
	else if (a=='b')
		cout << "Banana" << endl;
	else if (a=='c')
		cout << "Cherry" << endl;
	else if (a=='d')
		cout << "Did" << endl;
	else if (a=='e')
		cout << "Every" << endl;
	else if (a=='f')
		cout << "Fat" << endl;
	else if (a=='g')
		cout << "Glowing" << endl;
	else if (a=='h')
		cout << "House" << endl;
	else if (a=='i')
		cout << "Igloo" << endl;
	else if (a=='j')
		cout << "Jumper" << endl;
	else if (a=='k')
		cout << "Keys" << endl;
	else if (a=='l')
		cout << "Lift" << endl;
	else if (a=='m')
		cout << "Mouse" << endl;
	else if (a=='n')
		cout << "Night" << endl;
	else if (a=='o')
		cout << "Over" << endl;
	else if (a=='p')
		cout << "Pet" << endl;
	else if (a=='r')
		cout << "Red" << endl;
	else if (a=='s')
		cout << "Sound" << endl;
	else if (a=='t')
		cout << "Train" << endl;
	else if (a=='u')
		cout << "Umbrella" << endl;
	else if (a=='v')
		cout << "Vroom" << endl;
	else if (a=='w')
		cout << "Water" << endl;
	else if (a=='x')
		cout << "Xylophone" << endl;
	else if (a=='y')
		cout << "Yellow" << endl;
	else if (a=='z')
		cout << "Zebra" << endl;
	else if (a=='q')
		exit('q');
}
int main ()

{
	for (int n=1; n>0; n++) {
		cout << "Please enter letter # " << n << " of your name" << endl;
			cin >> a;
			acrostic();
	}
	
	return 0;
}
But what if my name is Dominique?
There are much better ways of doing this. Do you know how to use strings or switch statements,
and what if my name were Quincy?

Edit: And there's another isue. Some people will tend to capitalize their names. You only acount for lowercase.
Last edited on
Topic archived. No new replies allowed.