eally-root
eally-root

Reputation: 25

How to use main variable in functions?

I got a function but i can't define any variable inside and global too. This function gets a char value from user. I need to define this value to main function. How can i do it? Thanks for helping guys.

This is my code. I made it like this but I define variables in global but I need to define this variables in only main.

#include <iostream>

using namespace std;

char cName[255], cSurname[255];

bool nameFunc() {

    cout << "Whats Your Name ?\n";
    cin >> cName;
    if (cName != NULL && cName[0] == '\0') {
        return false;
    }
    else {
        return true;
    }
}

bool surnameFunc() {
    cout << "Whats Your Surname ?\n";
    cin >> cSurname;
    if (cSurname != NULL && cSurname[0] == '\0') {
        return false;
    }
    else {
        return true;
    }
}

int main() {
    if (nameFunc() and surnameFunc()) {
        cout << "Hello, " << cName << " " << cSurname << "." << endl;
    }
    else {
        cout << "Error! Name or Surname is empty." << endl;
    }
    system("PAUSE");
    return 0;
}

Upvotes: 0

Views: 278

Answers (2)

Ted Lyngmo
Ted Lyngmo

Reputation: 117258

You could pass references to your variables to the functions. Since the char[] have a fixed length, you need to make sure that you don't write out-of-bounds which complicates things.

Example:

#include <iostream>

template<size_t N>
bool nameFunc(char (&cName)[N]) {
    std::cout << "Whats Your Name ?\n";
    std::cin.getline(cName, N);            // read at most N chars
    return cName[0] != '\0';
}

template<size_t N>
bool surnameFunc(char (&cSurname)[N]) {
    std::cout << "Whats Your Surname ?\n";
    std::cin.getline(cSurname, N);         // read at most N chars
    return cSurname[0] != '\0';
}

int main() {
    char cName[255], cSurname[255];

    if (nameFunc(cName) and surnameFunc(cSurname)) {
        std::cout << "Hello, " << cName << " " << cSurname << ".\n";
    }
    else {
        std::cout << "Error! Name or Surname is empty.\n";
    }
}

A much easier option would be to use std::strings and pass them by reference to the functions.

Upvotes: 4

PhM75
PhM75

Reputation: 90

Is it what you're looking for?

#include <iostream>

using namespace std;

bool nameFunc(char* cName) {

    cout << "Whats Your Name ?\n";
    cin >> cName;
    if (cName != NULL && cName[0] == '\0') {
        return false;
    }
    else {
        return true;
    }
}

bool surnameFunc(char* cSurname) {
    cout << "Whats Your Surname ?\n";
    cin >> cSurname;
    if (cSurname != NULL && cSurname[0] == '\0') {
        return false;
    }
    else {
        return true;
    }
}

int main() {
    char cName[255], cSurname[255];
    if (nameFunc(cName) and surnameFunc(cSurname)) {
        cout << "Hello, " << cName << " " << cSurname << "." << endl;
    }
    else {
        cout << "Error! Name or Surname is empty." << endl;
    }
    system("PAUSE");
    return 0;
}

Upvotes: -3

Related Questions