跳过初学者C++功能:没有错误

Beginner C++ Function skipped: No errors

本文关键字:有错误 功能 C++ 初学者      更新时间:2023-10-16

我不知道这里发生了什么。 我没有收到任何错误,程序点击第一个函数,然后跳过返回 0。 这是我正在做的练习。 用户将在汽水机中输入一个数字,然后他们会收到他们选择的项目。 任何帮助将不胜感激!

#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();

这是一个函数声明。它告诉编译器有一个函数processSelection不带参数并返回int - 但编译器已经知道这一点。

调用该函数,您需要编写:

processSelection();

dropSoda也是如此.

您的主要函数是声明函数,而不是调用它们。

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