J
Jim
This has got to be really simple but I can't figure it out... I got
swamped with work and am doing this project for a class that I'm
taking and I can't figure out why the output is like this.
I'm taking a Functional Programming in C and I have to write a
shuffling program. The problem is that when I ask the user if they
want to shuffle, they type y or n to answer. It then goes into a
switch statement and executes code. It exits if they type no and is
suppose to keep looping until they say no.
So... Here is the output I'm getting:
-------------
The array holds 20 elements which consists of:
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20 }
Would you like to shuffle the array? (y=yes, n=no): y
You have said you would like to shuffle.
Shuffling...
Would you like to shuffle the array? (y=yes, n=no): Would you like to
shuffle the array? (y=yes, n=no):
----------------
Notice that the question is getting asked twice instead of just once
(after it loops). I added a default: line to my switch statement and
it seems to be reading something else in the userval variable but I
can't figure out what I'm doing wrong. Please help... This is due in
4 hours. -- Before anyone flames me, I didn't purposely
procrastinate on this, I work full time, have a family and didn't get
a chance to do this earlier.
Here is my code:
-----------------
#include <math.h>
21 #include <stdio.h>
22 #include <stdbool.h>
23 #include <stdlib.h>
24 #include <math.h>
25 #define SIZE 20
26
27 // function prototypes
28
29
30 // main program
31 int main(void) {
32
33 // declare variables
34 int
35 array1[SIZE]= { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20 },
36 cnt = 0;
37
38 bool cont = true;
39 char userval;
40
41 // while(cont) {
42 // Display the contents of the array.
43 printf("The array holds %d elements which consists of:
\n { ", SIZE);
44
45 for(cnt = 0; cnt < SIZE; cnt++) {
46 (cnt != SIZE - 1) ? printf("%d, ", array1[cnt]) :
printf("%d ", array1[cnt]);
47 }
48
49 printf("}\n\n");
50
51 while(cont) {
52 // ask if the user woule like to shuffle the contents
of the array
53 printf("Would you like to shuffle the array? (y=yes,
n=no): ");
54 scanf("%c", &userval);
55
56 switch(userval) {
57 case 'y':
58 case 'Y':
59 printf("You have said you would like to
shuffle.\nShuffling...\n\n");
60 break;
61
62 case 'n':
63 case 'N':
64 printf("No shuffling will take place.
\nExiting...\n");
65 cont = false;
66 break;
67 }
68 }
69 }
-----------------
swamped with work and am doing this project for a class that I'm
taking and I can't figure out why the output is like this.
I'm taking a Functional Programming in C and I have to write a
shuffling program. The problem is that when I ask the user if they
want to shuffle, they type y or n to answer. It then goes into a
switch statement and executes code. It exits if they type no and is
suppose to keep looping until they say no.
So... Here is the output I'm getting:
-------------
The array holds 20 elements which consists of:
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20 }
Would you like to shuffle the array? (y=yes, n=no): y
You have said you would like to shuffle.
Shuffling...
Would you like to shuffle the array? (y=yes, n=no): Would you like to
shuffle the array? (y=yes, n=no):
----------------
Notice that the question is getting asked twice instead of just once
(after it loops). I added a default: line to my switch statement and
it seems to be reading something else in the userval variable but I
can't figure out what I'm doing wrong. Please help... This is due in
4 hours. -- Before anyone flames me, I didn't purposely
procrastinate on this, I work full time, have a family and didn't get
a chance to do this earlier.
Here is my code:
-----------------
#include <math.h>
21 #include <stdio.h>
22 #include <stdbool.h>
23 #include <stdlib.h>
24 #include <math.h>
25 #define SIZE 20
26
27 // function prototypes
28
29
30 // main program
31 int main(void) {
32
33 // declare variables
34 int
35 array1[SIZE]= { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20 },
36 cnt = 0;
37
38 bool cont = true;
39 char userval;
40
41 // while(cont) {
42 // Display the contents of the array.
43 printf("The array holds %d elements which consists of:
\n { ", SIZE);
44
45 for(cnt = 0; cnt < SIZE; cnt++) {
46 (cnt != SIZE - 1) ? printf("%d, ", array1[cnt]) :
printf("%d ", array1[cnt]);
47 }
48
49 printf("}\n\n");
50
51 while(cont) {
52 // ask if the user woule like to shuffle the contents
of the array
53 printf("Would you like to shuffle the array? (y=yes,
n=no): ");
54 scanf("%c", &userval);
55
56 switch(userval) {
57 case 'y':
58 case 'Y':
59 printf("You have said you would like to
shuffle.\nShuffling...\n\n");
60 break;
61
62 case 'n':
63 case 'N':
64 printf("No shuffling will take place.
\nExiting...\n");
65 cont = false;
66 break;
67 }
68 }
69 }
-----------------