# Thread: I don't understand this.

1. ## I don't understand this.

Here is the assignment that I need to do. How should I be approaching this? I'm a 3.8 GPA student and I fear I won't pass this C programming. S.O.S.

The goal of this assignment is to work with repetition and selection structures, with a focus on repetition. As in past assignments you will create a small calculator application, although it will not be an extension of the earlier calculators. Instead, your program should present the user with a list of options, read the user's input, process the option, and then repeat until the user requests to end the program. The options you must accept and process are:
Code:
```    * 'Q' or 'q': Quit the program
* 'Eval(<exp>)': Evaluate a simple expression
* '!n': Evaluate an integer factorial
* 'P(n,k)': Count the number of permutations of length k in a string of length n.```
The details of each option are presented below. In this assignment you may use the do{} while(), while() {}, or for(;{} structures, but you may not use the goto or other unconditional repetition structures. Also, you may not use the math.h library or any other external math library to calculate your results - the goal of this assignment is to build your own code! Further, in this assignment you must accept input and display output as shown in the example below - you do not have the option of altering the input sequence or output sequence in this assignment.

The output below shows the input and output style that you should have in your program.
Code:
```Enter one of the following options:
Q - Quit the program
Eval(<exp>) - Evaluate a simple expression
! - Evaluate an integer factorial
P(n,k) - Count the k-permutations of a string of n characters

A string of 3 characters has 6 permutations of length 2

Enter one of the following options:
Q - Quit the program
Eval(<exp>) - Evaluate a simple expression
! - Evaluate an integer factorial
P(n,k) - Count the k-permutations of a string of n characters

Enter your option: Eval(2 ^ 3)
2 ^ 3 = 8

Enter one of the following options:
Q - Quit the program
Eval(<exp>) - Evaluate a simple expression
! - Evaluate an integer factorial
P(n,k) - Count the k-permutations of a string of n characters

Enter your option: Eval(2 - 1023)
2 - 1023 = -1021

Enter one of the following options:
Q - Quit the program
Eval(<exp>) - Evaluate a simple expression
! - Evaluate an integer factorial
P(n,k) - Count the k-permutations of a string of n characters

Enter your option: Eval(3.4 % 5)
3.40 % 5.00 = 3.00

Enter one of the following options:
Q - Quit the program
Eval(<exp>) - Evaluate a simple expression
! - Evaluate an integer factorial
P(n,k) - Count the k-permutations of a string of n characters

5! = 120

Enter one of the following options:
Q - Quit the program
Eval(<exp>) - Evaluate a simple expression
! - Evaluate an integer factorial
P(n,k) - Count the k-permutations of a string of n characters

13! = 6227020800

Enter one of the following options:
Q - Quit the program
Eval(<exp>) - Evaluate a simple expression
! - Evaluate an integer factorial
P(n,k) - Count the k-permutations of a string of n characters

A string of 3 characters has 6 permutations of length 2

Enter one of the following options:
Q - Quit the program
Eval(<exp>) - Evaluate a simple expression
! - Evaluate an integer factorial
P(n,k) - Count the k-permutations of a string of n characters

A string of 4 characters has 24 permutations of length 2

Enter one of the following options:
Q - Quit the program
Eval(<exp>) - Evaluate a simple expression
! - Evaluate an integer factorial
P(n,k) - Count the k-permutations of a string of n characters

Sorry it's so long, but I feel like you should see all of it.

2. Okay, so what have you tried so far?

3. How should I approach this? what kind of structure? I havent done anything yet, but I've been writing programs not thinking about it firstly, and Im not doing well. so enlighten me please? pretty please?

4. Well your first concern should be to get a basic menu structure going on, and leave the control for what you actually do in that case empty for now. That requires you to parse what the user inputs, see if the string he has inputted is a valid choice or not. If it is a valid choice, which one is it?

1) Did he type "Q" or "Quiet"? Only the first is valid
2) Did he type Eval(<expr>)? Is the expression valid (i.e. does it contain two operands and a valid operation symbol in between the operands? Are these operands numbers and not something else?
3) Did he type !6 or !shhhhh? Is he asking for a factorial or just messing with me.
4) Did he type P(2,3) or P(trickor,treat)?
5) Did he type something that doesn't even resemble any of the commands?

See what I am getting at?

5. You can have an integer say:

int choice;

to represent which one of the four it is, or to be negative in case the input is invalid.

Then just switch(choice) and for each valid case perform whatever calculations are needed. You can then use functions outside of main to do the actual calculations so that you keep your switch structure clean and easy to understand.

6. Feeling somewhat generous, here's a start I just whipped up:
Code:
```#include <stdio.h>
#include <string.h>

enum Choice { Invalid = -1, Quit, Eval, Fact, Perm };

void printMenuOption (const char * const opt, const char * const desc);

int main()
{
char buf[256] = { 0 };
enum Choice c = getMenuChoice(buf, sizeof(buf));
while (c != Quit)
{
if (c != Invalid)

}
printf("Bye!\n");
return 0;
}

void printMenuOption (const char * const opt, const char * const desc)
{
printf("%11s - %s\n", opt, desc);
}

{
printf("Enter one of the following options:\n");
printMenuOption("P(n,k)", "Count the k-permutations of a string of n charac\
ters");
}

enum Choice getMenuChoice(char *buf, const size_t len)
{
enum Choice choice = Invalid;
if (fgets(buf, len, stdin) == NULL)
return choice;
buf[strlen(buf) - 1] = 0;
switch (buf[0])
{
case 'Q':
case 'q':
choice = Quit;
break;
case 'E':
// Let's be optimistic
choice = Eval;
break;
case '!':
choice = Fact;
break;
case 'P':
choice = Perm;
break;
default:
break;
}

return choice;
}```
Not perfect, but might get you started.

7. Originally Posted by rags_to_riches
Feeling somewhat generous, here's a start I just whipped up:
Code:
```#include <stdio.h>
#include <string.h>

enum Choice { Invalid = -1, Quit, Eval, Fact, Perm };

void printMenuOption (const char * const opt, const char * const desc);

int main()
{
char buf[256] = { 0 };
enum Choice c = getMenuChoice(buf, sizeof(buf));
while (c != Quit)
{
if (c != Invalid)

}
printf("Bye!\n");
return 0;
}

void printMenuOption (const char * const opt, const char * const desc)
{
printf("%11s - %s\n", opt, desc);
}

{
printf("Enter one of the following options:\n");
printMenuOption("P(n,k)", "Count the k-permutations of a string of n charac\
ters");
}

enum Choice getMenuChoice(char *buf, const size_t len)
{
enum Choice choice = Invalid;
if (fgets(buf, len, stdin) == NULL)
return choice;
buf[strlen(buf) - 1] = 0;
switch (buf[0])
{
case 'Q':
case 'q':
choice = Quit;
break;
case 'E':
// Let's be optimistic
choice = Eval;
break;
case '!':
choice = Fact;
break;
case 'P':
choice = Perm;
break;
default:
break;
}

return choice;
}```
Not perfect, but might get you started.
Uhm NO. This doesn't even compile.

Also, int main() is to be avoided in C. See FAQ.

8. It probably doesn't compile under Visual Studio, but using an up-to-date compiler and standards it works just fine.
Code:
```r2r\$ gcc -Wall -pedantic -std=c99 -o what what.c
r2r\$ ./what
Enter one of the following options:
Q - Quit the program
Eval(<exp>) - Evaluate a simple expression
! - Evaluate an integer factorial
P(n,k) - Count the k-permutations of a string of n characters

Enter one of the following options:
Q - Quit the program
Eval(<exp>) - Evaluate a simple expression
! - Evaluate an integer factorial
P(n,k) - Count the k-permutations of a string of n characters

Enter one of the following options:
Q - Quit the program
Eval(<exp>) - Evaluate a simple expression
! - Evaluate an integer factorial
P(n,k) - Count the k-permutations of a string of n characters

Bye!
r2r\$```
It can be made to compile under C89 with the following change:
Code:
```char buf[256] = { 0 };
enum Choice c = Invalid;
while ((c = getMenuChoice(buf)) != Quit)
{
if (c != Invalid)                                                      \

}```
Also, int main() is to be avoided in C. See FAQ.
Oh, sorry it should be
Code:
`int main(void)`
.

9. I see what you're getting at. The order in which these processes is to be can somewhat confuse me. This is more than enough to get me started. thanks to all, especially dude that wrote code. I won't be copying it, just referencing.

10. Originally Posted by claudiu
You can have an integer say:

int choice;

to represent which one of the four it is, or to be negative in case the input is invalid.

Then just switch(choice) and for each valid case perform whatever calculations are needed. You can then use functions outside of main to do the actual calculations so that you keep your switch structure clean and easy to understand.
I am starting to understand what you mean by this. My instructor is awful, but you put it into better words than he did. f.m.l.

When I get confused next I will post up what I have. thanks man.

11. So after 5 hours at the tutoring center with another guy in my class. We finished most of this lab. any thoughts?

One thing, the factorial won't work past double digits. I get negative numbers. I think this is because it's reading one character?

one thing to remember, I am using dev c++ (i know, i know) and compiling through that program it runs exactly like it should.

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define  bufSize 8192
int main() {
int         varA, varB, i;
int         operA=1;
char        userInput[bufSize], char1;
char        doneFlag = 'f';

do {
printf("Enter one of the following options: \n\
Q - Quit the following program\n\
Eval(int * int) - Evaluate a simple expression\n\
! - Evaluate an integer factorial\n\
P(n,k) - Count the k-permutations of a string of n characters\n\
\n\
fgets(userInput,bufSize,stdin);

if (userInput[0] == 'q' || userInput[0] == 'Q') {
doneFlag = 't';

} else if (strstr(userInput,"Eval\n") != NULL) {
scanf("%*c%*c%*c%*c%*c%d%*c%c%*c%d%*c%*c",&varA,&char1,&varB);

if(char1 == '+')
{operA = varA + varB;}
else if(char1 == '-')
{operA = varA - varB;}
else if(char1 == '*')
{operA = varA * varB;}
else if(char1 == '/')
{operA = varA / varB;}
else if(char1 == '%')
{operA = varA % varB;}
else if(char1 == '^')
{for(i = 1; i <= varB; i++)
operA *= varA;
}
printf("%d %c %d = %d\n\n",varA,char1,varB,operA);

} else if (userInput[0] == '!') {
scanf("%d%*c",&varA);
for(i = 1; i <= varA; i++)
operA *= i;
printf("%d! = %d",varA,operA);

} else if (userInput[0] ==  'p' || userInput[0] == 'P') {
printf("You entered \n");
} else {
printf("\n? no valid option");
}

operA = 1;
scanf("%*c");
} while (doneFlag == 'f');
return 0;
}```

12. Well it's not really correct at least in my interpretation of the requirements. What happens if the user enters "quiet' as a command? your program quits when in fact it's supposed to print something like "invalid command"

13. Originally Posted by claudiu
Well it's not really correct at least in my interpretation of the requirements. What happens if the user enters "quiet' as a command? your program quits when in fact it's supposed to print something like "invalid command"
I also forgot to mention that we hadn't finished the permutations part yet.

It's reading the Q in quiet? Other strings of characters show "no valid option" if you delete the unnecessary ? that's in the code. lol.

14. Look I haven't actually run your code I admit, but make sure you run it with all the possible and impossible inputs. It should be able to handle the monkey test(i.e. a monkey typing on the keyboard something like oashdandbkjadfsfhfhohoafsihhjoadodojadfsio;la[pqewpoqw+)

Get it?

15. Originally Posted by claudiu
Look I haven't actually run your code I admit, but make sure you run it with all the possible and impossible inputs. It should be able to handle the monkey test(i.e. a monkey typing on the keyboard something like oashdandbkjadfsfhfhohoafsihhjoadodojadfsio;la[pqewpoqw+)

Get it?
what I am saying is you are correct it quits the program from reading q in quiet, other strings of random "monkey" characters, even those including q not as the first character show "no valid option". how can i stop it from reading q as quit if other characters are involved?

also, any idea about the factorial issue? some double digits give me negative numbers..