Programming "q"to quit

The program generates triangles continuously. I want when the user inputs the letter "q" that the program will terminate. How do you program this? And if the user enters a different letter it should display a message that an invalid character was inputted.

// Details: Uses loops to generate a triangle out of asteriks as per the user input

#include <iostream>
#include <cstdlib>
#include <sstream>

using namespace std;

int main ()
{
int height;

cout << "Enter height of triangle or \'q\' to quit" << endl;

for (int j=0; ; j++) // Repeats loop to infinity
{cin >> height;
cout << endl;

if (height < 0)
cout << "Height must be positive" << endl;

char star='*';
for (int i=height; i>=1; i--) // Represents the Row
{
if (i!=0)
{
for (int space=1; space<=i; space++) //Inserts spaces to align to left side
{
cout << " ";
}
}
for(int length=height-i ;length>=0; length--) //Inserts stars in columns
{
cout << star;
}
cout << endl;
}
cout << endl;
}

return 0;
}
I don't understand, you want the user to give input while you are outputting at the top of your lungs?
You enter a number and it outputs a triangle of rows with that number. You then enter a new number and it outputs another triangle. This will continue to happen. I want the program to terminate when the user inputs the character "q".
Replace these lines:
15
16
for (int j=0; ; j++) // Repeats loop to infinity
{cin >> height;
With these lines:
15
16
while(cin >> height)
{
And try it out. Note that anything which is not a number will quit, not just q.
I want only the 'q' key to quit. Any other letter should give a message saying that an invalid input was entered.
Here's one way you might do it.

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
#include <iostream>
#include <string>

using namespace std;

int main()
{
    for (;;)    // infinte loop.
    {
        cout << "Enter height of triangle or 'q' to quit" << endl;

        string line;
        getline(cin, line);

        if (line.size() == 1 && line[0] == 'q')
            break;

        int height;
        try { height = std::stoi(line); }
        catch (std::exception&)
        {
            std::cout << "Invalid input.\n";
            continue;
        }

        if (height < 0)
        {
            cout << "Height must be positive" << endl;
            continue;
        }

        char star = '*';
        for (int i = height; i >= 1; i--) // Represents the Row
            cout << string(i-1, ' ') << string(height-(i-1), star) << '\n';
        cout << '\n';
    }
}
here is this good enough ?


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
#include <iostream>
#include <string>
using namespace std;
#include <cstdlib>
int main ()
{
int height;
string buff;
cout << "Enter height of triangle or \'q\' to quit" << endl;

for (int j=0; ; j++) // Repeats loop to infinity
{
cin >> buff;
if(buff=="q") break;
height=atoi(buff.c_str());
if(height==0)
{
cout<<"Character not valid\n";
continue;
}
cout << endl;

if (height < 0)
cout << "Height must be positive" << endl;

char star='*';
for (int i=height; i>=1; i--) // Represents the Row
{
if (i!=0)
{
for (int space=1; space<=i; space++) //Inserts spaces to align to left side
{
cout << " ";
}
}
for(int length=height-i ;length>=0; length--) //Inserts stars in columns
{
cout << star;
}
cout << endl;
}
cout << endl;
}
ending:
return 0;
}
Last edited on
Hmm didnt see someone posted before me.Yes his code is alot better.Though std::stoi is kind of nonstandard...
Though std::stoi is kind of nonstandard...

Tell that to the standard.
Though std::stoi is kind of nonstandard...

Tell that to the standard.


What i was saying is that it doesnt work on all compilers.I had to patch mingw in order for it to work : http://tehsausage.com/mingw-to-string
Last edited on
It's new to the standard, not "kind of nonstandard". Some compilers haven't finished implementing it yet, but it is standard C++.
Thanks a lot! Appreciate it! Have heard of this atoi however was unsure about how to use it. Thanks again
Topic archived. No new replies allowed.