Search code examples
c++program-entry-pointvisual-studio-2015voidcout

void() issue - It doesn't print results


I am writing a program split in three different files:

  1. 1) An header named my.h;
  2. A source cpp file named my.cpp;
  3. The main file named use.cpp;

Here their statements:

/* Header file my.h
Define global variable foo and functions print and print_foo
to print results out */

extern int foo;
void print_foo();
void print(int);

/* Source file my.cpp where are defined the two funcionts print_foo() and print()
and in where it is called the library std_lib_facilities.h */

#include "stdafx.h"
#include "std_lib_facilities.h"
#include "my.h"

void print_foo() {
    cout << "The value of foo is: " << foo << endl;
    return;
} 
void print(int i) {
    cout << "The value of i is: " << i << endl;
    return;
}

/ use.cpp : definisce il punto di ingresso dell'applicazione console.
//

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

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    int foo = 7;
    int& i = foo;
    i = 99;
    char cc = '0';

    while (cin >> cc) {
        switch (cc) {
        case '1':
            void print_foo();
            break;
        case '2':
            void print();
            break;
        default:
            exit(EXIT_FAILURE);
        }
    }

    return 0;
}

My main problem is that program compiles and run correctly but it doesn't print anything as I supposed.

How can I fix it?

Thank you!

Leo


Solution

  • To call a function specifying return type is not required. Correct

    void print_foo();    // This actually declares a function prototype
    

    to

    print_foo();
    

    and

    print(i);    // Pass i as argument