2

i am a beginner in programming in C++. In my class we were recently showed to use functions and parameters. We were left an assignment where we are supposed to work with functions, except the only problem is i can't seem to get a start. I have wrote two functions so far. My main function, which asks for input from the user. Also another function which uses this input to create a new integer i will later need. I'm sure there are several mistakes in my code, but right now i really only need to know why only my main function will execute. I have been looking for hours, and switching stuff around, just to get another function other than the main to run, but my it will only run the main function and then the program will end. After inputting data from the user, the program will end. I haven't been able to get any other function to run other than the main, since i started this assignment. I am using visual studio 2017. Sorry for the trouble.

#include <iostream>
#include <cmath>
#include <string>

using namespace std;
int digits(int zip);

int main() {
    int zip = 0;
    cout << "Enter zipcode: ";
    cin >> zip;

    return 0;
}

int digits(int zip){
    int d5 = int();
    d5 = zip % 10;
    cout << "test: " << d5 << endl;
    return d5;

        }
  • 5
    You must *call* the function from inside `main()`: `cin >> zip; digits(zip); return 0;` – BoBTFish Apr 11 '18 at 04:53
  • 1
    Main is the entry point of a C++ program. In main, you never call the `digits` function that you have. Take up a [C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and understand how functions work. – Vishaal Shankar Apr 11 '18 at 04:54
  • "Won't compile"? Really, what's the compile error? It looks to me like they compile just fine but are never reached. – Ben Voigt Apr 11 '18 at 04:59
  • 1
    @BenVoigt: Speculating, of course, but a strict compiler setting may turn a warning about unreachable code in `digits` into an error. A beginner who is making changes without understanding them might have done that. (Angel Martinez: this is why you must add the actual error message). – MSalters Apr 11 '18 at 09:24

3 Answers3

2

You need to call the function digits

 #include <iostream>
    #include <cmath>
    #include <string>

    using namespace std;
    int digits(int zip);

    int main() {
        int zip = 0;
        cout << "Enter zipcode: ";
        cin >> zip;
        int data = digits(zip);
        cout<<"test: "<<data<<endl;
        return 0;
    }

    int digits(int zip){
        int d5 = int();
        d5 = zip % 10;
        // cout << "test: " << d5 << endl;
        return d5;
        }
Ash
  • 473
  • 2
  • 10
0

C++ looks for the main function and executes whatever is in it. In your case, it creates a new variable 'zip', asks the user for input, then inserts that value into the zip variable. Then it returns 0 and stops running. You must call the function digits() inside int main(). Like so:

int main() {
    int zip = 0;
    cout << "Enter zipcode: ";
    cin >> zip;
    digits(zip);

    return 0;
}

int digits(int zip){
    int d5 = int();
    d5 = zip % 10;
    cout << "test: " << d5 << endl;
    return d5;
}

Additionally, be aware that after executing the digits() function this program will simply return 0, not manipulating the 'd5' variable (returned by int digits()) in any way.

Also, it is not a good idea to cout anything inside additional functions. This makes it difficult to reuse the function. To keep the function as versatile as possible, make sure it only performs one task. In your case it should look something like this:

int main() {
    int zip = 0;
    cout << "Enter zipcode: ";
    cin >> zip;
    cout << "test: " << digits(zip) << endl;

    return 0;
}

int digits(int zip){
    int d5 = int();
    d5 = zip % 10;
    return d5;
}
0

You need to call the function digits before hand otherwise the program will skip over the function entirely so the above code should actually look something like this:

#include <iostream>
#include <cmath>
#include <string>

using namespace std;
int digits(int zip);

int main() {
    int zip = 0;
    cout << "Enter zipcode: ";
    cin >> zip;

    return 0;
}

int digits(int zip){
    int d5 = int();
    d5 = zip % 10;
    cout << "test: " << d5 << endl;
    return d5;

        }

Also on a side note in d5 = zip % 10; if your looking for it to be more accurate just define d5 as a double or a float.