I have a question. do we have any function in C++ to get back the previous value while running the program?
For Example:
First Name: Anthony
Last Name: john
Gender:
the pointer is at the Gender value right now soo is there any way to get back up at the Last Name value to change the name in C++? I just start learning C++, thank everybody.

That would depend on the data structures you are using to store your data and what other variables you are maintaining. For example, it is a common practice to have a prev variable to keep track of where you were on a previous step (or a stack for deeper history). Or in the case of linked lists, a back link so you can traverse the list in either direction. If your data is being stored in an array than just subtract one from your array index.

You just create that function to navigate back to redo your input. Here, I check for input during the gender input to see if the letter 'b' (back) is entered. If so, the last name function is called.

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

int navigate();
int display();

int firstn();
int lastn();
int gen();

int nav = 0;
string inputs = " ";
string first = " ";
string last = " ";
string gender = " ";

int main()
{   
    while (1)
    {
        display();
        if (nav == 0) {
            firstn();
    }

        if (nav == 1){
            lastn();
    }

        if (nav == 2) {
            gen();
    }
        nav = 0;    
        }
    return 0;
}

int navigate()
{
    std::cout << "\n";
    if (nav == 0) {
        inputs = "First Name: ";
    }
    if (nav == 1)
    {
        inputs = "Last Name: ";
    }
    if (nav == 2)
    {
        inputs = "Gender: ";

    }

    return 0;
}

int display()
{   
    navigate();

    return 0;
}

int firstn()
{

    std::cout << inputs;
    std::cin >> first;
    nav = nav + 1;
    display();

    return 0;
}

int lastn()
{

    std::cout << inputs;
    std::cin >> last;
    nav = nav + 1;
    display();
    gen();
    return 0;
}

int gen()
{
    char gender[50], str[50]{};
    int len1, len2; 
    std::cout << inputs;
    std::cin >> gender;
    str[0] = 'b';
    len1 = strlen(gender);
    len2 = strlen(str);

    if (nav > 2) {

        nav = 0;
    }
    if (len1 == len2)
    {
        if (strcmp(gender, str) == 0) {
            nav = 1;
            display();
            lastn();
        } else
            std::cout << "\n";
    }
    else
        std::cout << "\n";

    nav = nav + 1;

    display();

    return 0;
}

First Name: Bill

Last Name: Gates

Gender: b

Last Name: Murray

Gender: M

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.