0

I have a variable, value, declared in a class, input. I want to use it inside main. Here is my code.

#include <iostream>
#include <fstream>
#include <chrono>
#include <thread>
#include <filesystem>
#include <string>


class input {
public : 
    int add() {
        std::string Type;
        std::ifstream typeFile("type.txt");
        if (!typeFile.is_open()) {
            std::cerr << "Error opening variable type file." << std::endl;
            return 1;
        }

        std::getline(typeFile, Type);
        typeFile.close();

   
        std::string Data;
        std::ifstream dataFile("data.txt");
        if (!dataFile.is_open()) {
            std::cerr << "Error opening variable data file." << std::endl;
            return 1;
        }
        std::getline(dataFile, Data);
        dataFile.close();
        
        if (Type == "int") {
            int value = std::stoi(Data);
            
        } else if (Type == "bool") {
            bool value = std::stoi(Data);
            
        } else if (Type == "string") {
            std::string value = Data;       
        } else {
            std::cerr << "Unknown type." << std::endl;
            return 1;
        }
    }
};
int main() {

    input hello;
    hello.add();
    
}

I tried making the variable value public but I got an error message. I do not want to move the code in input to main, I just want it to be so I can use value in main. What I want is something where I can do

int main() {
    input hello;
    hello.add();
    std::cout << value;
}
5
  • 3
    If you want to use it in main(), it needs to be stored in the object (as a member variable) instead of being declared inside add(). Commented Aug 10 at 21:48
  • 2
    Either you return It and store the value of hello.add() in a variable when you call it in main, or you make it a field of the class. The best choice probably depends on what this class really represents (meaning if such value is supposed to be a state of your object or rather just the result of a function call) Commented Aug 10 at 21:50
  • 4
    It's much harder than you expect. C++ is a strongly typed language, all variables must have a single type. You don't have a single value variable, you have 3 completely unrelated ones (and only one of them lives near the end of add()). Sure, there are ways in which you can return "this-or-that", but all of them are quite above your current level. Commented Aug 10 at 21:51
  • 9
    Side note: Avoid writing stuff like "I got an error message." Instead copy the error message and paste it into the question. Commented Aug 10 at 22:25
  • Just out of curiosity, what this code suppose to do? What is desired functionality of input? Are you trying to read configuration of your program? If config is desired why not use some commonly used solution with YAML/JSon/XML/ini? Commented Aug 11 at 13:41

2 Answers 2

5

Your value variables are locally scoped to their blocks. Not only do they not exist in main but they don't even exist in input::add outside of their enclosing blocks.

Given that you're using the int return value from input::add to return a value indicating success or failure1 you likely want value to be a member variable. That will be difficult since apparently it can be either an int, bool, or a std::string. You might want to use std::variant. This value member variable can then be accessed with hello.value in main.

You may also wish to use exceptions on an I/O error, and return std::variant<int, bool, std::string> from the function.


1 Note that you don't return in all control flow paths, which is a problem itself. The main function is special and implicitly returns 0, but other functions are not special.

Sign up to request clarification or add additional context in comments.

Comments

4

Returning a std::variant might be an option:

std::optional<std::variant<int, bool, std::string>>
add(std::istream& typeIs, std::istream& dataIs)
{
    std::string Type;
    std::string Data;
    std::getline(typeIs, Type);
    std::getline(dataIs, Data);

    if (!typeIs || !dataIs)
    {
        std::cerr << "Error reading variable from file." << std::endl;
        return std::nullopt;
    }

    if (Type == "int") {
        return std::stoi(Data);
    } else if (Type == "bool") {
        return static_cast<bool>(std::stoi(Data));
    } else if (Type == "string") {
        return Data;       
    } else {
        std::cerr << "Unknown type." << std::endl;
        return std::nullopt;
    }
}

Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.