语法Query,在Visual C ++中调用模块

时间:2013-12-05 17:59:57

标签: c++

我已经尝试将我的模块化知识调整为Visual C ++,然而,在看似无穷无尽的搜索搜索语法时,我根本无法做到这一点。基本上在这段代码中,首先调用菜单,一旦用户输入他们的选择(到目前为止只有编码选项1),将该值返回到main,然后进入if语句并调用fahrenheit。我请求通过引用传递的语法,我知道C#的语法,但不是Visual C ++

这是代码。

    // Test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

void Celsius()
{
}

void fahrenheit()
{
    cout << "Success!" << endl; //....Outputs this just to see if the module is being called properly.
}

int menu(int Mystring) //....I was testing this syntax to pass the variable.
{
    cout << "What would you like to do : " << endl;
    cout << "1) Fanreheit to Celsius" << endl;
    cout << "2) Celsius to Fahrenheit" << endl;

    cout << "Choice : " ;

    cin >> Mystring;

    return Mystring;
}

int main()
{
    int celsius = 0;
    int fahrenheit = 0;
    int Mystring = 0;

    menu(Mystring); //....Testing this syntax to pass Mystring to menu.

    if (Mystring == 1) //....I was hoping the menu would return Mystring as value = 1.
    {
        fahrenheit(); //.......I want this to call fahrenheit module if Mystring = 1
    }
}

1 个答案:

答案 0 :(得分:3)

您所谈论的“事物”不称为模块,而是功能。这是一个非常大的差异,我认为你应该知道它,因为你几乎不会理解任何没有这些知识的文章。

清除后,您的代码中的问题是,您通过int menu(int Mystring))传递变量,而 - 为了在函数内部更改它 - 您需要通过引用指针

传递它
int menu(int &Mystring)

C ++中有很多关于函数的articles。你应该检查出来。