Please help ASAP, program won't quit correctly

OK, so after I enter a number the first time, the table is displayed and I'm prompted for a new number. I use the 'q' command for quit the second time and it displays the table AGAIN! It shouldn't display the table that time, and I'm getting an error message on the bed test saying it won't exit correctly. Please tell me why! (Check the MAIN function towards the bottom)

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <iostream>
#include <iomanip>
using namespace std;
using std::setw;

/**********************************************************************
* Displays the table of the Data Types, using the inputted variable as the 
* object, and assigns that variable to each of the different data types.
***********************************************************************/
void displayTable(unsigned long long value)
{
   bool boolVar = value;
   char charVar = value;
   unsigned char uChar = value;
   short shortVar = value;
   unsigned short uShort = value;
   int integer = value;
   unsigned int uInt = value;
   long longVar = value;
   unsigned long uLong = value;
   long long longLong = value;
   unsigned long long uLongLong = value;
   float floatVar = value;
   double doubleVar = value;
   long double lDouble = value;
   
   cout << "\nOriginal input value: " << value << endl << endl;
   cout << "   Data Type       Bytes      Value (Dec)           Value (Hex)"
        << "       Overflow\n";
   cout << "------------------ -----  --------------------  "
        << "--------------------  --------\n";

   cout << "bool                 " << sizeof (bool) << "    " << setw(20) 
        << boolVar << "  " << setw(20) << hex << boolVar << dec 
        << "      N/A\n";



   cout << "char                 " << sizeof (char) << "    " << setw(20);
   if (charVar > 122 || charVar < 65)
   {
      cout << (int)charVar;
      cout << "  " << setw(20) << hex << (int)charVar << dec;
   }
   else
   {
      cout << charVar;
      cout << "  " << setw(20) << hex << (int)charVar << dec; 
   }
   if (charVar != value)
      cout << "      T\n";
   else
      cout << "      F\n";

   cout << "unsigned char        " << sizeof (unsigned char) << "    " 
        << setw(20);
   if (uChar > 122 || uChar < 65)
   {
      cout << (int)uChar;
      cout << "  " << setw(20) << hex << (int)uChar << dec; 
   }
   else
   {
      cout << uChar ;
      cout << "  " << setw(20) << hex << (int)uChar << dec; 
   }
   if (uChar != value)
      cout << "      T\n";
   else
      cout << "      F\n";

   cout << "short                " << sizeof (short) << "    " << setw(20) 
        << shortVar << "  " << setw(20) << hex << shortVar << dec;
   if (shortVar != value)
      cout << "      T\n";
   else
      cout << "      F\n";

   cout << "unsigned short       " << sizeof (unsigned short) << "    " 
        << setw(20) << uShort << "  " 
        << setw(20) << hex << uShort << dec;
   if (uShort != value)
      cout << "      T\n";
   else
      cout << "      F\n";

   cout << "int                  " << sizeof (int) << "    " << setw(20) 
        << integer << "  " << setw(20) << hex << integer << dec;
   if (integer != value)
      cout << "      T\n";
   else
      cout << "      F\n";

   cout << "unsigned int         " << sizeof  (unsigned int) << "    " 
        << setw(20) << uInt << "  " << setw(20) << hex << uInt << dec;
   if (uInt != value)
      cout << "      T\n";
   else
      cout << "      F\n";

   cout << "long                 " << sizeof (long) << "    " << setw(20) 
        << longVar << "  " << setw(20) << hex << longVar << dec;
   if (longVar != value || longVar < 0)
      cout << "      T\n";
   else
      cout << "      F\n";

   cout << "unsigned long        " << sizeof (unsigned long) << "    " 
        << setw(20) << uLong << "  " << setw(20) << hex << uLong << dec;
   if (uLong != value)
      cout << "      T\n";
   else
      cout << "      F\n";

   cout << "long long            " << sizeof (long long) << "    " << setw(20)
        << longLong << "  " << setw(20) << hex << longLong << dec;
   if (longLong < 0 || longLong != value)
      cout << "      T\n";
   else
      cout << "      F\n";

   cout << "unsigned long long   " << sizeof (unsigned long long) << "    " 
        << setw(20) << uLongLong << "  " << setw(20) << hex << uLongLong 
        << dec;
   if (uLongLong > value || uLongLong < value)
      cout << "      T\n";
   else
      cout << "      F\n";

   cout << "float                " << sizeof (float) << "    " << setw(20)  
        << floatVar << "  " << setw(20) << scientific << setprecision (5)  
        << floatVar << fixed;
   if (floatVar != value || floatVar > value || floatVar > 100000000)
      cout << "      T\n";
   else
      cout << "      F\n";

   cout << "double               " << sizeof (double) << "    " << setw(20) 
        << setprecision (0) << doubleVar << "  " << setw(20) << scientific 
        << setprecision (5) <<  doubleVar << fixed;
   if (doubleVar != value || doubleVar > value || 
       doubleVar > 10000000000000000)
      cout << "      T\n";
   else
      cout << "      F\n";

   cout << "long double         " << sizeof (long double) << "    " << setw(20)
        << setprecision (0) << lDouble << "  " << setw(20) << scientific 
        << setprecision (5) <<  lDouble << fixed;
   if (lDouble > value)
      cout << "      T\n";
   else
      cout << "      F\n";

   cout << fixed << dec << setprecision (0);

   return;
}

/**********************************************************************
* Runs the program, declares a variable, prompts user for input, and calls
* function, while looping the actions
***********************************************************************/
int main()
{
   unsigned long long value = 0;
   cout << "\nEnter a number (q to quit): ";
   cin >> value;
   if (cin.fail())
      return 0;
   else
      displayTable(value);
   while (!cin.fail())
   {
      cin.clear();
      cin.ignore();
      cout << "\nEnter a number (q to quit): ";
      cin >> value;
      switch (value)
      {
         case 'q':
         case 'Q':
            return 0;
            break;
         default:
            displayTable(value);
      }
   }

   return 0;
}
Last edited on
You are entering into an integer (unsigned long long) to be exact, which won't read characters properly with cin>>. The only reason your switch/case even works with characters is because a character literal is convertible to an int, it's ASCII value. Try putting in the ASCII value of a lowercase or uppercase Q and watch it not display the table with that value. :P

You may want to read input as a string and try converting it to a number. If it works, display the table with that number. Otherwise, check to see if it is a Q and quit or report an error as applicable.

And is there a reason you made another topic about this?
http://cplusplus.com/forum/general/50668/
Last edited on
Topic archived. No new replies allowed.