Finding a problem

•Describe the symptoms of your problem carefully and clearly.
Once I input something, nothing happens, and still asks for another input.

•Describe the environment in which it occurs (machine, OS, application, whatever).
CLion

•Describe the research you did to try and understand the problem before you asked the question.
Google, this forum, other forums. It's hard to search for an unknown problem.

•Describe the diagnostic steps you took to try and pin down the problem yourself before you asked the question.
Switched my input from a char, to a string. Tried to store the input in main().

This is a homework problem, so I am asking very nicely to please, please, please don't just tell/show me where the problem is. I would appreciate very much, any wisdom on how to pinpoint a problem, and any hints or tricks for future developing.

Also, this is not complete. I just want to get over this hurdle before continuing.

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
  #include <iostream>
#include <string>


using namespace std;

void printMenu(void);
string getUserInput(void);
int handleUserInput(string);
void printMachineStatus(int, int, int, int);
int storeLucky(void);
int storeCamel(void);
int storeGaul(void);
int storePall(void);

int main() {

    printMenu();
    getUserInput();
    handleUserInput(getUserInput());

    return 0;
}
void printMenu(void) {
    cout << "s - report the machine status" << endl
         << "d - drop in a quarter" << endl
         << "1 - pull the 1st knob" << endl
         << "2 - pull the 2nd knob" << endl
         << "3 - pull the 3rd knob" << endl
         << "4 - pull the 4th knob" << endl
         << "r - restock the machine" << endl
         << "q - quit" << endl;
}
string getUserInput(void){
    string Input;
    cin >> Input;
    return Input;
}
int handleUserInput(string){
    if (getUserInput() == "s") {
        printMachineStatus(storeLucky(), storeCamel(), storeGaul(), storePall());
        cout << "s" << endl;
    }
    else if (getUserInput() == "d") {
        //drop in quarter
        cout << "d" << endl;
    }
    else if (getUserInput() == "1") {
        //pull1Knob
        cout << "1" << endl;
    }
    else if (getUserInput() == "2") {
        //pull2Knob
        cout << "2" << endl;
    }
    else if (getUserInput() == "3") {
        //pull3Knob
        cout << "3" << endl;
    }
    else if (getUserInput() == "4") {
        //pull4Knob
        cout << "4" << endl;
    }
    else if (getUserInput() == "r") {
        //restock machine
        cout << "r" << endl;
    }
    else if (getUserInput() == "q"){
        cout << "So long!" << endl;
        return 0;
    }
    else {
        cout << "I do not understand." << endl;
        return 0;
    }
    return 0;
}
void printMachineStatus(int, int, int, int){
    cout << "1: " << storeLucky() << " packs of Lucky Strikes" << endl
         << "2: " << storeCamel() << " packs of Camels" << endl
         << "3: " << storeGaul() << " packs of Gauloises" << endl
         << "4: " << storePall() << " packs of Pall Malls" << endl;
}
int storeLucky(void){
    int packs = 5;
    return packs;
}
int storeCamel(void){
    int packs = 7;
    return packs;
}
int storeGaul(void){
    int packs = 1;
    return packs;
}
int storePall(void){
    int packs = 6;
    return packs;
Last edited on
Line 19: You are invoking a function that returns string type variable and you are ignoring the returned value. Remove this line or comment it out.

Line 20: You are yet again, invoking the same function mentioned in Line 19 and now, you are passing it as a parameter to another function. Ok..

Line 39: The function is supposed to receive string type variable as a parameter. Specify the variable name.
For example
 
int handleUserInput(string userInput )


And then, replace getUserInput() with userInput at Lines 40 , 44 , 48, 52, 56, 60 , 64 , 68.
Last edited on
Once I input something, nothing happens, and still asks for another input.

You call getUserInput() twice. Once at line 19 and again as an argument on line 20.

Then at lines 40-68, you call getUserInput() repeatedly. Every time you call getUserInput(), you're going to be asking the user for more input.

Note that at line 39, handleUserInput() accepts a string as an argument, but you don't give the argument a name. You need to give the argument a name so you can refer to it. In each of the if statements, you should be referring to the argument name rather than repeatedly calling getUserInput().

1
2
int handleUserInput (string str)
{  if (str == "s") 







> please, please, please don't just tell/show me where the problem is. I would appreciate very much, any wisdom on how to pinpoint a problem, and any hints or tricks for future developing.

You need to learn how to properly pass variables to user-defined functions as parameters.
Any parameter that is passed can be used as a "local" variable in the user-defined function.

http://www.cplusplus.com/doc/tutorial/functions/
Last edited on
You need to learn how to properly pass variables to user-defined functions as parameters.
Any parameter that is passed can be used as a "local" variable in the user-defined function.


Perfect, thank you! That was exactly what I was looking for. I thought I had functions down, obviously I have more studying to do. Thank you again.
closed account (E0p9LyTq)
Another look at functions, parameters and return values:

http://www.learncpp.com/cpp-tutorial/71-function-parameters-and-arguments/
http://www.learncpp.com/cpp-tutorial/71-function-parameters-and-arguments/


This link is a goldmine. I'll nail these functions now. Thank you!
Topic archived. No new replies allowed.