This is my code for my program, it seems to work fine but the problem is i think roll has to be a char in order for q to quit to work when i implement the code in there. but when i use char, when i input the number i want it to randomize, its obviously not the numbers i entered in the selection. how can i get a simple q to quit to work, please help.

#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
#include <cstdlib>

using namespace std;

int main()

{
        int roll;
        int side1 = 0;
        int side2 = 0;
        int side3 = 0;
        int side4 = 0;
        int side5 = 0;
        int side6 = 0;
        const int low = 1;


        srand((unsigned)time(NULL));


            cout << "\nHow many times would you like to roll the dice? (Enter 'Q' to Quit): ";
            cin >> roll;

// Ensures 1 is not entered at all times

    {
         while (roll <= low)
        {
            cout << "\nPlease enter a number greater than 1: ";
            cin >> roll;
        }

// Random Number Code in the range of what the user inputed for roll
        }

         for ( int i = 0; i < roll; i++ )
        {
         int counter = rand() % 6 + 1; 

// Switch statement to switch each (dice side) with rand function

         switch ( counter )

{   
case 1:
side1++;

break;

case 2:
side2++;

break;

case 3:
side3++;

break;

case 4:
side4++;

break;

case 5:
side5++;

break;

case 6:
side6++;

break;
}
    }

const double percent = .100;

cout << "\n\nDice Roll Statistics";
cout << setw(2) << " \n\n# Rolled" << setw(10) << " # Times" << setw(11) << " % Times\n";
cout << setw(2) << " -------" << setw(10) << " -------" << setw(12) << " -------\n\n";
cout << setw(9) << "1 " << setw(9) << side1 << setw(7) << "% " << side1/percent << endl;
cout << setw(9) << "2 " << setw(9) << side2 << setw(7) << "% " << side2/percent << endl;
cout << setw(9) << "3 " << setw(9) << side3 << setw(7) << "% " << side3/percent << endl;
cout << setw(9) << "4 " << setw(9) << side4 << setw(7) << "% " << side4/percent << endl;
cout << setw(9) << "5 " << setw(9) << side5 << setw(7) << "% " << side5/percent << endl;
cout << setw(9) << "6 " << setw(9) << side6 << setw(7) << "% " << side6/percent << endl << endl;


system("PAUSE");
return 0;
}

If roll is an int then you can set it to 'q' by simply doing this

roll = 'q';

but having one variable for two independent uses can lead to confusing code.

Well, I really see no point in why you need the option to quit using 'q' in this case. Just let the user enter the number 2 or whatever. I ran some tests with your code, and if you enter a char from within the for-loop you enter an infinite loop. I think that is because cin only reads integers from the buffer, leaving all chars (including the '\n').

I would like to recommend that you either ask the user if he/she wants to quit separately, and then ask for an integer, OR you will have to read an entire c-string:

char * inStr;
cin.get(inStr);

Then you have to check stuff like if the length is 2 (char + '\n') then see if it equals 'q'. If not, then you will have to ranslate it manually to an integer.

Good luck to you!
Emil Olofsson

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.