2016-03-17 2 views
0

Я понятия не имею, что здесь происходит. Я не получаю никаких ошибок, программа попадает в первую функцию, а затем пропускает, чтобы вернуть 0. Это упражнение, которое я делаю. Пользователь вводит номер в машине соды, а затем получает элемент, который они выбрали. Любая помощь будет принята с благодарностью!Beginner C++ Функция пропущена: ошибок нет

#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; 
} 
+0

'INT processSelection();' это объявление функции в главном теле. Какова цель на самом деле? –

+0

Строка 'Menu();' предполагает, что у вас есть некоторое представление о том, как вызывать функции, но вы не смогли применить это знание к двум другим функциям. – molbdnilo

ответ

0
int processSelection(); 

Это объявление функции. Он сообщает компилятору, что есть функция processSelection, которая не принимает аргументов и возвращает int - но компилятор уже знал это.

Для вызова функции, можно было бы написать:

processSelection(); 

и то же самое для dropSoda.

+0

Примечание. Если вы это сделаете, вы, вероятно, заметите, что 'processSelection' вызывается дважды. Это потому, что * вы сказали компьютеру сделать это * - один вызов в 'main' и один в' dropSoda'. – immibis

0

Ваша основная функция - объявлять функции, а не вызывать их.

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; 
} 

Чтобы исправить, вызовите две функции так же, как под названием Menu()

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    Menu(); 
    processSelection(); 
    dropSoda(); 
    return 0; 
} 
Смежные вопросы