Home > Software design >  Problem with a variable with the same value in different functions (C )
Problem with a variable with the same value in different functions (C )

Time:01-08

Do you know if there is any way to save the value of an assigned variable in one function to use it in another function?

In this case, I have a program that asks me to enter the name of the variable nombre in the function registro(); to register a plant. For example, if I enter Maguey de sol, the program prints:

¿Quieres continuar con el registro de Maguey de sol?

So far everything works fine. The problem appears when I press any key to continue. The program should print:

La planta es Maguey de sol

But it only prints:

La planta es

I know that a solution is to include the code of the function registro(); in main();, but it is not a viable option, because in my program I use more functions.

This is the code:

#include <iostream>
#include <locale.h>     // Español
#include <string>       // Cadenas
#include <fstream>      // Ficheros

using namespace std;

bool registro();

int main(){
    setlocale(LC_CTYPE, "Spanish"); // Español
    
    string nombre;
    
    registro();
    
    cout << "La planta es " << nombre << endl;

    return 0;
}

bool registro(){    
    string nombre;
    
    cout << "\n \t Ingresa el nombre de la planta" << endl;
    cout << "\t (Máximo 15 caracteres) \n \n" << endl;
    
    cin.clear();
    fflush(stdin);
    
    while (getline(cin, nombre)){
        if (nombre.size() <= 15){
            cout << "\n \t ¿Quieres continuar con el registro de " << nombre << "?\n" << endl;
            system("pause");
            break;  
        }else{
            cout << "\n \t ¡El nombre debe tener un máximo de 15 caracteres! \n" << endl;   
        }
    }   
}

Thanks for your help.

CodePudding user response:

I think you need to use the global variable or use my way as below. Return value for registro function.

/******************************************************************************

                              Online C   Compiler.
               Code, Compile, Run and Debug C   program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <string>
using namespace std;

string registro();

int main(){
    setlocale(LC_CTYPE, "Spanish"); // Español
    
    string nombre= registro();
    
    
    
    cout << "La planta es " << nombre << endl;

    return 0;
}

string registro(){   
    string nombre;
    cout << "\n \t Ingresa el nombre de la planta" << endl;
    cout << "\t (Máximo 15 caracteres) \n \n" << endl;
    
    cin.clear();
    fflush(stdin);
    
    while (getline(cin, nombre)){
        cout<<nombre<<endl;
        if (nombre.size() <= 15){
            cout << "\n \t Quieres continuar con el registro de " << nombre << "?\n" << endl;
            system("pause");
            break;  
        }else{
            cout << "\n \t El nombre debe tener un máximo de 15 caracteres! \n" << endl;   
        }
    }   
    return nombre;
}

CodePudding user response:

Not sure about what you're trying to achieve, but to elaborate on Stephen Newell 's comment, here is a modified version of your code.

Although your nombre variables have the same name, they are technically different variables, and therefore have different values, in each of their respective scope. Learn more here: https://www.geeksforgeeks.org/scope-of-variables-in-c/

#include <iostream>
#include <locale.h> // Español
#include <string>   // Cadenas
#include <fstream>  // Ficheros

using namespace std;

string registro();

int main()
{
    setlocale(LC_CTYPE, "Spanish"); // Español

    string nombre;

    nombre = registro();

    cout << "La planta es " << nombre << endl;

    return 0;
}

string registro()
{
    string nombre;

    cout << "\n \t Ingresa el nombre de la planta" << endl;
    cout << "\t (Máximo 15 caracteres) \n \n"
         << endl;

    cin.clear();
    fflush(stdin);

    while (getline(cin, nombre))
    {
        if (nombre.size() <= 15)
        {
            cout << "\n \t ¿Quieres continuar con el registro de " << nombre << "?\n"
                 << endl;
            // system("pause");
            break;
        }
        else
        {
            cout << "\n \t ¡El nombre debe tener un máximo de 15 caracteres! \n"
                 << endl;
        }
    }
    return nombre;
}
  •  Tags:  
  • Related