making this loop continue and q to quit
Apr 11, 2015 at 10:42pm UTC
I am trying to make this program keep asking for inputs to make new squares and also to stop when you hit q. i have tried different things but i keep getting errors. this will run but it stops due to the break. if the break is not there it will continue to run and not stop.
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
#include <iostream>
using namespace std;
int main() {
int size;
int row;
int col;
int quit;
cout <<"Please enter a number between 1 and 10: " ;
cin >> size;
if (size < 0, size > 10)
{
cout <<"That is not a positive number, please try again!! " <<endl;
cin >> size;
}
while (true )
{
for (int row = 0; row < size; row++){
for (int col = 0; col < size; col++){
if (row==0 || row == (size-1) )
{
if (col == 0 || col == size - 1)
cout << "-" ;
else {
cout << "-" ;
}
}
else if (col==0 || col == (size-1) )
cout <<"|" ;
else
cout <<" " ;
}
cout << endl;
}
cout << "Try again or press q to quit" << endl;
break ;
}
return 0;
}
Apr 11, 2015 at 10:51pm UTC
if (size < 0, size > 10) // Doesnt work that way. You want to say if the size is less than 0 OR size is bigger than 10. Then you use the || operator.
if (size < 0 || size > 10)
if you want the user to enter quit, then you need a char variable, not integer.
char quit = 'a' ; // initialize it to any other letter than q
1 2 3 4
while (quit != 'q' )
{
// Everything that you want repeated until q is pressed you put here. Remove the break.
}
Last edited on Apr 11, 2015 at 11:08pm UTC
Apr 11, 2015 at 11:07pm UTC
@tarikNeaj
ok i replaced true with the quit and here is the code but i'm getting invalid conversion from const char to char
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
#include <iostream>
using namespace std;
int main() {
int size;
int row;
int col;
char quit = "q" ;
cout <<"Please enter a number between 1 and 10: " ;
cin >> size;
if (size < 0 || size > 10)
{
cout <<"That is not a positive number, please try again!! " <<endl;
cin >> size;
}
while (quit != "q" )
{
for (int row = 0; row < size; row++){
for (int col = 0; col < size; col++){
if (row==0 || row == (size-1) )
{
if (col == 0 || col == size - 1)
cout << "-" ;
else {
cout << "-" ;
}
}
else if (col==0 || col == (size-1) )
cout <<"|" ;
else
cout <<" " ;
}
cout << endl;
}
cout << "Try again or press q to quit" << endl;
}
return 0;
}
Apr 11, 2015 at 11:08pm UTC
Thats becuase you didint look close enough.
while (quit != 'q' ) // ' ' not " "
Last edited on Apr 11, 2015 at 11:08pm UTC
Apr 11, 2015 at 11:51pm UTC
@Tarikneaj
ok same code I changed to sigle '' and it runs but after i input a number it just says press any key to continue. does not make my square . I moved the cout try again or press q into the main while statement
Apr 11, 2015 at 11:57pm UTC
char quit = 'a' ; // initialize it to any other letter than q
You gotta read things more carefully my friend
Edit: And dont forget to add a input at the end so the user can actually choose to enter q or not.
Last edited on Apr 12, 2015 at 12:00am UTC
Apr 12, 2015 at 4:44am UTC
ok i had to just start over i was getting my self lost. here is my new code and it does the same thing but a little shorter. only thing it does is when i hit q it will ask the enter a number from 1 to 10 over and over and will not close the program
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
#include<iostream>
using namespace std;
int main()
{
int size ;
char exit1;
do {
cout<< "Enter a number from 1 to 10 or hit q to leave: \n" ;
cin>> size;
if (size < 0 || size > 10)
{
cout <<"That is not a positive number, please try again!! " <<endl;
cin >> size;
}
for (int width=1; width<=size; width++)
{
if (width <= 1)
for (int width=1; width<=size; width++)
{
cout<< "-" ;
}
else if (width<size)
{
cout<< endl;
for (int width2=1; width2<=size; width2++)
{
if (width2==1 || width2==size)
cout<< "|" ;
else
cout<< " " ;
}
}
else
{
cout<< endl;
for (int width3=1; width3<=size; width3++)
{
cout<<"-" ;
}
}
}
cout << endl;
}
while (exit1 != 'q' );
}
Apr 12, 2015 at 9:19am UTC
You never ask the user whether he wants to quit or not... you never use the variable exit1 to take input from the user.
Apr 12, 2015 at 10:25am UTC
only thing it does is when i hit q it will ask the enter a number from 1 to 10 over and over and will not close the program
1 2
cout<< "Enter a number from 1 to 10 or hit q to leave: \n" ;
cin>> size;
size is an int type, what would you expect if you enter 'q'?
edit:
this is okay but it will block the execution of program the moment you hit q and will not conitnue anymore
Last edited on Apr 12, 2015 at 10:28am UTC
Topic archived. No new replies allowed.