loop all the time when hitting q to quit
Apr 28, 2015 at 1:33am UTC
when i hit q to quit it goes into an infi loop and just goes over and over. Is there another way to make this quit?
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
#include <iostream>
using namespace std;
void myswap(int &num1, int &num2);
int main(int argc, char ** argv) {
int num1, num2, num3, temp;
char quit='a' ;
cout << "Number Swapper\n" << endl;
while (quit != 'q' )
{
cout << "Enter Three numbers: " ;
cin >> num1 >> num2 >> num3;
cout << "Your numbers in order are: " ;
// Compares the first two numbers inputted
if (num1 >= num2)
{
myswap(num1, num2);
}
// compares the last number
if (num3 <= num2 && num3 <= num1)
{
myswap(num3, num2);
myswap(num1, num2);
}
// compares the last and middle
if (num3 >= num1 && num3 <= num2)
{
myswap(num3, num2);
}
cout << num1 << " " << num2 << " " << num3 << endl;// outputs the numbers in order
// press q to quit
if (quit == 'q' )
break ;
}
cin >> num1;
return 0;
}
//pulling function myswap
void myswap(int & num1, int & num2)
{
int temp = num1;
num1 = num2;
num2 = temp;
}
Apr 28, 2015 at 1:41am UTC
You're never inputting to quit. Add
1 2
cout << "Press q to quit or c to continue" ;
cin >> quit;
or something similar right above the if(quit == 'q') statement.
What is happening is that you're attempting to input a char to an int and the cin.fail() flag is being thrown. Once that happens all cin>> statements are ignored until you fix it and it's just looping infinitely.
Apr 28, 2015 at 2:03am UTC
ya i did that and it is not letting the loop continue right after the first numbers are imputed. is there a way for me to add it to my first cout like
1 2
cout<< "enter 3 numbers or press q to quit" ;
having that at the beginning and then have the loop continue until q is pressed?
Apr 28, 2015 at 2:05am UTC
This is very clunky and weird style but:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
while (true )
{
cout<< "enter 3 numbers or press q to quit"
cin >> num1 >> num2 >> num3;
if (cin.fail()) {
cin.clear();
cin.ignore(100, '\n' );
break ;
}
.
.
.
}
should work.
Last edited on Apr 28, 2015 at 2:10am UTC
Apr 28, 2015 at 2:24am UTC
hmmm, it does stop it, but it only stops asking me to input things.
like when i hit q it will just let me keep hitting enter.
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
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 << "Try again or press q to quit" << endl;
cin >> quit;
if (quit == 'q' )
break ;
else
{
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 <<" " ;
}
}
}
}
}
return 0;
}
i tried something similar to this but for some reason it will not work right in the other program
Apr 28, 2015 at 2:27am UTC
That's because you have a random cin outside the loop itself. Just remove that, it works fine for me without it.
Topic archived. No new replies allowed.