Mar 1, 2014 at 12:32pm UTC
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;
}
Mar 1, 2014 at 4:13pm UTC
I don't understand, you want the user to give input while you are outputting at the top of your lungs?
Mar 1, 2014 at 4:48pm UTC
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".
Mar 3, 2014 at 5:45pm UTC
I want only the 'q' key to quit. Any other letter should give a message saying that an invalid input was entered.
Mar 3, 2014 at 7:29pm UTC
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 Mar 3, 2014 at 7:40pm UTC
Mar 3, 2014 at 7:33pm UTC
Hmm didnt see someone posted before me.Yes his code is alot better.Though std::stoi is kind of nonstandard...
Mar 3, 2014 at 8:34pm UTC
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 Mar 3, 2014 at 8:36pm UTC
Mar 3, 2014 at 9:29pm UTC
It's new to the standard, not "kind of nonstandard". Some compilers haven't finished implementing it yet, but it is standard C++.
Mar 4, 2014 at 5:54pm UTC
Thanks a lot! Appreciate it! Have heard of this atoi however was unsure about how to use it. Thanks again