0

I have no idea what is going on here. I do not get any errors, the program hits the first function and then skips to return 0. This is a practice exercise I'm doing. The user will input a number in a soda machine and then they receive the item which they chose. Any help would be greatly appreciated!

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

void Menu()
{
cout << "===========================" << endl;
cout << "========SODA MACHINE=======" << endl;
cout << "===========================" << endl;
cout << "Pick a soda... " << endl;
cout << "[1] Coca-Cola" << endl;
cout << "[2] Diet Coca-Cola" << endl;
cout << "[3] MUG Root Beer" << endl;
cout << "[4] Sprite" << endl;
}

int processSelection()
 {  
int selection;
cin >> selection;
cout << "This function was called." << endl; 
return selection;
}

void dropSoda()
{
int myselection;
myselection = processSelection();

switch (myselection)
{
case 1:
    cout << "Your can of Coca-Cola drops into the bin at the bottom of the       machine." << endl;
    break;
case 2:
    cout << "Your can of Diet Coca-Cola drops into the bin at the bottom of the machine." << endl;
    break;
case 3:
    cout << "Your can of MUG Root Beer drops into the bin at the bottom of the machine." << endl;
    break;
case 4:
    cout << "Your can of Sprite drops into the bin at the bottom of the     machine." << endl;
    break;
default:
    cout << "INVALID SELECTION." << endl;
    break;
}
}


int _tmain(int argc, _TCHAR* argv[])
{
Menu();
int processSelection();
void dropSoda();
return 0;
}
  • `int processSelection();` is a function declaration in your main body. What's it's purpose actually? – πάντα ῥεῖ Mar 17 '16 at 19:51
  • The line `Menu();` suggests that you have some understanding of how to call functions, but you've failed to apply that knowledge to the other two functions. – molbdnilo Mar 17 '16 at 19:54

2 Answers2

0
int processSelection();

This is a function declaration. It tells the compiler that there is a function processSelection that takes no arguments and returns int - but the compiler already knew that.

To call the function, you would write:

processSelection();

and the same for dropSoda.

user253751
  • 57,427
  • 7
  • 48
  • 90
  • Note: If you do this, you'll probably notice that `processSelection` gets called twice. That's because *you told the computer to do that* - one call in `main` and one in `dropSoda`. – user253751 Mar 17 '16 at 20:01
0

Your main function is declaring functions, not calling them.

int _tmain(int argc, _TCHAR* argv[])
{
    Menu();  // menu called
    // declare a function `processSelection` taking no arguments and returning an int
    int processSelection(); 
    // again, functoin declaration
    void dropSoda();
    return 0;
}

To fix, call the two functions the same way you called Menu()

int _tmain(int argc, _TCHAR* argv[])
{
    Menu();
    processSelection(); 
    dropSoda();
    return 0;
}
twentylemon
  • 1,248
  • 9
  • 11